1111. Online Map (30)

来源:互联网 发布:mac可以玩跑跑卡丁车吗 编辑:程序博客网 时间:2024/06/11 19:09


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

#include<iostream>#include<queue>#include<vector>#include<cstdio>using namespace std;typedef pair<int ,int > P;int map_length[505][505];int map_time[505][505];int length[505],time1[505];int vis_length[505],vis_time[505];int path_length[505],path_time[505];int path1[505],path2[505],cro[505];int n,m;int start,endx;const int INF = 1<<29;void dij_length(int s){    for (int i=0;i<=n;i++)    {        vis_length[i]=0;        length[i]=INF;    }    length[s]=0;    priority_queue< P,vector<P>,greater<P> > que;    que.push({length[s],s});    while(que.size())    {        P p=que.top();        que.pop();        int num=p.second;        if (vis_length[num]) continue;        vis_length[num]=1;        for (int i=0;i<n;i++)        {            if (map_length[num][i]!=INF)            {                if (length[i]>length[num]+map_length[num][i])                {                    length[i]=length[num]+map_length[num][i];                    time1[i]=time1[num]+map_time[num][i];                    path_length[i]=num;                    que.push({length[i],i});                }                else if(length[i]==length[num]+map_length[num][i])                {                    if (time1[i]>time1[num]+map_time[num][i])                    {                        path_length[i]=num;                       time1[s]=time1[num]+map_time[num][i];                    }                }            }        }    }}void dij_time(int s){    for (int i=0;i<=n;i++)    {        vis_time[i]=0;        time1[i]=INF;        cro[i]=0;    }    time1[s]=0;    cro[s]=1;    priority_queue< P,vector<P>,greater<P> > que;    que.push({time1[s],s});    while(que.size())    {        P p=que.top();        que.pop();        int num=p.second;        if (vis_time[num]) continue;        vis_time[num]=1;        for (int i=0;i<n;i++)        {            if (map_time[num][i]!=INF)            {                if (time1[i]>time1[num]+map_time[num][i])                {                    time1[i]=time1[num]+map_time[num][i];                    path_time[i]=num;                    cro[i]=cro[num]+1;                    que.push({time1[i],i});                }                else if(time1[i]==time1[num]+map_time[num][i])                {                    if (cro[i]>cro[num]+1)                    {                        path_time[i]=num;                      cro[i]=cro[num]+1;                    }                }            }        }    }}int main(){    cin>>n>>m;    int v1,v2,o_way,len,tim;    for (int i=0;i<=n;i++)        for (int j=0;j<=n;j++)            map_length[i][j]=map_time[i][j]=INF;    for (int i=0;i<m;i++)    {        cin>>v1>>v2>>o_way>>len>>tim;        if(o_way==1)        {            map_length[v1][v2]=len;            map_time[v1][v2]=tim;        }        else        {            map_length[v1][v2]=map_length[v2][v1]=len;            map_time[v1][v2]=map_time[v2][v1]=tim;        }    }    cin>>start>>endx;    dij_length(start);    dij_time(start);    int temp=endx,index1=0;    while(path_length[temp]!=start)    {        path1[index1++]=temp;        temp=path_length[temp];    }    path1[index1++]=temp;    int temp2=endx,index2=0;    while(path_time[temp2]!=start)    {        path2[index2++]=temp2;        temp2=path_time[temp2];    }    path2[index2++]=temp2;    if (index1==index2)    {        int flag=1;        for (int i=0;i<index1;i++)        {            if (path1[i]!=path2[i])            {                flag=0;                break;            }        }        if (flag==1)        {            printf("Distance = %d; Time = %d: %d",length[endx],time1[endx],start);            for (int i=index1-1;i>=0;i--)                printf(" -> %d",path1[i]);            return 0;        }    }    printf("Distance = %d: %d",length[endx],start);    for (int i=index1-1;i>=0;i--)        printf(" -> %d",path1[i]);    printf("\nTime = %d: %d",time1[endx],start);    for (int i=index2-1;i>=0;i--)        printf(" -> %d",path2[i]);    return 0;}