PAT(Advanced Level)1111. Online Map (30)

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

       pat的有一道最短路的问题,这次牵涉到回溯路径以及多重比较。如果距离相同则输出用时最短的,用时最短的则输出经过节点数目最少的,如果根据距离和时间推出的最短路是相同的话,就只输出一条,我用的Dijkstra的变形判断,就是标记数组开的很多。的首届天梯赛的初赛试题,比赛的时候没有得全分,回来做出来了,还好我们进了决赛并最终得了个三等奖,今年二战天梯华东赛区进了决赛,就要去杭州走一波,争取有个好成绩。

#include <iostream>
#include <cstdio>
#include <cstring>
#define inf 0x3f3f3f3f
using namespace std;
int timei[505][505],lengthi[505][505];//一个时间图,一个距离图
int v1,v2,v3,v4,v5;
int v[505];
int patht[505],pathl[505];//一个距离最短,一个用时最短用于回溯路径
int anst[505],ansl[505],lt=0,ll=0;//保留最终结果的路径
int cntl[505];
int le,ti;
int n,m,s,e;
void bfstime();
void bfslength();
void huisutime(int z);
void huisulength(int z);
int main()
{
    cin>>n>>m;
    while(m--){
        cin>>v1>>v2>>v3>>v4>>v5;
        if(v3){
            timei[v1][v2] = v5;
            lengthi[v1][v2] = v4;
        }
        else {
            timei[v1][v2] = v5;
            timei[v2][v1] = v5;
            lengthi[v1][v2] = v4;
            lengthi[v2][v1] = v4;
        }
    }
    cin>>s>>e;
    if(s==e){
        printf("Distance = 0; Time = 0: %d",e);
        return 0;
    }
    bfstime();
    memset(v,0,sizeof(v));
    bfslength();
    huisulength(e);
    huisutime(e);
    int f = 0;
    if(ll!=lt)f=1;
    for(int i = 0;i < ll;i++){
        if(anst[i]!=ansl[i]){
            f=1;
            break;
        }
    }
    if(f){
        printf("Distance = %d: ",le);
        for(int i = 0;i < ll;i++){
            if(!i)printf("%d",ansl[i]);
            else printf(" -> %d",ansl[i]);
        }
        printf(" -> %d\n",e);
        printf("Time = %d: ",ti);
        for(int i = 0;i < lt;i++){
            if(!i)printf("%d",anst[i]);
            else printf(" -> %d",anst[i]);
        }
        printf(" -> %d\n",e);
    }
    else {
        printf("Distance = %d; Time = %d: ",le,ti);
        for(int i = 0;i < lt;i++){
            if(!i)printf("%d",anst[i]);
            else printf(" -> %d",anst[i]);
        }
        printf(" -> %d",e);
    }
    return 0;
}
void bfstime(){
  int d[505];
  for(int i = 0;i < n;i++)
    d[i] = cntl[i] = inf;
  d[s] = 0;
  cntl[s] = 0;
  for(;;){
    int minn = inf,temp,f = 0;
    for(int i = 0;i < n;i++){
        if(!v[i]&&d[i]<minn){
            f = 1;
            temp = i;
            minn = d[i];
        }
    }
    if(!f)break;
    v[temp] = 1;
    for(int i = 0;i < n;i++){
        if(!v[i]&&timei[temp][i]){
            if(timei[temp][i]+d[temp]<d[i]){
                d[i] = d[temp]+timei[temp][i];
                cntl[i] = cntl[temp]+1;
                patht[i] = temp;
            }
            else if(timei[temp][i]+d[temp]==d[i]){
                if(cntl[i]>cntl[temp]+1){
                    patht[i] = temp;
                    cntl[i] = cntl[temp]+1;
                }
            }
        }
    }
  }
  ti = d[e];
}
void bfslength(){
  int d[505],tm[505];
  for(int i = 0;i < n;i++)
    d[i] = tm[i]=inf;
  d[s] = 0;
  tm[s] = 0;
  for(;;){
    int minn = inf,temp,f = 0;
    for(int i = 0;i < n;i++){
        if(!v[i]&&d[i]<minn){
            f = 1;
            temp = i;
            minn = d[i];
        }
    }
    if(!f)break;
    v[temp] = 1;
    for(int i = 0;i < n;i++){
        if(!v[i]&&lengthi[temp][i]){
            if(lengthi[temp][i]+d[temp]<d[i]){
                d[i] = d[temp]+lengthi[temp][i];
                tm[i] = timei[temp][i]+tm[temp];
                pathl[i] = temp;
            }
            else if(lengthi[temp][i]+d[temp]==d[i]){
                if(tm[temp]+timei[temp][i]<tm[i]){
                    tm[i] = timei[temp][i]+tm[temp];
                    pathl[i] = temp;
                }
            }
        }
    }
  }
  le = d[e];
}
void huisulength(int z){
  if(pathl[z]!=s)
    huisulength(pathl[z]);
  ansl[ll++]=pathl[z];
}
void huisutime(int z){
  if(patht[z]!=s)
    huisutime(patht[z]);
  anst[lt++] = patht[z];
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值