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 -> v~1~ -> ... -> destination
Then in the next line print the fastest path with total time T:
Time = T: source -> w~1~ -> ... -> 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 -> u~1~ -> ... -> 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
给出一些streets的端点v1,v2,如果是one-way的,即单程,那么只能从v1到v2,如果不是单程的,也可以从v2到v1,找出最短的路(不唯一,那就找出其中时间最短的)和时间最短的(不唯一,就找出
经过地点最少的),如果说这两个路径一样,就只输出一次,按照题目给定的格式。
代码:
#include <stdio.h>
#include <string.h>
#define inf 0x3f3f3f3f
int m,n,source,destination,a,b,w,l,t;
int length[][],times[][],dis[],cost[],cost1[],num[],vis[],path1[],path2[];
void getpath1(int x) {
if(x != source) {
getpath1(path1[x]);
printf(" -> ");
}
printf("%d",x);
}
void getpath2(int x) {
if(x != source) {
getpath2(path2[x]);
printf(" -> ");
}
printf("%d",x);
}
int equals(int x) {
if(path1[x] != path2[x])return ;
else if(x == source)return ;
return equals(path1[x]);
}
int main() {
scanf("%d%d",&n,&m);
for(int i = ;i < n;i ++) {
for(int j = ;j < n;j ++) {
length[i][j] = times[i][j] = inf;
}
dis[i] = cost[i] = cost1[i] = inf;
path1[i] = path2[i] = -;
}
for(int i = ;i < m;i ++) {
scanf("%d%d%d%d%d",&a,&b,&w,&l,&t);
if(w) {
length[a][b] = l;
times[a][b] = t;
}
else {
length[a][b] = length[b][a] = l;
times[a][b] = times[b][a] = t;
}
}
scanf("%d%d",&source,&destination);
dis[source] = cost[source] = cost1[source] = ;
while() {
int t = -,mi = inf;
for(int i = ;i < n;i ++) {
if(!vis[i] && mi > dis[i]) {
mi = dis[i];
t = i;
}
}
if(t == -)break;
vis[t] = ;
for(int i = ;i < n;i ++) {
if(vis[i] || length[t][i] == inf)continue;
if(dis[i] > dis[t] + length[t][i]) {
path1[i] = t;
dis[i] = dis[t] + length[t][i];
cost1[i] = cost1[t] + times[t][i];
}
else if(dis[i] == dis[t] + length[t][i] && cost1[i] > cost1[t] + times[t][i]) {
cost1[i] = cost1[t] + times[t][i];
path1[i] = t;
}
}
}
memset(vis,,sizeof(vis));
while() {
int t = -,mi = inf;
for(int i = ;i < n;i ++) {
if(!vis[i] && mi > cost[i]) {
mi = cost[i];
t = i;
}
}
if(t == -)break;
vis[t] = ;
for(int i = ;i < n;i ++) {
if(vis[i] || times[t][i] == inf)continue;
if(cost[i] > cost[t] + times[t][i]) {
path2[i] = t;
cost[i] = cost[t] + times[t][i];
num[i] = num[t] + ;
}
else if(cost[i] == cost[t] + times[t][i] && num[i] > num[t] + ) {
num[i] = num[t] + ;
path2[i] = t;
}
}
}
printf("Distance = %d",dis[destination]);
if(!equals(destination)) {
printf(": ");
getpath1(destination);
printf("\n");
}
else {
printf("; ");
}
printf("Time = %d: ",cost[destination]);
getpath2(destination);
}
1111 Online Map (30)(30 分)的更多相关文章
-
1111 Online Map (30 分)
1111 Online Map (30 分) Input our current position and a destination, an online map can recommend sev ...
-
1111 Online Map (30 分)
1111. Online Map (30)Input our current position and a destination, an online map can recommend sever ...
-
【刷题-PAT】A1111 Online Map (30 分)
1111 Online Map (30 分) Input our current position and a destination, an online map can recommend sev ...
-
PAT 1111 Online Map[Dijkstra][dfs]
1111 Online Map(30 分) Input our current position and a destination, an online map can recommend seve ...
-
PAT甲级——1131 Subway Map (30 分)
可以转到我的CSDN查看同样的文章https://blog.csdn.net/weixin_44385565/article/details/89003683 1131 Subway Map (30 ...
-
PAT甲级——1111 Online Map (单源最短路经的Dijkstra算法、priority_queue的使用)
本文章同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90041078 1111 Online Map (30 分) ...
-
PAT甲级1111. Online Map
PAT甲级1111. Online Map 题意: 输入我们当前的位置和目的地,一个在线地图可以推荐几条路径.现在你的工作是向你的用户推荐两条路径:一条是最短的,另一条是最快的.确保任何请求存在路径. ...
-
etectMultiScale(gray, 1.2,3,CV_HAAR_SCALE_IMAGE,Size(30, 30))
# 函数原型detectMultiScale(gray, 1.2,3,CV_HAAR_SCALE_IMAGE,Size(30, 30)) # gray需要识别的图片 # 1.03:表示每次图像尺寸减小 ...
-
c# 时间格式处理,获取格式: 2014-04-12T12:30:30+08:00
C# 时间格式处理,获取格式: 2014-04-12T12:30:30+08:00 一.获取格式: 2014-04-12T12:30:30+08:00 方案一:(局限性,当不是当前时间时不能使用) ...
-
java.time.format.DateTimeParseException: Text &#39;2019-10-11 12:30:30&#39; could not be parsed at index 10
java.time.format.DateTimeParseException: Text '2019-10-11 12:30:30' could not be parsed at index 10 ...
随机推荐
-
不注册COM在Richedit中使OLE支持复制粘贴
正常情况下在Richedit中使用OLE,如果需要OLE支持复制粘贴,那么这个OLE对象必须是已经注册的COM对象. 注册COM很简单,关键问题在于注册时需要管理员权限,这样一来,如果希望APP做成绿 ...
-
SAP第一轮面试总结
1. 开始是一套面试题,可以选JAVA或C/C++两个语言的英文题.基础语法题,以指针为主. 2. 英文介绍,*发挥.问了以下几个问题: 离职的愿意,未来五年的计划,介不介意使用ABAP langu ...
-
CSS深入之label与input对齐!
我想很多人都会碰到label与input 对齐的问题. 这个东西本身不难,但是要做到与IE这个东西兼容确实有点头疼. 参考各大门户网站的前端源码. 得一方法,以记录之: html确实很简单: 帐号 输 ...
-
POJ1035——Spell checker(字符串处理)
Spell checker DescriptionYou, as a member of a development team for a new spell checking program, ar ...
-
java的抽象类
现实世界中,人们表征世界时,会把现实世界中的很多类具有相同特征的事物归为一个抽象类.比如水果是许多植物果实的总称,我们可以定义一个苹果类.定义一个西瓜类,可以实例化一个苹果对象,可以实例化一个西瓜对象 ...
-
mongoDB4--mongoDB的增删改查
MongoDb基本操作之增删改查我们知道传统关系型数据库的最常用操作就是"增加/删除/修改/查询",也就是传说中的CRUD(create/remove/updte/delete). ...
-
Gradle的一些技巧和遇到的问题
全局变量的使用 在多个module的情况下,不同module的build.gradle文件中有部分配置项类似,或者依赖的类库,有部分是相同的,在维护上不是很方便,这个时候就可以考虑统一配置.在项目根目 ...
-
echarts X轴显示不全 有省略
代码如下: xAxis: [ { type: 'category', data: result.weekListAndYear,//result.weekList, axisLabel:{ // in ...
-
【HDU - 4341】Gold miner(分组背包)
BUPT2017 wintertraining(15) #8B 题意 给出每个黄金的坐标.价值及耗时,同一方向的黄金只能依次取,求T时间内收获的最大值. 题解 同一方向,物品前缀和构成的组合,相当于是 ...
-
bouncing-balls-evil-circle
效果如下 代码目录 <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset=&q ...