欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

PAT A1111. Online Map (30)

程序员文章站 2022-06-11 17:59:11
...
  1. Online Map (30)

时间限制
300 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue
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<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
using namespace std;

const int MAXV=510;
const int INF=0x3fffffff;

int n,m,st,ed,G[MAXV][MAXV],Time[MAXV][MAXV];
int d[MAXV],t[MAXV],minTime=INF;
bool visD[MAXV]={false},visT[MAXV]={false};
vector<int>tempPath,pathD,pathT;
vector<int>preD[MAXV],preT[MAXV];

void Dijkstra(int s,int G[][MAXV],int d[],vector<int>pre[],bool vis[]){
    fill(d,d+MAXV,INF);
    d[s]=0;
    for(int i=0;i<n;i++){
        int u=-1,MIN=INF;
        for(int j=0;j<n;j++){
            if(vis[j]==false&&d[j]<MIN){
                u=j;
                MIN=d[j];
            }
        }
        if(u==-1)return;
        vis[u]=true;
        for(int v=0;v<n;v++){
            if(vis[v]==false&&G[u][v]!=INF){
                if(d[u]+G[u][v]<d[v]){
                    d[v]=d[u]+G[u][v];
                    pre[v].clear();
                    pre[v].push_back(u);
                }else if(d[u]+G[u][v]==d[v]){
                    pre[v].push_back(u);
                }
            }
        }
    }
}

void getFirstPath(){
    int tempTime=0;
    for(int i=tempPath.size()-1;i>0;i--){
        int id=tempPath[i],idNext=tempPath[i-1];
        tempTime+=Time[id][idNext];
    }
    if(tempTime<minTime){
        minTime=tempTime;
        pathD=tempPath;
    }
}

void getSecondPath(){
    if(pathT.size()==0||tempPath.size()<pathT.size()){
        pathT=tempPath;
    }
}

void DFS(int v,vector<int>pre[],bool isFirstPath){
    if(v==st){
        tempPath.push_back(v);
        if(isFirstPath)getFirstPath();
        else getSecondPath();
        tempPath.pop_back();
        return;
    }
    tempPath.push_back(v);
    for(int i=0;i<pre[v].size();i++){
        DFS(pre[v][i],pre,isFirstPath);
    }
    tempPath.pop_back();
}

void printPath(vector<int>path){
    printf("%d",path.back());
    for(int i=path.size()-2;i>=0;i--){
        printf(" -> %d",path[i]);
    }
    printf("\n");
}

int main(){
    scanf("%d%d",&n,&m);
    int u,v,oneWay;
    fill(G[0],G[0]+MAXV*MAXV,INF);
    fill(Time[0],Time[0]+MAXV*MAXV,INF);
    for(int i=0;i<m;i++){
        scanf("%d%d%d",&u,&v,&oneWay);
        scanf("%d%d",&G[u][v],&Time[u][v]);
        if(!oneWay){
            G[v][u]=G[u][v];
            Time[v][u]=Time[u][v];
        }
    }
    scanf("%d%d",&st,&ed);
    Dijkstra(st,G,d,preD,visD);
    Dijkstra(st,Time,t,preT,visT);
    DFS(ed,preD,true);
    DFS(ed,preT,false);
    printf("Distance = %d",d[ed]);
    if(pathD==pathT){
        printf("; ");
    }else{
        printf(": ");
        printPath(pathD);
    }
    printf("Time = %d: ",t[ed]);
    printPath(pathT);
    return 0;
}

PAT A1111. Online Map (30)