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做出来的,具体 ...
随机推荐
- jq 轮播图 上下自动滚动
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- MySql 同一个列中的内容进行批量改动
问题重现: MySql 数据库中,一给列的内容中包含 ".wmv" 须要将 "." 后的wmv格式 换为"flv" 解决的方法 up ...
- zoj 1648 Circuit Board
题目:意思就是推断给定的几条线段是否有相交的. 方法:模版吧,有空在来细细学习. 代码: #include <iostream> #include <cstdio> using ...
- CSS制作响应式正方形及其应用
CSS制作响应式正方形?什么鬼?干嘛用的?干嘛用的没人有每人的需求,本人也正好是由于公司正在做的业务须要,想做个照片展示的功能(当然得符合响应式了).而这个照片展示必须符合下面几点功能:1.用三张图片 ...
- git 的安装和使用
安装Git 下载并安装 mysysgit 下载并安装 git windows版本号 配置Git 设置你的名字和邮箱 git config --global user.name "xxxx&q ...
- 浅谈PHP数据结构之单链表
什么是链表?(依据百度词条查询而得) 链表是一种物理存储单元上非连续.非顺序的存储结构,数据元素的逻辑顺序是通过链表中的指针链接次序实现的.链表由一系列结点(链表中每个元素称为结点)组成,结点能够在执 ...
- 在ubuntu中安装photoshop cs6
对于很多专业的PS高手来说,真心难以找到顺手的且可以完美替代PS的软件,所以我们这里的解决办法就是用wine来安装. 虽然网上有很多的wine安装ps的方法,但是在使用过程往住会发生莫名其妙的崩溃,体 ...
- poj 3662 Telephone Lines(最短路+二分)
Telephone Lines Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6973 Accepted: 2554 D ...
- B5248 [2018多省省队联测]一双木棋 状压dp
这个题当时划水,得了二十分,现在来整一整. 这个题用状压来压缩边界线,然后通过记忆化搜索进行dp.我们可以观察到,其实每次转移,就是把一个1向左移一位.最后的状态设为0. 这其中还要有一个变量来记录谁 ...
- bzoj 1093 [ ZJOI 2007 ] 最大半连通子图 —— 拓扑+DP
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1093 先缩点,然后就是找最长链,DP一下即可: 注意缩点后的重边!会导致重复计算答案. 代码 ...