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<iostream>
#include<vector>
#include<stack>
using namespace std;
const int maxium=99999999999;
int n, m;
vector<int> pre1(505, -1), pre2(505, -1), visited(505, 0), _time(505,0);
vector<int> cnt(505, maxium);
struct road{
int end;
int len;
int Time;
road(int e, int l, int t):end(e), len(l), Time(t){
}
};
int findmin(vector<int> &minset){
int min=maxium, temp=-1;
for(int i=0; i<n; i++)
if(minset[i]<=min&&visited[i]==0){
temp=i;
min=minset[i];
}
visited[temp]=1;
return temp;
}
int main(){
vector<int> mint(505,maxium), mind(505, maxium);
scanf("%d %d",&n, &m);
int s, e, l, t, o;
vector<vector<road>> map(n);
for(int i=0; i<m; i++){
scanf("%d %d %d %d %d", &s, &e, &o, &l, &t);
road r(e, l, t);
map[s].push_back(r);
if(o==0){
road r1(s, l, t);
map[e].push_back(r1);
}
}
int b, d;
scanf("%d %d",&b, &d);
mind[b]=0;
int num=n;
while(num--){
int t=findmin(mind);
for(int i=0; i<map[t].size(); i++){
road temp=map[t][i];
if(visited[temp.end]==1) continue;
if(mind[temp.end]>mind[t]+temp.len){
mind[temp.end]=mind[t]+temp.len;
pre1[temp.end]=t;
_time[temp.end]=_time[t]+temp.Time;
}else if(mind[temp.end]==mind[t]+temp.len&&_time[t]+temp.Time<_time[temp.end]){
_time[temp.end]=_time[t]+temp.Time;
pre1[temp.end]=t;
}
}
}
fill(visited.begin(), visited.end(), 0);
mint[b]=0;
cnt[b]=0;
num=n;
while(num--){
int t=findmin(mint);
for(int i=0; i<map[t].size(); i++){
road temp=map[t][i];
if(visited[temp.end]==1) continue;
if(mint[temp.end]>mint[t]+temp.Time){
mint[temp.end]=mint[t]+temp.Time;
pre2[temp.end]=t;
cnt[temp.end]=cnt[t]+1;
}else if(mint[temp.end]==mint[t]+temp.Time&&cnt[temp.end]>cnt[t]+1){
pre2[temp.end]=t;
cnt[temp.end]=cnt[t]+1;
}
}
}
vector<int> ans1, ans2;
int temp=e;
stack<int> st;
while(temp!=-1){
st.push(temp);
temp=pre1[temp];
}
while(st.size()!=0){
ans1.push_back(st.top());
st.pop();
}
temp=e;
while(temp!=-1){
st.push(temp);
temp=pre2[temp];
}
while(st.size()!=0){
ans2.push_back(st.top());
st.pop();
}
if(ans1!=ans2){
printf("Distance = %d: ", mind[e]);
for(int i=0; i<ans1.size(); i++) printf("%d%s", ans1[i], i!=(ans1.size()-1)?" -> ":"\n");
printf("Time = %d: ", mint[e]);
for(int i=0; i<ans2.size(); i++) printf("%d%s", ans2[i], i!=(ans2.size()-1)?" -> ":"\n"); }else{
printf("Distance = %d; Time = %d: ", mind[e], mint[e]);
for(int i=0; i<ans1.size(); i++) printf("%d%s", ans1[i], i!=(ans1.size()-1)?" -> ":"\n");
}
return 0;
}

PAT 1111 Online Map的更多相关文章

  1. PAT 1111 Online Map[Dijkstra][dfs]

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

  2. PAT甲级1111. Online Map

    PAT甲级1111. Online Map 题意: 输入我们当前的位置和目的地,一个在线地图可以推荐几条路径.现在你的工作是向你的用户推荐两条路径:一条是最短的,另一条是最快的.确保任何请求存在路径. ...

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

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

  4. 1111 Online Map (30 分)

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

  5. 1111 Online Map (30 分)

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

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

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

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

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

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

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

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

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

随机推荐

  1. Linux gadget驱动分析3------复合设备驱动

    windows上面对usb复合设备的识别需要下面条件. “ 如果设备满足下列要求,则总线驱动程序还会报告 USB\COMPOSITE 的兼容标识符: 设备描述符的设备类字段 (bDeviceClass ...

  2. ios集合

    Foundation framework中用于收集cocoa对象(NSObject对象)的三种集合分别是: NSArray 用于对象有序集合(数组) NSSet 用于对象无序集合 (集合) NSDic ...

  3. ZOJ2334 Monkey King 左偏树

    ZOJ2334 用左偏树实现优先队列最大的好处就是两个队列合并可以在Logn时间内完成 用来维护优先队列森林非常好用. 左偏树代码的核心也是两棵树的合并! 代码有些细节需要注意. #include&l ...

  4. bzoj4821

    线段树 这题真是无聊 把式子拆开,然后可知维护xi,yi,xi^2,xi*yi,重点在于标记下传,当我们进行2号操作时,直接累加进答案和标记即可,进行3号操作时,update时先把自己这层赋值成要改变 ...

  5. [Swift通天遁地]四、网络和线程-(10)处理图片:压缩、缩放、圆角、CoreImage滤镜、缓存

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  6. [Swift通天遁地]七、数据与安全-(16)检测Apple设备是否越狱

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  7. Appium + python - weixin公众号操作

    from appium import webdriverfrom time import sleep desired_caps = { "platformName":"A ...

  8. Unity项目 - 捡苹果 Apple Picker

    项目展示 Github项目地址:Apple Picker 涉及知识 正投视图 3D场景内树与苹果的图层 记录最高分到本地 准备工作 模型制作: 基本模型创建 树叶:sphere 拉伸为椭圆形,绿色材质 ...

  9. 个人微信二次开发API接口

    通过这个API接口可以做什么? 通过我们提供的API接口您可以开发: 工作手机(如:X创,X码,XX管家等) 微信群讲课软件(如:讲课X师,一起X堂等) 微信社群管理软件(如:小X管家,微X助手等) ...

  10. php从数据库读取中文显示问号??的解决办法

    出错原因:1.数据库编码格式不对 2.PHP编码格式不对 3.浏览器编码格式不对 上面三者编码格式不统一,就会出现问题 数据库读取的时候在mysqli_connect()之后要设置连接字符编码mysq ...