1111 Online Map (30 分)

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

分析:Dijkstra模板题,套2次就行了。

 /**
 * Copyright(c)
 * All rights reserved.
 * Author : Mered1th
 * Date : 2019-02-26-22.31.18
 * Description : A1111
 */
 #include<cstdio>
 #include<cstring>
 #include<iostream>
 #include<cmath>
 #include<algorithm>
 #include<string>
 #include<unordered_set>
 #include<map>
 #include<vector>
 #include<set>
 using namespace std;
 ;
 ;
 bool vis[maxn];
 int G[maxn][maxn],d[maxn],t[maxn],T[maxn][maxn];
 int n,m,st,ed;
 int pred[maxn],pret[maxn],mininter[maxn];
 vector<int> shortPath,fastPath;
 void dDijkstra(int s){
     fill(d,d+maxn,INF);
     fill(t,t+maxn,INF);
     d[s]=;
     t[s]=;
     memset(vis,,sizeof(vis));
     ;i<n;i++) pred[i]=i;
     ;i<n;i++){
         ,MIN=INF;
         ;j<n;j++){
             if(vis[j]==false && d[j]<MIN){
                 u=j;
                 MIN=d[j];
             }
         }
         ) return;
         vis[u]=true;
         ;v<n;v++){
             if(vis[v]==false && G[u][v]!=INF){
                 if(G[u][v]+d[u]<d[v]){
                     d[v]=d[u]+G[u][v];
                     t[v]=t[u]+T[u][v];
                     pred[v]=u;
                 }
                 else if(G[u][v]+d[u]==d[v]){
                     if(T[u][v]+t[u]<t[v]){
                         pred[v]=u;
                         t[v]=T[u][v]+t[u];
                     }
                 }
             }
         }
     }
     for(int i=ed;i!=st;i=pred[i]){
         shortPath.push_back(i);
     }
     shortPath.push_back(st);
 }

 void tDijkstra(int s){
     fill(t,t+maxn,INF);
     t[s]=;
     fill(mininter,mininter+maxn,);
     memset(vis,,sizeof(vis));
     ;i<n;i++) pret[i]=i;
     ;i<n;i++){
         ,MIN=INF;
         ;j<n;j++){
             if(vis[j]==false && t[j]<MIN){
                 u=j;
                 MIN=t[j];
             }
         }
         ) return;
         vis[u]=true;
         ;v<n;v++){
             if(vis[v]==false && T[u][v]!=INF){
                 if(T[u][v]+t[u]<t[v]){
                     t[v]=t[u]+T[u][v];
                     pret[v]=u;
                     mininter[v]=mininter[u]+;
                 }
                 else if(T[u][v]+t[u]==t[v]){
                     ){
                         pret[v]=u;
                         mininter[v]=mininter[u]+;
                     }
                 }
             }
         }
     }
     for(int i=ed;i!=st;i=pret[i]){
         fastPath.push_back(i);
     }
     fastPath.push_back(st);
 }

 int main(){
 #ifdef ONLINE_JUDGE
 #else
     freopen("1.txt", "r", stdin);
 #endif
     int u,v,o;
     cin>>n>>m;
     fill(G[],G[]+maxn*maxn,INF);
     fill(T[],T[]+maxn*maxn,INF);
     ;i<m;i++){
         cin>>u>>v>>o;
         cin>>G[u][v]>>T[u][v];
         ){
             G[v][u]=G[u][v];
             T[v][u]=T[u][v];
         }
     }
     cin>>st>>ed;
     dDijkstra(st);
     tDijkstra(st);
     if(shortPath!=fastPath){
         printf("Distance = %d: ",d[ed]);
         ;i>=;i--){
             printf("%d",shortPath[i]);
             ) printf(" -> ");
             else printf("\n");
         }
         printf("Time = %d: ",t[ed]);
         ;i>=;i--){
             printf("%d",fastPath[i]);
             ) printf(" -> ");
             else printf("\n");
         }
     }
     else{
         printf("Distance = %d; Time = %d: ",d[ed],t[ed]);
         ;i>=;i--){
             printf("%d",fastPath[i]);
             ) printf(" -> ");
             else printf("\n");
         }
     }
     ;
 }

