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

知识点: Dijkstra算法; DFS算法

思路:

第一是求最短路径,有相同的则输出时间最短;利用Dijkstra求解,更新最短路时,如果最短路相同,则比较时间

 if(!visited[i]){
if(minl[i]>minD+G_l[minV][i]){
minl[i]=minD+G_l[minV][i];
mint[i]=mint[minV]+G_t[minV][i];
pre_shorest[i]=minV;
}else if(minl[i]==minD+G_l[minV][i]&&
mint[i]>mint[minV]+G_t[minV][i]){
mint[i]=mint[minV]+G_t[minV][i];
pre_shorest[i]=minV;
}
}

第二是求最快路,如果有相同的,输出节点最少的:用Dijkstra算法,设立容器pre来储存每个节点的优选前去节点;然后用DFS来遍历每条路径,选出最少节点的

 for(int i=;i<n;i++){
if(!visited[i]){
if(mint[i]>minD+G_t[minV][i]){
mint[i]=mint[minV]+G_t[minV][i];
pre_faster[i].clear();
pre_faster[i].push_back(minV);
}else if(mint[i]==minD+G_t[minV][i]){
pre_faster[i].push_back(minV);
}
}
}

最后,vector可以比较,相同的情况特殊处理

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 <string>
#include <vector>
using namespace std;
const int maxn = 550;
const int inf = 999999; int n,m;
int G_l[maxn][maxn];
int G_t[maxn][maxn];
int minl[maxn];
int mint[maxn];
int visited[maxn];
int pre_shorest[maxn];
vector<int> pre_faster[maxn];
vector<int> s_path;
vector<int> f_path;
vector<int> f_tmpp;
int minsize; int dijkstra_shorest(int start,int end){
fill(pre_shorest, pre_shorest+maxn, -1);
fill(minl,minl+maxn,inf);
fill(mint,mint+maxn,inf);
fill(visited,visited+maxn,0);
minl[start] = 0;
for(int i=0;i<n;i++){
int minV=-1, minD=inf;
for(int i=0;i<n;i++){
if(minl[i]<minD && !visited[i]){
minV=i;
minD=minl[i];
}
}
if(minV==-1) break;
visited[minV] = 1;
for(int i=0;i<n;i++){
if(!visited[i]){
if(minl[i]>minD+G_l[minV][i]){
minl[i]=minD+G_l[minV][i];
mint[i]=mint[minV]+G_t[minV][i];
pre_shorest[i]=minV;
}else if(minl[i]==minD+G_l[minV][i]&&
mint[i]>mint[minV]+G_t[minV][i]){
mint[i]=mint[minV]+G_t[minV][i];
pre_shorest[i]=minV;
}
}
}
}
int ptr = end;
while(ptr != -1){
//printf(" %d\n",ptr);
s_path.push_back(ptr);
ptr=pre_shorest[ptr];
}
return minl[end];
} void DFS(int v,int start){
f_tmpp.push_back(v);
if(v==start){
if(f_tmpp.size()<minsize){
f_path=f_tmpp;
minsize=f_tmpp.size();
}
f_tmpp.pop_back();
return;
}
for(int i=0;i<pre_faster[v].size();i++){
DFS(pre_faster[v][i], start);
}
f_tmpp.pop_back();
} int dijkstra_fastest(int start,int end){
fill(mint,mint+maxn,inf);
fill(visited,visited+maxn,0);
mint[start] = 0;
for(int i=0;i<n;i++){
int minV=-1, minD=inf;
for(int i=0;i<n;i++){
if(mint[i]<minD && !visited[i]){
minV=i;
minD=mint[i];
}
}
if(minV==-1) break;
//printf(". %d\n",minV);
visited[minV] = 1;
for(int i=0;i<n;i++){
if(!visited[i]){
if(mint[i]>minD+G_t[minV][i]){
mint[i]=mint[minV]+G_t[minV][i];
pre_faster[i].clear();
pre_faster[i].push_back(minV);
}else if(mint[i]==minD+G_t[minV][i]){
pre_faster[i].push_back(minV);
}
}
}
}
minsize = inf;
DFS(end,start);
for(int i=0;i<f_path.size();i++){
//printf("%d\n",f_path[i]);
}
return mint[end];
} int main(int argc, char *argv[]) {
fill(G_l[0],G_l[0]+maxn*maxn,inf);
fill(G_t[0],G_t[0]+maxn*maxn,inf); scanf("%d %d",&n,&m);
int v1,v2,oneway,len,tim;
for(int i=0;i<m;i++){
scanf("%d %d %d %d %d",&v1,&v2,&oneway,&len,&tim);
if(!oneway){
G_l[v1][v2]=len;
G_l[v2][v1]=len;
G_t[v1][v2]=tim;
G_t[v2][v1]=tim;
}else{
G_l[v1][v2]=len;
G_t[v1][v2]=tim;
}
}
scanf("%d %d",&v1,&v2); int D = dijkstra_shorest(v1,v2); int T = dijkstra_fastest(v1,v2); if(s_path==f_path){
printf("Distance = %d; Time = %d: ",D,T);
for(int i=s_path.size()-1;i>=0;i--){
printf("%d",s_path[i]);
if(i!=0) printf(" -> ");
}
}else{
printf("Distance = %d: ",D);
for(int i=s_path.size()-1;i>=0;i--){
printf("%d",s_path[i]);
if(i!=0) printf(" -> ");
}
printf("\nTime = %d: ",T);
for(int i=f_path.size()-1;i>=0;i--){
printf("%d",f_path[i]);
if(i!=0) printf(" -> ");
}
}
}

 

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

