1. 程式人生 > >POJ 1724 ROADS(使用鄰接表和優先隊列的BFS求解最短路問題)

POJ 1724 ROADS(使用鄰接表和優先隊列的BFS求解最短路問題)

pos sep second size output ear 相對 ont queue

題目鏈接:

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
 1 /*
 2 題意描述
 3 輸入總錢數k,頂點數n,邊數m
 4 每一條邊s到d長度是l,花費是t
 5 可能有重邊
 6 問在不超過k的前提下,從1到n的最短路徑是多少,如果不存在這樣的最短路輸出-1.
 7  
 8 解題思路 
 9 使用用優先隊列的廣搜,保證每次彈出的結點是距離最短,而且花費相對小的,搜索的時候不用像矩陣中那樣標記哪個結點用過了,因為使用
10 的是每個結點的鄰接邊,標記了可能就不能用這條邊了,故直接搜索。還需註意的是一定要是彈出的結點是終點再結束,如果在搜索的時候遇
11 到直接結束,可能這條路徑不是最短的,但是從優先隊列裏第一個彈出的一定是最短的。 
12 */ 
13 #include<cstdio>
14 #include<vector>
15 #include<queue>
16 #include<cstring>
17 const int maxn = 110;
18 const int INF = 0x3f3f3f3f;
19 using namespace std;
20 
21 struct Edge{
22     int from, to, dist, pay;
23     Edge(int s, int d, int l, int t) : from(s), to(d), dist(l), pay(t) { };
24 };
25  
26 struct HeapNode {
27     int u, d, c;
28     bool operator < (const HeapNode& a) const {
29         if(d == a.d){
30             return c > a.c;
31         }
32         return d > a.d;
33     }
34 };
35 
36 struct BFS {
37     int n,m;
38     vector<Edge> edges;
39     vector<int> G[maxn];
40     bool done[maxn];
41     
42     void init(int n) {
43         this->n = n;
44         for(int i = 0; i < n; i++)
45             G[i].clear();
46         edges.clear();
47     }
48     
49     void AddEdge(int from, int to, int dist, int pay) {
50         edges.push_back(Edge(from, to, dist, pay));
51         m = edges.size();
52         G[from].push_back(m - 1);
53     }
54     
55     int bfs(int k, int s) {
56         memset(done, 0, sizeof(done));
57         done[s] = 1;
58         
59         priority_queue<HeapNode> q;
60         q.push((HeapNode){s, 0, 0});
61         while(!q.empty()) {
62             HeapNode x = q.top();
63             q.pop();
64             int u = x.u;
65             if(u == n - 1)
66                 return x.d;
67             for(int i = 0; i < G[u].size(); i++) {
68                 Edge e = edges[G[u][i]];
69                 if(k >= e.pay + x.c)
70                     q.push((HeapNode){e.to, x.d+e.dist, e.pay + x.c});
71             }
72         }
73         return -1;
74     }
75 };
76 
77 struct BFS solve;
78 int main() 
79 {
80     int k, n, m, s, d, l, t;
81     while(scanf("%d", &k) != EOF) {
82         scanf("%d%d", &n, &m);
83         
84         solve.init(n);
85         for(int i = 1; i <= m; i++) {
86             scanf("%d%d%d%d",&s, &d, &l, &t);
87             s--;d--;
88             solve.AddEdge(s, d, l, t);//先--再使用 
89         }
90         
91         printf("%d\n", solve.bfs(k,0));
92     }
93     return 0;
94 } 

POJ 1724 ROADS(使用鄰接表和優先隊列的BFS求解最短路問題)