1111 Online Map(30 分)
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:
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:
Distance = 6: 3 -> 4 -> 8 -> 5
Time = 3: 3 -> 1 -> 5
Sample Input 2:
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:
Distance = 3; Time = 4: 3 -> 2 -> 5
Dijkstra解决。
#include<stdio.h>
#include<vector>
#include<algorithm>
using namespace std;
#define INF 0x3fffffff
#define NUM 505
int from,to;
int N,M;
int glen[NUM][NUM],gtime[NUM][NUM];
int path1[NUM],path2[NUM];
int dis[NUM],time1[NUM];
int time2[NUM],step[NUM];
bool visited[NUM];
void dijkstra1(){
fill(dis,dis+N,INF);
fill(time1,time1+N,INF);
fill(path1,path1+N,-1);
fill(visited,visited+N,false);
dis[from]=0;time1[from]=0;
while(1){
int t=-1,mindis=INF;
for(int i=0;i<N;i++){
if(!visited[i]&&dis[i]<mindis){
mindis=dis[i];t=i;
}
}
if(t==-1)
break;
visited[t]=true;
for(int i=0;i<N;i++){
if(!visited[i]&&glen[t][i]!=INF&&dis[i]>=dis[t]+glen[t][i]){
if(dis[i]==dis[t]+glen[t][i]&&time1[i]<=time1[t]+gtime[t][i]){
continue;
}
dis[i]=dis[t]+glen[t][i];
time1[i]=time1[t]+gtime[t][i];
path1[i]=t;
}
}
}//while
}
void dijkstra2(){
fill(step,step+N,INF);
fill(time2,time2+N,INF);
fill(path2,path2+N,-1);
fill(visited,visited+N,false);
time2[from]=0;step[from]=0;
while(1){
int t=-1,mintime=INF;
for(int i=0;i<N;i++){
if(!visited[i]&&time2[i]<mintime){
mintime=time2[i];t=i;
}
}
if(t==-1)
break;
visited[t]=true;
for(int i=0;i<N;i++){
if(!visited[i]&>ime[t][i]!=INF&&time2[i]>=time2[t]+gtime[t][i]){
if(time2[i]==time2[t]+gtime[t][i]&&step[i]<=step[t]+1){
continue;
}
time2[i]=time2[t]+gtime[t][i];
step[i]=step[t]+1;
path2[i]=t;
}
}
}//while
}
int main(){
scanf("%d%d",&N,&M);
fill(glen[0],glen[NUM-1]+NUM,INF);
fill(gtime[0],gtime[NUM-1]+NUM,INF);
int v1,v2,oneway,len,t;
for(int i=0;i<M;i++){
scanf("%d%d%d%d%d",&v1,&v2,&oneway,&len,&t);
if(oneway==0){
glen[v2][v1]=len;
gtime[v2][v1]=t;
}
glen[v1][v2]=len;
gtime[v1][v2]=t;
}
scanf("%d%d",&from,&to);
dijkstra1();
dijkstra2();
vector<int> p1,p2;
for(int i=to;i!=-1;i=path1[i])
p1.push_back(i);
for(int i=to;i!=-1;i=path2[i])
p2.push_back(i);
bool same=false;
if(p1.size()==p2.size()){
int i=0;
for(;i<p1.size();i++){
if(p1[i]!=p2[i]){
break;
}
}
if(i==p1.size())
same=true;
}
if(same){
printf("Distance = %d; Time = %d: %d",dis[to],time1[to],from);
for(int i=p1.size()-2;i>=0;i--){
printf(" -> %d",p1[i]);
}
}else{
printf("Distance = %d: %d",dis[to],from);
for(int i=p1.size()-2;i>=0;i--){
printf(" -> %d",p1[i]);
}
printf("\nTime = %d: %d",time2[to],from);
for(int i=p2.size()-2;i>=0;i--){
printf(" -> %d",p2[i]);
}
}
return 0;
}