1111 Online Map (30 分)(兩次dij)
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
兩最短路,其中我漏看了後面的 就是如果最短的不唯一,輸出最快的,
最快的不唯一,輸出經過節點最少的
就是兩次Dij 程式碼繁瑣,其他倒是沒有什麼難度
程式碼
#include<bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
int path[505][505], tim[505][505];
int tmp1[505];
int vis[505],dis[505];
int point, m;
int road1[505],road2[505];
int cnt[505];
//求最短路的,如果路最短有多個 則求最快的
int Dij1(int beginn, int endd)
{
memset(vis,0,sizeof(vis));
for(int i = 0; i < point; i ++)
{
dis[i] = path[beginn][i];
tmp1[i] = tim[beginn][i];
if(dis[i] == inf || dis[i] == 0)
road1[i] = -1;
else
road1[i] = beginn;
}
vis[beginn] = 1;
for(int i = 0; i < point; i++)
{
int maxn = inf, u = -1;
for(int j = 0; j < point; j++)
{
if(vis[j] == 0 && dis[j] < maxn)
{
maxn = dis[j];
u = j;
}
}
if(u == -1)
break;
vis[u] = 1;
for(int j = 0; j < point; j++ )
{
if(vis[j] == 0 )
{
if(dis[u] + path[u][j] < dis[j])
{
dis[j] = dis[u] + path[u][j];
tmp1[j] = tmp1[u] + tim[u][j];
road1[j] = u;//表示i前一個節點
}else if(dis[u] + path[u][j] == dis[j] && tmp1[u] + tim[u][j] < tmp1[j])
road1[j] = u;
}
}
}
return dis[endd];
}
//求最快路的,如果最快路有多個,則求經過點最少的
int Dij2(int beginn, int endd)
{
memset(vis,0,sizeof(vis));
memset(cnt, 0, sizeof(cnt));
for(int i = 0; i < point; i ++)
{
dis[i] = tim[beginn][i];
if(dis[i] == inf || dis[i] == 0)
road2[i] = -1;
else
road2[i] = beginn;
}
cnt[beginn] = 1;
vis[beginn] = 1;
for(int i = 0; i < point; i++)
{
int maxn = inf, u = -1;
for(int j = 0; j < point; j++)
{
if(vis[j] == 0 && dis[j] < maxn)
{
maxn = dis[j];
u = j;
}
}
if(u == -1)
break;
vis[u] = 1;
for(int j = 0; j < point; j++ )
{
if(vis[j] == 0 )
{
if(dis[u] + tim[u][j] < dis[j])
{
dis[j] = dis[u] + tim[u][j];
road2[j] = u;
cnt[j] = cnt[u] + 1;
}else if(dis[u] + tim[u][j] == dis[j])
{
if(cnt[u] + 1 < cnt[j])
{
cnt[j] = cnt[u] + 1;
road2[j] = u;
}
}
}
}
}
return dis[endd];
}
//判斷兩條路是否相等
bool same(int dd1[], int now1, int dd2[], int now2)
{
if(now1 != now2)
return false;
for(int i = 1; i <= now1; i++)
{
if(dd1[i] != dd2[i])
return false;
}
return true;
}
int main()
{
scanf("%d %d",&point, &m);
for(int i = 0; i <= point; i ++)
{
for(int j = 0 ; j <= point; j ++)
path[i][j] = inf, tim[i][j] = inf;
path[i][i] = 0 , tim[i][i] = 0;
}
for(int i = 1; i <= m; i ++)
{
int x, y ,z ,u ,v;
scanf("%d %d %d %d %d",&x,&y,&z, &u, &v);
if(z == 1)
{
path[x][y] = u;
tim[x][y] = v;
}else
{
path[x][y] = path[y][x] = u;
tim[x][y] = tim[y][x] = v;
}
}
int beginn, endd;
scanf("%d %d",&beginn, & endd);
int totalpath = Dij1(beginn,endd);
int totaltim = Dij2(beginn,endd);
int gg = endd;
stack<int>st1,st2;
while(gg != -1){
st1.push(gg);
gg = road1[gg];
}
gg = endd;
while(gg != -1){
st2.push(gg);
gg = road2[gg];
}
int dd1[505],dd2[505],now1 = 0, now2 = 0;
while(st1.empty() == 0)
{
gg = st1.top();
st1.pop();
dd1[++now1] = gg;
}
while(st2.empty() == 0)
{
gg = st2.top();
st2.pop();
dd2[++now2] = gg;
}
// for(int i = 1; i <= now1; i++)
// printf("%d%c",dd1[i]," \n"[i==now1]);
// for(int i = 1; i <= now2; i++)
// printf("%d%c",dd2[i]," \n"[i==now2]);
if(same(dd1,now1,dd2,now2))
{
printf("Distance = %d; Time = %d: %d",totalpath,totaltim,dd1[1]);
for(int i = 2; i <= now1; i++)
printf(" -> %d",dd1[i]);
printf("\n");
}else
{
printf("Distance = %d: %d",totalpath,dd1[1]);
for(int i = 2; i <= now1; i++)
printf(" -> %d",dd1[i]);
printf("\n");
printf("Time = %d: %d",totaltim, dd2[1]);
for(int i = 2; i <= now2; i++)
printf(" -> %d",dd2[i]);
printf("\n");
}
return 0;
}