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. param size: The requested size, in points.

    param size: The requested size, in points. 字幕宽度的自适应 . fontScale c++ - OpenCV find the text Scale fro ...

  2. YOCTO

    Yocto ,是一个开源社区它通过提供模版.工具和方法帮助开发者创建基于linux内核的定制系统,支持ARM, PPC, MIPS, x86 (32 & 64 bit)硬件体系架构.

  3. protected (C# Reference)

    https://msdn.microsoft.com/en-us/library/bcd5672a.aspx The protected keyword is a member access modi ...

  4. Linux运维人员-服务器组成硬件基础

    第1章 1.1关于运维人员 1.1.1 运维的职责 数据不能丢 网站7*24小时运行 保证用户体验(用户体验要好) 1.1.2 运维原则 简单.易用.高效  === 简单.粗暴 1.2 服务器 1.2 ...

  5. codeforce 杀题计划

    先尽量做Div 1 A B 想做难题时做C 全天学竞赛时每天至少两道Div2 (算法数据结构没学的先过,题面很长的......也先过  我的英语啊...)

  6. 牛客小白月赛15 C 表单 ( map 使用)

    链接:https://ac.nowcoder.com/acm/contest/917/C来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言52428 ...

  7. SpringBoot2.0整合SpringSecurity实现自定义表单登录

    我们知道企业级权限框架一般有Shiro,Shiro虽然强大,但是却不属于Spring成员之一,接下来我们说说SpringSecurity这款强大的安全框架.费话不多说,直接上干货. pom文件引入以下 ...

  8. ASP.NET MVC5 之路由器

    这篇博客介绍的很详细 http://www.cnblogs.com/yaozhenfa/p/asp_net_mvc_route_1.html

  9. JAVA小记(一)

    java中向上转型.向下转型.内部类中所需注意的问题: 向上转型与向下转型: 举个例子:有2个类,Father是父类,Son类继承自Father. Father f1 = new Son();   / ...

  10. 【USACO2002 Feb】奶牛自行车队

    [USACO2002 Feb]奶牛自行车队 Time Limit: 1000 ms Memory Limit: 131072 KBytes Description N 头奶牛组队参加自行车赛.车队在比 ...