【PAT】A1111 Online Map (30 point(s))


Author: CHEN, Yue
Organization: 浙江大学
Time Limit: 400 ms
Memory Limit: 64 MB
Code Size Limit: 16 KB

A1111 Online Map (30 point(s))

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

Code

#include <stdio.h>
#include <iostream>
#include <vector>
using namespace std;
int n,m,u,v,isoneway,l,t;
int Glength[510][510],Gtime[510][510];
int dL[510],dT[510];
int visL[510],visT[510];
int minLength=1e9,minTime1=1e9,minTime2=1e9,minInter=1e9;
vector<int> preL[510],preT[510];
vector<int>tempLengthPath,lengthPath,tempTimePath,timePath;
void dijkstraLength(){
    fill(dL,dL+510,1e9);
    dL[u]=0;
    for(int i=0;i<n;i++){
        int a=-1,minL=1e9;
        for(int j=0;j<n;j++){
            if(visL[j]==0&&dL[j]<minL){
                a=j;
                minL=dL[j];
            }
        }
        if(a==-1)   return;
        visL[a]=1;
        for(int b=0;b<n;b++){
            if(visL[b]==0&&Glength[a][b]!=1e9){
                if(dL[a]+Glength[a][b]<dL[b]){
                    dL[b]=dL[a]+Glength[a][b];
                    preL[b].clear();
                    preL[b].push_back(a);
                }
                else if(dL[b]==dL[a]+Glength[a][b]){
                    preL[b].push_back(a);
                }
            }
        }
    }
}
void dijkstraTime(){
    fill(dT,dT+510,1e9);
    dT[u]=0;
    for(int i=0;i<n;i++){
        int a=-1,minT=1e9;
        for(int j=0;j<n;j++){
            if(visT[j]==0&&dT[j]<minT){
                a=j;
                minT=dT[j];
            }
        }
        if(a==-1)   return;
        visT[a]=1;
        for(int b=0;b<n;b++){
            if(visT[b]==0&&Gtime[a][b]!=1e9){
                if(dT[a]+Gtime[a][b]<dT[b]){
                    dT[b]=dT[a]+Gtime[a][b];
                    preT[b].clear();
                    preT[b].push_back(a);
                }
                else if(dT[b]==dT[a]+Gtime[a][b]){
                    preT[b].push_back(a);
                }
            }
        }
    }
}
void dfslength(int root,int costlength,int usetime){
    if(root==u&&costlength<minLength){
        minLength=costlength;
        minTime1=usetime;
        lengthPath=tempLengthPath;
    }
    if(root==u&&costlength==minLength&&usetime<minTime1){
        minTime1=usetime;
        lengthPath=tempLengthPath;
    }
    if(root==u) return;
    for(int i=0;i<preL[root].size();i++){
        tempLengthPath.push_back(preL[root][i]);
        dfslength(preL[root][i],costlength+Glength[preL[root][i]][root],usetime+Gtime[preL[root][i]][root]);
        tempLengthPath.pop_back();
    }
}
void dfstime(int root,int usetime,int inter){
    if(root==u&&usetime<minTime2){
        minTime2=usetime;
        minInter=inter;
        timePath=tempTimePath;
    }
    if(root==u&&usetime==minTime2&&inter<minInter){
        minInter=inter;
        timePath=tempTimePath;
    }
    if(root==u) return;
    for(int i=0;i<preT[root].size();i++){
        tempTimePath.push_back(preT[root][i]);
        dfstime(preT[root][i],usetime+Gtime[preT[root][i]][root],inter+1);
        tempTimePath.pop_back();
    }
}
int main(){
    scanf("%d %d",&n,&m);
    fill(Glength[0],Glength[0]+510*510,1e9);
    fill(Gtime[0],Gtime[0]+510*510,1e9);
    for(int i=0;i<m;i++){
        scanf("%d %d %d %d %d",&u,&v,&isoneway,&l,&t);
        if(isoneway==0){
            Glength[u][v]=Glength[v][u]=l;
            Gtime[u][v]=Gtime[v][u]=t;
        }
        else{
            Glength[u][v]=l;
            Gtime[u][v]=t;
        }
    }
    scanf("%d %d",&u,&v);
    dijkstraLength();
    dijkstraTime();
    tempLengthPath.push_back(v);
    dfslength(v,0,0);
    tempLengthPath.pop_back();
    tempTimePath.push_back(v);
    dfstime(v,0,1);
    tempTimePath.pop_back();
    if(lengthPath==timePath){
        printf("Distance = %d; Time = %d: %d",minLength,minTime2,u);
        for(int i=lengthPath.size()-2;i>=0;i--){
            printf(" -> %d",lengthPath[i]);
        }
        printf("\n");
    }
    else{
        printf("Distance = %d: %d",minLength,u);
        for(int i=lengthPath.size()-2;i>=0;i--){
            printf(" -> %d",lengthPath[i]);
        }
        printf("\n");
        printf("Time = %d: %d",minTime2,u);
        for(int i=timePath.size()-2;i>=0;i--){
            printf(" -> %d",timePath[i]);
        }
        printf("\n");
    }
    return 0;
}

Analysis

-超级繁琐的题,需要耐心。

-给一张地图,以及两点间的路径长度与通过所需时间。

-求一条最快到达的与一条最短距离,共两条路线。如果两条路线重合,则输出一条路线。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值