PAT 1111 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

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
const int maxn=510;
const int INF=1000000000;

int Gd[maxn][maxn];
int Gt[maxn][maxn];
int d[maxn];
int t[maxn];
bool visit[maxn];
vector<int> pred[maxn];
vector<int> pret[maxn];
void dijkstrad(int st,int n){
    fill(d,d+n,INF);
    fill(visit,visit+n,false);
    d[st]=0;
    for(int i=0;i<n;i++){
        int m=-1,min=INF;
        for(int j=0;j<n;j++){
            if(!visit[j]&&min>d[j]){
                m=j;
                min=d[j];
            }
        }
        if(m==-1) return;
        visit[m]=true;
        for(int j=0;j<n;j++){
            if(!visit[j]&&Gd[m][j]!=INF){
                if(d[j]>d[m]+Gd[m][j]){
                    d[j]=d[m]+Gd[m][j];
                    pred[j].clear();
                    pred[j].push_back(m);
                }
                else if(d[j]==d[m]+Gd[m][j]){
                    pred[j].push_back(m);
                }
                
            }
        }
    }
}
void dijkstrat(int st,int n){
    fill(t,t+n,INF);
    fill(visit,visit+n,false);
    t[st]=0;
    for(int i=0;i<n;i++){
        int m=-1,min=INF;
        for(int j=0;j<n;j++){
            if(!visit[j]&&min>t[j]){
                m=j;
                min=t[j];
            }
        }
        if(m==-1) return;
        visit[m]=true;
        for(int j=0;j<n;j++){
            if(!visit[j]&&Gt[m][j]!=INF){
                if(t[j]>t[m]+Gt[m][j]){
                    t[j]=t[m]+Gt[m][j];
                    pret[j].clear();
                    pret[j].push_back(m);
                }
                else if(t[j]==t[m]+Gt[m][j]){
                    pret[j].push_back(m);
                }
                
            }
        }
    }
}
int source,destination;
vector<int> pathd,tempd;
int cald=INF;
void dfsd(int end,int sum){
    if(end==source){
        tempd.push_back(end);
        if(sum<cald){
            pathd=tempd;
            cald=sum;
        }
        tempd.pop_back();
    }
    tempd.push_back(end);
    for(int i=0;i<pred[end].size();i++){
        dfsd(pred[end][i],sum+Gt[pred[end][i]][end]);
    }
    tempd.pop_back();
}
vector<int> patht,tempt;
int calt=INF;
void dfst(int end){
    if(end==source){
        tempt.push_back(end);
        if(tempt.size()<calt){
            patht=tempt;
            calt=tempt.size();
        }
        tempt.pop_back();
    }
    tempt.push_back(end);
    for(int i=0;i<pret[end].size();i++){
        dfst(pret[end][i]);
    }
    tempt.pop_back();
}
int main(){
    int n,m;
    scanf("%d %d",&n,&m);
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            Gd[i][j]=INF;
            Gt[i][j]=INF;
        }
    }
    for(int i=0;i<m;i++){
        int v1,v2,tag,length,time;
        scanf("%d %d %d %d %d",&v1,&v2,&tag,&length,&time);
        if(tag==1){
            Gd[v1][v2]=length;
            Gt[v1][v2]=time;
        }
        else{
            Gd[v1][v2]=length;
            Gt[v1][v2]=time;
             Gd[v2][v1]=length;
            Gt[v2][v1]=time;
        }
    }
    scanf("%d %d",&source,&destination);
    dijkstrad(source,n);
    dijkstrat(source,n);
    dfsd(destination,0);
    dfst(destination);
    if(pathd==patht){
        printf("Distance = %d; Time = %d: ",d[destination],t[destination]);
        for(int i=pathd.size()-1;i>=0;i--){
            printf("%d",pathd[i]);
            if(i!=0) printf(" -> ");
        }
        printf("\n");
    }
    else{
        printf("Distance = %d: ",d[destination]);
            for(int i=pathd.size()-1;i>=0;i--){
            printf("%d",pathd[i]);
            if(i!=0) printf(" -> ");
        }
        printf("\n");
        printf("Time = %d: ",t[destination]);
         for(int i=patht.size()-1;i>=0;i--){
            printf("%d",patht[i]);
            if(i!=0) printf(" -> ");
        }
        printf("\n");
}
}

读完题干之后可以知道这道题让求的是单源最短路径,所以要用dijkstra算法来做,然后题目与模板不同的是让求两个指标下的最短路径,然后如果相同的话一个求第二指标最短一个求节点数最短,都是比较简单的。
我采用了复用两个dijkstra和dfs来做,需要注意的是不要写错了变量名,因为这样写会造成变量名很多。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值