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. had been doing 和had been done有什么差别

    had been doing 和had been done有什么差别 浏览 37114 次 1个回答 最佳答案 21Doreen 来自科学教育类芝麻团 推荐于2017-10-15   1.首先要区分h ...

  2. 获取Access数据里所有表的名称和表的字段

    -------------//获取Access数据库表名        public void GetTableName()        {                string connSt ...

  3. 为什么要使用XHTML?

    XHTML 是什么? XHTML 指可扩展超文本标签语言(EXtensible HyperText Markup Language). XHTML 的目标是取代 HTML. XHTML 与 HTML ...

  4. 乐字节-Java8核心特性实战之函数式接口

    什么时候可以使用Lambda?通常Lambda表达式是用在函数式接口上使用的.从Java8开始引入了函数式接口,其说明比较简单:函数式接口(Functional Interface)就是一个有且仅有一 ...

  5. Linux 本命令 基本上用到的命令-自己留着用

    1:在某个目录下查找文件: find /data -name '*srm*' 2:监测文件流: tail –f  /data/log.xml 3:   删除文件: rm –f /data/log.xm ...

  6. Unity相机平滑跟随

    简介 unity中经常会用到固定视角的相机跟随,然后百度发现大家都是自己写的,然后偶也写咯一个,分享一下 PS: 由于刚学C#不久,才发现delegate这个东东,也不知道对性能影响大不大,但是看MS ...

  7. [Windows Server 2003] 安装PHP+MySQL方法

    ★ 欢迎来到[护卫神·V课堂],网站地址:http://v.huweishen.com★ 护卫神·V课堂 是护卫神旗下专业提供服务器教学视频的网站,每周更新视频.★ 本节我们将带领大家:PHP+MyS ...

  8. [Windows Server 2003] IIS自带FTP安装及配置方法

    ★ 欢迎来到[护卫神·V课堂],网站地址:http://v.huweishen.com★ 护卫神·V课堂 是护卫神旗下专业提供服务器教学视频的网站,每周更新视频.★ 本节我们将带领大家:IIS6.0自 ...

  9. 三角形状的点阵模糊效果iOS源码

    源码FFAngularPointilism,FFAngularPointilism能够将UIImageView像添加滤波器一样生成三角形状的点阵模糊效果.可以通过动画方式来模糊,也可以立刻模糊.另外并 ...

  10. shell编程之grep命令的使用

    大家在学习正则表达式之前,首先要明确一点,并把它牢牢记在心里,那就是: 在linux中,通配符是由shell解释的,而正则表达式则是由命令解释的,不要把二者搞混了.切记!!! 通常有三种文本处理工具/ ...