PAT-A1111 Online Map 题目内容及题解

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

题目大意

题目给出一个图,要求分别找出从源到目的地最短的路径和最快的路径,并将其按照题目要求的格式输出。

解题思路

  1. 初始化并读取路线图;
  2. 分别用Dijkstra算法求解最短的和最快的路径,并用DFS分别求出其路径;
  3. 对比并按照题目要求输出路径;
  4. 返回零值。

代码

#include<cstdio>
#include<vector>
using namespace std;
#define maxn 510
#define INF 1000000000

int N,M,sourse,destination;
int D[maxn],T[maxn],T1[maxn],Nnum[maxn];
int vis[maxn];
int preD[maxn],preT[maxn];
vector<int> TD,TT;

struct Node{
    int v;
    int l;
    int t;
};

vector<Node> G[maxn];

void Init(){
    int i;
    int V1,V2,flag,l,t;
    Node temp;
    scanf("%d%d",&N,&M);
    for(i=0;i<maxn;i++){
        D[i]=INF;
        T[i]=INF;
        T1[i]=INF;
        Nnum[i]=INF;
    }
    for(i=0;i<M;i++){
        scanf("%d%d%d%d%d",&V1,&V2,&flag,&l,&t);
        temp.v=V2;
        temp.l=l;
        temp.t=t;
        G[V1].push_back(temp);
        if(flag!=1){
            temp.v=V1;
            temp.l=l;
            temp.t=t;
            G[V2].push_back(temp);
        } 
    }
    scanf("%d%d",&sourse,&destination);
}

void DDFS(int now){
    TD.push_back(now);
    if(now==sourse){
        return;
    }
    DDFS(preD[now]);
}

void DDijkstra(){
    int i,j;
    int u,Min;
    Node temp;
    for(i=0;i<N;i++){
        vis[i]=0;
    }
    D[sourse]=0;
    T1[sourse]=0;
    for(i=0;i<N;i++){
        u=-1,Min=INF;
        for(j=0;j<N;j++){
            if(vis[j]==0&&D[j]<Min){
                u=j;
                Min=D[u];
            }
        }
        if(u==-1){
            break;
        }
        vis[u]=1;
        for(j=0;j<G[u].size();j++){
            temp=G[u][j];
            if(vis[temp.v]==0){
                if(D[temp.v]>D[u]+temp.l){
                    D[temp.v]=D[u]+temp.l;
                    preD[temp.v]=u;
                    T1[temp.v]=T1[u]+temp.t;
                }else if(D[temp.v]==D[u]+temp.l&&T1[temp.v]>T1[u]+temp.t){
                    preD[temp.v]=u;
                    T1[temp.v]=T1[u]+temp.t;
                }
            }
            
        }
        if(u==destination){
            break;
        }
    }
    DDFS(destination);
}

void TDFS(int now){
    TT.push_back(now);
    if(now==sourse){
        return;
    }
    TDFS(preT[now]);
}

void TDijkstra(){
    int i,j;
    int u,Min;
    Node temp;
    for(i=0;i<N;i++){
        vis[i]=0;
    }
    T[sourse]=0;
    Nnum[sourse]=0;
    for(i=0;i<N;i++){
        u=-1,Min=INF;
        for(j=0;j<N;j++){
            if(vis[j]==0&&T[j]<Min){
                u=j;
                Min=T[u];
            }
        }
        if(u==-1){
            break;
        }
        vis[u]=1;
        for(j=0;j<G[u].size();j++){
            temp=G[u][j];
            if(vis[temp.v]==0){
                if(T[temp.v]>T[u]+temp.t){
                    T[temp.v]=T[u]+temp.t;
                    preT[temp.v]=u;
                    Nnum[temp.v]=Nnum[u]+1;
                }else if(T[temp.v]==T[u]+temp.t&&Nnum[temp.v]>Nnum[u]+1){
                    preT[temp.v]=u;
                    Nnum[temp.v]=Nnum[u]+1;
                }
            }
            
        }
        if(u==destination){
            break;
        }
    }
    TDFS(destination);
}

void Print(){
    int i;
    if(TT==TD){
        printf("Distance = %d; Time = %d: ",D[destination],T[destination]);
        i=TD.size()-1;
        while(i>0){
            printf("%d -> ",TD[i]);
            i--;
        }
        printf("%d\n",destination);
    }else{
        printf("Distance = %d: ",D[destination]);
        i=TD.size()-1;;
        while(i>0){
            printf("%d -> ",TD[i]);
            i--;
        }
        printf("%d\n",destination);
        printf("Time = %d: ",T[destination]);
        i=TT.size()-1;;
        while(i>0){
            printf("%d -> ",TT[i]);
            i--;
        }
        printf("%d\n",destination);
    }
}

int main(){
    Init();
    DDijkstra();
    TDijkstra();
    Print();
    return 0;
}

运行结果

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值