1111 Online Map (30 分)的更多相关文章

  1. 【PAT甲级】1111 Online Map (30分)(dijkstra+路径记录)

    题意: 输入两个正整数N和M(N<=500,M<=N^2),分别代表点数和边数.接着输入M行每行包括一条边的两个结点(0~N-1),这条路的长度和通过这条路所需要的时间.接着输入两个整数表 ...

  2. 1111 Online Map (30)(30 分)

    Input our current position and a destination, an online map can recommend several paths. Now your jo ...

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

    预处理出最短路再进行暴力dfs求答案会比较好.直接dfs效率太低. #include<cstdio> #include<cstring> #include<cmath&g ...

  4. 1111. Online Map (30)

    Input our current position and a destination, an online map can recommend several paths. Now your jo ...

  5. PAT Advanced 1111 Online Map (30) [Dijkstra算法 + DFS]

    题目 Input our current position and a destination, an online map can recommend several paths. Now your ...

  6. PAT甲题题解-1111. Online Map (30)-PAT甲级真题(模板题,两次Dijkstra,同时记下最短路径)

    题意:给了图,以及s和t,让你求s到t花费的最短路程.最短时间,以及输出对应的路径.   对于最短路程,如果路程一样,输出时间最少的. 对于最短时间,如果时间一样,输出节点数最少的.   如果最短路程 ...

  7. PAT-1111 Online Map (30分) 最短路+dfs

    明天就要考PAT,为了应付期末已经好久没有刷题了啊啊啊啊,今天开了一道最短路,状态不是很好 1.没有读清题目要求,或者说没有读完题目,明天一定要注意 2.vis初始化的时候从1初始化到n,应该从0开始 ...

  8. PAT甲级——1111 Online Map (单源最短路经的Dijkstra算法、priority_queue的使用)

    本文章同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90041078   1111 Online Map (30 分) ...

  9. 1111 Online Map (30 分)

    1111. Online Map (30)Input our current position and a destination, an online map can recommend sever ...

随机推荐

  1. Excel根据人名匹配得到编号

    操作步骤:输入公式 =IF(COUNTIF($E$2:$E2,$E2)>COUNTIF($B:$B,$E2),"",INDEX(C:C,SMALL(IF($B$1:$B$10 ...

  2. 2018-2019-2 网络对抗技术 20165202 Exp2 后门原理与实践

    博客目录 一.基础问题回答 二.实验准备:后门软件 1.Windows获得Linux Shell 2.Linux获得Windows Shell 3.使用nc传输数据 4.使用ncat实现文件传输 三. ...

  3. JSONField解决序列化与反序列化字段匹配问题

    需求:调用第三方数据,数据格式为Json,并提供一个接口将获取的第三方数据给本公司其他部门调用. 处理流程:第三方Json--反序列化实体--保存到本地数据库--查询数据--序列化Json数据供本公司 ...

  4. Android中Parcel的分析以及使用

    简单点来说:Parcel就是一个存放读取数据的容器, Android系统中的binder进程间通信(IPC)就使用了Parcel类来进行客户端与服务端数据的交互,而且AIDL的数据也是通过Parcel ...

  5. 把腾讯云的ubuntu16.04升级到18.04

    腾讯云买的服务器也没怎么弄,正好重装一下玩乐了. 1. 重装系统,在腾讯云里先停机,然后重装系统,目前最高是ubuntu16.04.为什么选择Ubuntu?因为,因为习惯吧,之前学习laravel就是 ...

  6. QModelIndex 与 QStandardItem互转

    1. QModelIndex 转换成QStandardItem QStandardItem * item=QStandardItemModel::​itemFromIndex(const QModel ...

  7. LeetCode刷题记录(python3)

    由于之前对算法题接触不多,因此暂时只做easy和medium难度的题. 看完了<算法(第四版)>后重新开始刷LeetCode了,这次决定按topic来刷题,有一个大致的方向.有些题不止包含 ...

  8. FZU 1202

    http://acm.fzu.edu.cn/problem.php?pid=1202 二分图最大匹配,问哪些边是必要的,O(n^3)的方法 删边的时候把连接关系也要删掉,如果在此基础上无法找到增广路, ...

  9. 当前目录如何打开cmd

    前言 有时候需要在当前文件夹打开cmd控制台,直接从开始打开cmd进入当前目录比较麻烦,可以直接进入. 方法 按住键盘上Shift键的同时,在要操作的文件夹(DOS年代称为目录)上点击鼠标右键,在弹出 ...

  10. BZOJ2460,LG4570 [BJWC2011]元素

    题意 相传,在远古时期,位于西方大陆的 Magic Land 上,人们已经掌握了用魔法矿石炼制法杖的技术.那时人们就认识到,一个法杖的法力取决于使用的矿石. 一般地,矿石越多则法力越强,但物极必反:有 ...