1030 Travel Plan (30 分)
Posted zllwxm123
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了1030 Travel Plan (30 分)相关的知识,希望对你有一定的参考价值。
A traveler‘s map gives the distances between cities along the highways, together with the cost of each highway. Now you are supposed to write a program to help a traveler to decide the shortest path between his/her starting city and the destination. If such a shortest path is not unique, you are supposed to output the one with the minimum cost, which is guaranteed to be unique.
Input Specification:
Each input file contains one test case. Each case starts with a line containing 4 positive integers N, M, S, and D, where N (≤) is the number of cities (and hence the cities are numbered from 0 to N−1); M is the number of highways; Sand D are the starting and the destination cities, respectively. Then M lines follow, each provides the information of a highway, in the format:
City1 City2 Distance Cost
where the numbers are all integers no more than 500, and are separated by a space.
Output Specification:
For each test case, print in one line the cities along the shortest path from the starting point to the destination, followed by the total distance and the total cost of the path. The numbers must be separated by a space and there must be no extra space at the end of output.
Sample Input:
4 5 0 3
0 1 1 20
1 3 2 30
0 3 4 10
0 2 2 20
2 3 1 20
Sample Output:
0 2 3 3 40
最短路的板子题算是。
1 #include <bits/stdc++.h> 2 #define N 600 3 #define inf 0x3f3f3f3f 4 using namespace std; 5 int n, m, s, d; 6 struct Node 7 int to, val, cost; 8 friend bool operator<(const Node &a, const Node &b) 9 if(a.val == b.val) 10 return a.cost > b.cost; 11 return a.val > b.val; 12 13 ; 14 stack<int> st; 15 vector<Node> v[N]; 16 int value[N],vis[N]; 17 int path[N]; 18 int fa[N]; 19 int min_val = inf, min_cost = inf; 20 priority_queue<Node> q; 21 void dij(int x) 22 Node node; 23 node.to = x, node.val = 0, node.cost = 0; 24 q.push(node); 25 while(!q.empty()) 26 node = q.top(); 27 q.pop(); 28 if(vis[node.to]) 29 continue; 30 vis[node.to] = 1; 31 value[node.to] = min(value[node.to], node.val); 32 for(int i = 0 ; i < v[node.to].size(); i++) 33 Node ans = v[node.to][i]; 34 ans.val += value[node.to]; 35 ans.cost += node.cost; 36 if(ans.val <= value[ans.to]) 37 value[ans.to] = ans.val; 38 path[ans.to] = node.to; 39 40 if(ans.to == d) 41 if(ans.val < min_val) 42 min_val = ans.val; 43 min_cost = ans.cost; 44 for(int i = 0 ; i <= n; i++) 45 fa[i] = path[i]; 46 47 else if(ans.val == min_val) 48 if(ans.cost < min_cost) 49 min_cost = ans.cost; 50 for(int i = 0 ; i <= n; i++) 51 fa[i] = path[i]; 52 53 54 55 56 q.push(ans); 57 58 59 60 61 62 int main() 63 cin >> n >> m >> s >> d; 64 int w,x,y,z; 65 for(int i = 0; i < m; i++) 66 cin >> x >> y >> z >> w; 67 v[x].push_back(y,z,w); 68 v[y].push_back(x,z,w); 69 70 memset(value, inf, sizeof(value)); 71 dij(s); 72 while(d != s) 73 st.push(d); 74 d = fa[d]; 75 76 cout << s; 77 while(!st.empty()) 78 cout <<" "<<st.top(); 79 st.pop(); 80 81 cout <<" " <<min_val<<" "<<min_cost<<endl; 82 return 0; 83
以上是关于1030 Travel Plan (30 分)的主要内容,如果未能解决你的问题,请参考以下文章
1030 Travel Plan (30 分) 难度: 中 / 知识点: 最短路