Input our current position and a destination, an online map can recommend several paths. Now your job is to recommend two paths to your user: one is the shortest, and the other is the fastest. It is guaranteed that a path exists for any request.
Input Specification:
Each input file contains one test case. For each case, the first line gives two positive integers N (2 <= N <= 500), and M, being the total number of streets intersections on a map, and the number of streets, respectively. Then M lines follow, each describes a street in the format:
V1 V2 one-way length time
where V1 and V2 are the indices (from 0 to N-1) of the two ends of the street; one-way is 1 if the street is one-way from V1 to V2, or 0 if not;length is the length of the street; and time is the time taken to pass the street.
Finally a pair of source and destination is given.
Output Specification:
For each case, first print the shortest path from the source to the destination with distance D in the format:
Distance = D: source -> v1 -> … -> destination
Then in the next line print the fastest path with total time T:
Time = T: source -> w1 -> … -> destination
In case the shortest path is not unique, output the fastest one among the shortest paths, which is guaranteed to be unique. In case the fastest path is not unique, output the one that passes through the fewest intersections, which is guaranteed to be unique.
In case the shortest and the fastest paths are identical, print them in one line in the format:
Distance = D; Time = T: source -> u1 -> … -> destination
Sample Input 1:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
10 15 0 1 0 1 1 8 0 0 1 1 4 8 1 1 1 3 4 0 3 2 3 9 1 4 1 0 6 0 1 1 7 5 1 2 1 8 5 1 2 1 2 3 0 2 2 2 1 1 1 1 1 3 0 3 1 1 4 0 1 1 9 7 1 3 1 5 1 0 5 2 6 5 1 1 2 3 5 |
Sample Output 1:
1 2 |
Distance = 6: 3 -> 4 -> 8 -> 5 Time = 3: 3 -> 1 -> 5 |
Sample Input 2:
1 2 3 4 5 6 7 8 9 10 11 |
7 9 0 4 1 1 1 1 6 1 1 3 2 6 1 1 1 2 5 1 2 2 3 0 0 1 1 3 1 1 1 3 3 2 1 1 2 4 5 0 2 2 6 5 1 1 2 3 5 |
Sample Output 2:
1 |
Distance = 3; Time = 4: 3 -> 2 -> 5 |
用 dijkstra 算法即可。
不过,我实现的方式十分不优雅,我分别写了两个函数来寻径……
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
#include<stdio.h> #include<stdlib.h> #include<memory.h> typedef struct mgraph_ { int length; int time_cost; } mgraph; mgraph graph[500][500]; typedef struct vertex_ { int known; int parent; int distance; int time_cost; } vertex; int find_shortest_unknown(vertex *path,int n) { int i,min=-1; for(i=0;i<n;i++) { if(path[i].known==-1&&path[i].distance!=-1&&(min==-1||path[i].distance<path[min].distance)) { min=i; } } return min; } int find_fastest_unknown(vertex *path,int n) { int i,min=-1; for(i=0;i<n;i++) { if(path[i].known==-1&&path[i].time_cost!=-1&&(min==-1||path[i].time_cost<path[min].time_cost)) { min=i; } } return min; } int shortest_path(vertex *path,int n) { int temp,i; while(1) { temp=find_shortest_unknown(path,n); if(temp==-1) { break; } path[temp].known=1; for(i=0;i<n;i++) { if(graph[temp][i].length>0&&path[i].known==-1) { if(path[i].distance==-1||path[i].distance>path[temp].distance+graph[temp][i].length) { path[i].distance=path[temp].distance+graph[temp][i].length; path[i].time_cost=path[temp].time_cost+graph[temp][i].time_cost; path[i].parent=temp; } else if(path[i].distance==path[temp].distance+graph[temp][i].length) { if(path[i].time_cost>path[temp].time_cost+graph[temp][i].time_cost) { path[i].time_cost=path[temp].time_cost+graph[temp][i].time_cost; path[i].parent=temp; } } } } } } int fastest_path(vertex *path,int n) { int temp,i; while(1) { temp=find_fastest_unknown(path,n); if(temp==-1) { break; } path[temp].known=1; for(i=0;i<n;i++) { if(graph[temp][i].time_cost>0&&path[i].known==-1) { if(path[i].time_cost==-1||graph[temp][i].time_cost+path[temp].time_cost<path[i].time_cost) { path[i].distance=path[temp].distance+1; path[i].time_cost=graph[temp][i].time_cost+path[temp].time_cost; path[i].parent=temp; } else if(graph[temp][i].time_cost+path[temp].time_cost==path[i].time_cost) { if(path[temp].distance+1<path[i].distance) { path[i].distance=path[temp].distance+1; path[i].parent=temp; } } } } } } int main() { int n,m,i,j,v1,v2,state,length,time_cost,source,destination,temp,*result[2],offset[2]; vertex *path[2]; scanf("%d%d",&n,&m); result[0]=(int *)malloc(n*sizeof(int)); result[1]=(int *)malloc(n*sizeof(int)); for(i=0;i<m;i++) { scanf("%d%d%d%d%d",&v1,&v2,&state,&length,&time_cost); graph[v1][v2].length=length; graph[v1][v2].time_cost=time_cost; if(state==0) { graph[v2][v1].length=length; graph[v2][v1].time_cost=time_cost; } } scanf("%d%d",&source,&destination); for(i=0;i<2;i++) { path[i]=(vertex *)malloc(n*sizeof(vertex)); memset(path[i],0xFF,n*sizeof(vertex)); path[i][source].distance=0; path[i][source].time_cost=0; } shortest_path(path[0],n); fastest_path(path[1],n); for(i=0;i<2;i++) { temp=destination; offset[i]=0; while(path[i][temp].parent!=-1) { result[i][offset[i]++]=temp; temp=path[i][temp].parent; } result[i][offset[i]]=temp; } if(offset[0]==offset[1]) { for(i=0;i<offset[0];i++) { if(result[0][i]!=result[1][i]) { break; } } if(i==offset[0]) { printf("Distance = %d; Time = %d: ",path[0][destination].distance,path[0][destination].time_cost); printf("%d",source); for(i=offset[0]-1;i>=0;i--) { printf(" -> %d",result[0][i]); } printf("\n"); return 0; } } printf("Distance = %d: ",path[0][destination].distance); printf("%d",source); for(i=offset[0]-1;i>=0;i--) { printf(" -> %d",result[0][i]); } printf("\n"); printf("Time = %d: ",path[1][destination].time_cost); printf("%d",source); for(i=offset[1]-1;i>=0;i--) { printf(" -> %d",result[1][i]); } printf("\n"); } |