POJ 1724 ROADS(使用邻接表和优先队列的BFS求解最短路问题)

时间:2023-08-12 12:00:43

题目链接:

https://cn.vjudge.net/problem/POJ-1724

N cities named with numbers 1 ... N are connected with one-way roads. Each road has two parameters associated with it : the road length and the toll that needs to be paid for the road (expressed in the number of coins).
Bob and Alice used to live in the city 1. After noticing that
Alice was cheating in the card game they liked to play, Bob broke up
with her and decided to move away - to the city N. He wants to get there
as quickly as possible, but he is short on cash.

We want to help Bob to find
the shortest path from the city 1 to the city N
that he can afford with the amount of money he has.

Input

The first line of the input contains the integer K, 0 <= K
<= 10000, maximum number of coins that Bob can spend on his way.

The second line contains the integer N, 2 <= N <= 100, the total number of cities.

The third line contains the integer R, 1 <= R <= 10000, the total number of roads.

Each of the following R lines describes one road by specifying integers S, D, L and T separated by single blank characters :

  • S is the source city, 1 <= S <= N
  • D is the destination city, 1 <= D <= N
  • L is the road length, 1 <= L <= 100
  • T is the toll (expressed in the number of coins), 0 <= T <=100

Notice that different roads may have the same source and destination cities.

Output

The first and the only line of the output should contain the
total length of the shortest path from the city 1 to the city N whose
total toll is less than or equal K coins.

If such path does not exist, only number -1 should be written to the output.

Sample Input

5
6
7
1 2 2 3
2 4 3 3
3 4 2 4
1 3 4 1
4 6 2 1
3 5 2 0
5 4 3 2

Sample Output

11
 /*
题意描述
输入总钱数k,顶点数n,边数m
每一条边s到d长度是l,花费是t
可能有重边
问在不超过k的前提下,从1到n的最短路径是多少,如果不存在这样的最短路输出-1. 解题思路
使用用优先队列的广搜,保证每次弹出的结点是距离最短,而且花费相对小的,搜索的时候不用像矩阵中那样标记哪个结点用过了,因为使用
的是每个结点的邻接边,标记了可能就不能用这条边了,故直接搜索。还需注意的是一定要是弹出的结点是终点再结束,如果在搜索的时候遇
到直接结束,可能这条路径不是最短的,但是从优先队列里第一个弹出的一定是最短的。
*/
#include<cstdio>
#include<vector>
#include<queue>
#include<cstring>
const int maxn = ;
const int INF = 0x3f3f3f3f;
using namespace std; struct Edge{
int from, to, dist, pay;
Edge(int s, int d, int l, int t) : from(s), to(d), dist(l), pay(t) { };
}; struct HeapNode {
int u, d, c;
bool operator < (const HeapNode& a) const {
if(d == a.d){
return c > a.c;
}
return d > a.d;
}
}; struct BFS {
int n,m;
vector<Edge> edges;
vector<int> G[maxn];
bool done[maxn]; void init(int n) {
this->n = n;
for(int i = ; i < n; i++)
G[i].clear();
edges.clear();
} void AddEdge(int from, int to, int dist, int pay) {
edges.push_back(Edge(from, to, dist, pay));
m = edges.size();
G[from].push_back(m - );
} int bfs(int k, int s) {
memset(done, , sizeof(done));
done[s] = ; priority_queue<HeapNode> q;
q.push((HeapNode){s, , });
while(!q.empty()) {
HeapNode x = q.top();
q.pop();
int u = x.u;
if(u == n - )
return x.d;
for(int i = ; i < G[u].size(); i++) {
Edge e = edges[G[u][i]];
if(k >= e.pay + x.c)
q.push((HeapNode){e.to, x.d+e.dist, e.pay + x.c});
}
}
return -;
}
}; struct BFS solve;
int main()
{
int k, n, m, s, d, l, t;
while(scanf("%d", &k) != EOF) {
scanf("%d%d", &n, &m); solve.init(n);
for(int i = ; i <= m; i++) {
scanf("%d%d%d%d",&s, &d, &l, &t);
s--;d--;
solve.AddEdge(s, d, l, t);//先--再使用
} printf("%d\n", solve.bfs(k,));
}
return ;
}