随机推荐

  1. 任意格式视频转MP4格式

    下载ffmpeg解压,提取ffmpeg.exe 在mmfpeg.exe目录下新建批处理,内容如下 @echo off title 正在转换,mp4转换完成自动关闭 ffmpeg -i %1 -y -q ...

  2. PC初始化

    @charset "utf-8"; /* CSS Document */ html{width:%;font-family: ;padding: ;} a{color:#;text ...

  3. python 数据类型 之 字典

    python 3.6.5字典的特性和定义定义:{'key_1':vlaue_1,'key_2':value_2}1.键与值用冒号 : 分开2.项与项 用 , 分开 特性1.可以存放多个值,可以不唯一, ...

  4. PAT 1038 统计同成绩学生(20)(代码)

    1038 统计同成绩学生(20)(20 分) 本题要求读入N名学生的成绩,将获得某一给定分数的学生人数输出. 输入格式: 输入在第1行给出不超过10^5^的正整数N,即学生总人数.随后1行给出N名学生 ...

  5. 继承 (js原型链)

    原型链是实现继承的主要方法.基本思想:利用原型让一个引用类型继承另一个引用类型的属性和方法. 1.构造函数.原型.实例的关系: 每个构造函数都有原型属性(Prototype),指向一个原型对象(函数创 ...

  6. iOS.Objective-C.Dependency.Graphing-v0.1

    当Project越来越复杂,模块间的依赖就会很复杂,不合理的依赖就出现:不必要的依赖,双向依赖等等. 在iOS Application Project中可以将依赖定义为:对某个头文件的import. ...

  7. 怎么隐藏服务器的IP地址?

    服务器一般很少会使用公网地址,直接放置在互联网上使用. 一般是设置成局域网的私网地址,并通过路由器的端口映射,发布在互联网:内部的NAT转换,相当于隐藏了路由器,外网访问并不知道具体服务器的IP地址. ...

  8. JianShu_failban2实现动态屏蔽的功能

    一,首先是服务安装 #vim /etc/yum.repos.d/Centos-Base.repo 在最新新增 [atrpms] name=Red Hat Enterprise Linux $relea ...

  9. 新手必看,史上最全的iOS开发教程集锦,没有之一!

    最近大火的iPhone XS Max和iPhone XS,不知道有没有同学已经下手了呢?一万三的价位确实让很多人望而却步啊.据说为了赢得中国的用户,专门出了双卡双待的,可想而知中国市场这块“肥肉”人人 ...

  10. 让UI设计师崩溃的瞬间,你经历过哪些?

    隔行如隔山,这句话人人耳熟能详,但其实隔行并不可怕,大家各谋其事,各尽其职,倒也互不打扰,真正可怕的是,是内行还要受外行指点江山,而最难的部分,便是那沟通.流畅的沟通,和声细语,是有如时雨之化者:无效 ...