PAT_A1111#Online Map
Source:
Description:
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), 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
V1andV2are the indices (from 0 to N−1) of the two ends of the street;one-wayis 1 if the street is one-way fromV1toV2, or 0 if not;lengthis the length of the street; andtimeis 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
Din 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
Keys:
Attention:
- 其实就是求两次最短路径,代码code一遍再copy一遍,考试的时候怎么快怎么来,优化代码反而容易出错
Code:
/*
Data: 2019-08-18 21:30:18
Problem: PAT_A1111#Online Map
AC: 42:06 题目大意:
给出当前位置和目的地,给出最短路径和最快路径
输入:
第一行给出,结点数N,边数M
接下来M行,v1,v2,单/双向,距离,时间
最后一行,起点,终点
输出:
最短距离,输出路径,不唯一则选择最快的
最短时间,输出路径,不唯一则选择经过结点最少的
*/
#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
const int M=1e3,INF=1e9;
int grap[M][M],cost[M][M],vis[M],d[M],c[M];
int n,m,v1,v2,one,optTime=INF,optLeth=INF;
vector<int> preL[M],preT[M],optL,pathL,optT,pathT; void DijskraL(int s)
{
fill(vis,vis+M,);
fill(d,d+M,INF);
d[s]=;
for(int i=; i<n; i++)
{
int u=-,Min=INF;
for(int j=; j<n; j++)
{
if(vis[j]== && d[j]<Min)
{
Min = d[j];
u = j;
}
}
if(u==-) return;
vis[u]=;
for(int v=; v<n; v++)
{
if(vis[v]== && grap[u][v]!=INF)
{
if(d[u]+grap[u][v] < d[v])
{
preL[v].clear();
preL[v].push_back(u);
d[v] = d[u]+grap[u][v];
}
else if(d[u]+grap[u][v]==d[v])
preL[v].push_back(u);
}
}
}
} void DFSL(int s)
{
if(s == v1)
{
pathL.push_back(v1);
int time=,len=pathL.size();
for(int i=; i<len; i++)
time += cost[pathL[i]][pathL[i-]];
if(time < optTime)
{
optTime = time;
optL = pathL;
}
pathL.pop_back();
return;
}
pathL.push_back(s);
for(int i=; i<preL[s].size(); i++)
DFSL(preL[s][i]);
pathL.pop_back();
} void DijskraT(int s)
{
fill(vis,vis+M,);
fill(c,c+M,INF);
c[s]=;
for(int i=; i<n; i++)
{
int u=-,Min=INF;
for(int j=; j<n; j++)
{
if(vis[j]== && c[j]<Min)
{
Min = c[j];
u = j;
}
}
if(u==-) return;
vis[u]=;
for(int v=; v<n; v++)
{
if(vis[v]== && cost[u][v]!=INF)
{
if(c[u]+cost[u][v] < c[v])
{
preT[v].clear();
preT[v].push_back(u);
c[v] = c[u] + cost[u][v];
}
else if(c[u]+cost[u][v]==c[v])
preT[v].push_back(u);
}
}
}
} void DFST(int s)
{
if(s == v1)
{
pathT.push_back(v1);
if(pathT.size() < optLeth)
{
optLeth = pathT.size();
optT = pathT;
}
pathT.pop_back();
return;
}
pathT.push_back(s);
for(int i=; i<preT[s].size(); i++)
DFST(preT[s][i]);
pathT.pop_back();
} int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("Test.txt", "r", stdin);
#endif // ONLINE_JUDGE fill(grap[],grap[]+M*M,INF);
fill(cost[],cost[]+M*M,INF); scanf("%d%d", &n,&m);
for(int i=; i<m; i++)
{
scanf("%d%d%d", &v1,&v2,&one);
scanf("%d%d", &grap[v1][v2],&cost[v1][v2]);
if(!one)
{
grap[v2][v1]=grap[v1][v2];
cost[v2][v1]=cost[v1][v2];
}
}
scanf("%d%d", &v1,&v2);
DijskraL(v1);
DFSL(v2);
DijskraT(v1);
DFST(v2);
if(optT == optL)
{
printf("Distance = %d; Time = %d: %d", d[v2],c[v2],optT[optT.size()-]);
for(int i=optT.size()-; i>=; i--)
printf(" -> %d", optT[i]);
}
else
{
printf("Distance = %d: %d", d[v2], optL[optL.size()-]);
for(int i=optL.size()-; i>=; i--)
printf(" -> %d", optL[i]);
printf("\nTime = %d: %d", c[v2], optT[optT.size()-]);
for(int i=optT.size()-; i>=; i--)
printf(" -> %d", optT[i]);
} return ;
}
PAT_A1111#Online Map的更多相关文章
- mapreduce中一个map多个输入路径
package duogemap; import java.io.IOException; import java.util.ArrayList; import java.util.List; imp ...
- .NET Core中间件的注册和管道的构建(3) ---- 使用Map/MapWhen扩展方法
.NET Core中间件的注册和管道的构建(3) ---- 使用Map/MapWhen扩展方法 0x00 为什么需要Map(MapWhen)扩展 如果业务逻辑比较简单的话,一条主管道就够了,确实用不到 ...
- Java基础Map接口+Collections工具类
1.Map中我们主要讲两个接口 HashMap 与 LinkedHashMap (1)其中LinkedHashMap是有序的 怎么存怎么取出来 我们讲一下Map的增删改查功能: /* * Ma ...
- Java基础Map接口+Collections
1.Map中我们主要讲两个接口 HashMap 与 LinkedHashMap (1)其中LinkedHashMap是有序的 怎么存怎么取出来 我们讲一下Map的增删改查功能: /* * Ma ...
- 多用多学之Java中的Set,List,Map
很长时间以来一直代码中用的比较多的数据列表主要是List,而且都是ArrayList,感觉有这个玩意就够了.ArrayList是用于实现动态数组的包装工具类,这样写代码的时候就可以拉进 ...
- Java版本:识别Json字符串并分隔成Map集合
前言: 最近又看了点Java的知识,于是想着把CYQ.Data V5迁移到Java版本. 过程发现坑很多,理论上看大部分很相似,实践上代码写起来发现大部分都要重新思考方案. 遇到的C#转Java的一些 ...
- MapReduce剖析笔记之八: Map输出数据的处理类MapOutputBuffer分析
在上一节我们分析了Child子进程启动,处理Map.Reduce任务的主要过程,但对于一些细节没有分析,这一节主要对MapOutputBuffer这个关键类进行分析. MapOutputBuffer顾 ...
- MapReduce剖析笔记之七:Child子进程处理Map和Reduce任务的主要流程
在上一节我们分析了TaskTracker如何对JobTracker分配过来的任务进行初始化,并创建各类JVM启动所需的信息,最终创建JVM的整个过程,本节我们继续来看,JVM启动后,执行的是Child ...
- MapReduce剖析笔记之五:Map与Reduce任务分配过程
在上一节分析了TaskTracker和JobTracker之间通过周期的心跳消息获取任务分配结果的过程.中间留了一个问题,就是任务到底是怎么分配的.任务的分配自然是由JobTracker做出来的,具体 ...
随机推荐
- EF--model is being created异常
使用EF的时候出现了下面的异常,我使用了TASK和saveChangeAsync()异步 The context cannot be used while the model is being cre ...
- Spring MVC-表单(Form)标签-单选按钮集合(RadioButtons)示例(转载实践)
以下内容翻译自:https://www.tutorialspoint.com/springmvc/springmvc_radiobuttons.htm 说明:示例基于Spring MVC 4.1.6. ...
- 关于重置IOS App请求推送的授权请求
项目要加入推送通知.測试完本地通知后.发现測不了远程通知.于是想重置授权请求. 下面是重置授权请求的方法: 方法一: 通用->还原->抹掉全部内容和设置 可是第一种方法非常费时,抹掉内容预 ...
- android:怎样用一天时间,写出“飞机大战”这种游戏!(无框架-SurfaceView绘制)
序言作为一个android开发人员,时常想开发一个小游戏娱乐一下大家,今天就说说,我是怎么样一天写出一个简单的"飞机大战"的. 体验地址:http://www.wandoujia. ...
- VCenter中嵌套openstack VM不能ping通外部网络问题解决的方法
问题描写叙述: 近期搭建了vCenter环境,并使用vCenter创建的VM搭建了一套openstack环境.在验证openstack的外网功能时.发现报文死活ping不通外网,抓包发现报文在vcen ...
- mac os lscpu 【转】
CPU Information on Linux and OS X This is small blog post detailing how to obtain information on you ...
- c++ string 解析ip
比如输入是192.168.80.12-15,解析成192.168.80.12.192.168.80.13.192.168.80.14.192.168.80.15. #include <iostr ...
- js form settimeout
<html><head><meta charset="utf8"><script type="text/javascript&q ...
- bzoj 2599(点分治)
2599: [IOI2011]Race Time Limit: 70 Sec Memory Limit: 128 MBSubmit: 3642 Solved: 1081[Submit][Statu ...
- Java中的常用类有哪些
1NumberFormat 2DecimalFormat 3BigDecimal 4Math 5Random 6DateFormat 7SimpleDateFormat 8Calendar 9Date ...