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 (<=500) is the number of cities (and hence the cities are numbered from 0 to N-1); M is the number of highways; S and 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 Input4 5 0 3 0 1 1 20 1 3 2 30 0 3 4 10 0 2 2 20 2 3 1 20Sample Output
0 2 3 3 40
- #include<iostream>
- #include<cstdio>
- #include<memory.h>
- #include<algorithm>
- #include<cstring>
- #include<queue>
- #include<cmath>
- #include<vector>
- #include<cstdlib>
- #include<cmath>
- #include<iomanip>
- #include<string>
- #include<map>
- using namespace std;
- #define MAX 0xFFFFFFF
- int mapd[501][501];
- int mapp[501][501];
- int dis[501];
- int ps[501];
- int pre[501];
- struct Node{
- int dis;
- int p;
- int th;
- bool operator<(const Node & node)const{
- if(dis!=node.dis)
- return dis>node.dis;
- return p>node.p;
- }
- };
- int main(){
- //freopen("in.txt", "r", stdin);
- int n, m, s, d;
- priority_queue<Node> q;
- cin>>n>>m>>s>>d;
- for(int i=0;i<n;++i)
- for(int j=0;j<n;++j){
- mapd[i][j] = MAX;
- mapp[i][j] = MAX;
- dis[i] = MAX;
- }
- int a, b, dd, c;
- for(int i=0;i<m;++i){
- scanf("%d%d%d%d", &a, &b, &dd, &c);
- if(mapd[a][b]==MAX || mapd[a][b]!=MAX && dd<mapd[a][b]){
- mapd[a][b] = mapd[b][a] = dd;
- mapp[a][b] = mapp[b][a] = c;
- }else if(mapd[a][b]!=MAX && dd==mapd[a][b] && c<mapp[a][b]){
- mapp[a][b] = mapp[b][a] = c;
- }
- }
- dis[s] = 0;
- Node node;
- node.dis = 0;
- node.p = 0;
- node.th = s;
- q.push(node);
- while(!q.empty()){
- Node node = q.top();
- q.pop();
- for(int i=0;i<n;++i){
- if(mapd[node.th][i]!=MAX && dis[node.th]+mapd[node.th][i]<dis[i]){
- dis[i] = dis[node.th]+mapd[node.th][i];
- ps[i] = ps[node.th] + mapp[node.th][i];
- Node tmp;
- tmp.dis = dis[i];
- tmp.th = i;
- tmp.p = ps[node.th] + mapp[node.th][i];
- pre[i] = node.th;
- q.push(tmp);
- }else if(mapd[node.th][i]!=MAX && dis[node.th]+mapd[node.th][i]==dis[i] && ps[node.th]+mapp[node.th][i]<ps[i]){
- ps[i] = ps[node.th]+mapp[node.th][i];
- Node tmp;
- tmp.dis = dis[i];
- tmp.th = i;
- tmp.p = ps[i];
- pre[i] = node.th;
- q.push(tmp);
- }
- }
- }
- vector<int> ans;
- int now = d;
- while(now!=s){
- ans.push_back(now);
- now = pre[now];
- }
- ans.push_back(s);
- printf("%d", ans[ans.size()-1]);
- for(int i=ans.size()-2;i>=0;i--){
- printf(" %d", ans[i]);
- }
- printf(" %d %d\n", dis[d], ps[d]);
- //fclose(stdin);
- }