【PAT甲级】1111 Online Map (30分)(dijkstra+路径记录)
题意:
输入两个正整数N和M(N<=500,M<=N^2),分别代表点数和边数。接着输入M行每行包括一条边的两个结点(0~N-1),这条路的长度和通过这条路所需要的时间。接着输入两个整数表示起点和终点,输出路径最短的路,如果多条路路径最短输出其中通过时间最短的路,以及通过时间最短的路,如果通过时间最短的路有多条输出其中经过点个数最短的路,如果两条路相同,则合并输出。详见样例。
trick:
用邻接表存图,优先队列优化版本的dijkstra做,最后一个测试点会段错误,将数组开到25000以上后变为答案错误,实际上看似没有访问500~25000这一段数组,原因暂时不明,对于点少边多的图,可能用朴素写法效率较高,并且邻接表存图并不会占用太大空间,完全可以实现。
AAAAAccepted code:
#define HAVE_STRUCT_TIMESPEC
#include<bits/stdc++.h>
using namespace std;
int dis[],tim[];
int e[][],w[][];
int pre[],timpre[],weight[],num[];
bool vis[];
vector<int>dispath,timpath;
int s,t;
void dfsdispath(int v){
dispath.push_back(v);
if(v==s)
return ;
dfsdispath(pre[v]);
}
void dfstimpath(int v){
timpath.push_back(v);
if(v==s)
return ;
dfstimpath(timpre[v]);
}
int main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
for(int i=;i<;++i)
dis[i]=1e9+,tim[i]=1e9+,weight[i]=1e9+;
for(int i=;i<;++i)
for(int j=;j<;++j)
e[i][j]=w[i][j]=1e9+;
int n,m;
cin>>n>>m;
int u,v,flag,x,y;
for(int i=;i<m;++i){
cin>>u>>v>>flag>>x>>y;
e[u][v]=x;
w[u][v]=y;
if(!flag){
e[v][u]=x;
w[v][u]=y;
}
}
cin>>s>>t;
dis[s]=;
for(int i=;i<n;++i){
int u=-,mn=1e9+;
for(int j=;j<n;++j){
if(!vis[j]&&dis[j]<mn){
u=j;
mn=dis[j];
}
}
if(u==-)
break;
vis[u]=;
for(int v=;v<n;++v){
if(!vis[v]&&e[u][v]<1e9+){
if(e[u][v]+dis[u]<dis[v]){
dis[v]=e[u][v]+dis[u];
weight[v]=weight[u]+w[u][v];
pre[v]=u;
}
else if(e[u][v]+dis[u]==dis[v]&&weight[v]>weight[u]+w[u][v]){
weight[v]=weight[u]+w[u][v];
pre[v]=u;
}
}
}
}
dfsdispath(t);
tim[s]=;
for(int i=;i<;++i)
vis[i]=;
for(int i=;i<n;++i){
int u=-,mn=1e9+;
for(int j=;j<n;++j){
if(!vis[j]&&mn>tim[j]){
u=j;
mn=tim[j];
}
}
if(u==-)
break;
vis[u]=;
for(int v=;v<n;++v){
if(!vis[v]&&w[u][v]<1e9+){
if(w[u][v]+tim[u]<tim[v]){
tim[v]=w[u][v]+tim[u];
timpre[v]=u;
num[v]=num[u]+;
}
else if(w[u][v]+tim[u]==tim[v]&&num[u]+<num[v]){
timpre[v]=u;
num[v]=num[u]+;
}
}
}
}
dfstimpath(t);
cout<<"Distance = "<<dis[t];
if(dispath==timpath)
cout<<"; Time = "<<tim[t]<<": ";
else{
cout<<": ";
for(int i=dispath.size()-;i>=;--i){
cout<<dispath[i];
if(i>)
cout<<" -> ";
}
cout<<"\nTime = "<<tim[t]<<": ";
}
for(int i=timpath.size()-;i>=;--i){
cout<<timpath[i];
if(i>)
cout<<" -> ";
}
return ;
}
【PAT甲级】1111 Online Map (30分)(dijkstra+路径记录)的更多相关文章
- 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 题意: 输入我们当前的位置和目的地,一个在线地图可以推荐几条路径.现在你的工作是向你的用户推荐两条路径:一条是最短的,另一条是最快的.确保任何请求存在路径. ...
- PAT 甲级 1030 Travel Plan (30 分)(dijstra,较简单,但要注意是从0到n-1)
1030 Travel Plan (30 分) A traveler's map gives the distances between cities along the highways, to ...
- PAT 甲级 1072 Gas Station (30 分)(dijstra)
1072 Gas Station (30 分) A gas station has to be built at such a location that the minimum distance ...
- PAT 甲级 1049 Counting Ones (30 分)(找规律,较难,想到了一点但没有深入考虑嫌麻烦)***
1049 Counting Ones (30 分) The task is simple: given any positive integer N, you are supposed to co ...
- PAT 甲级 1080 Graduate Admission (30 分) (简单,结构体排序模拟)
1080 Graduate Admission (30 分) It is said that in 2011, there are about 100 graduate schools ready ...
- PAT 甲级 1026 Table Tennis (30 分)(坑点很多,逻辑较复杂,做了1天)
1026 Table Tennis (30 分) A table tennis club has N tables available to the public. The tables are ...
- PAT 甲级 1022 Digital Library (30 分)(字符串读入getline,istringstream,测试点2时间坑点)
1022 Digital Library (30 分) A Digital Library contains millions of books, stored according to thei ...
- PAT Advanced 1111 Online Map (30) [Dijkstra算法 + DFS]
题目 Input our current position and a destination, an online map can recommend several paths. Now your ...
随机推荐
- Java 并发核心机制
目录 一.J.U.C 简介 二.synchronized 三.volatile 四.CAS 五.ThreadLocal 参考资料
- VS Code的一些常用插件
1.Bracket Pair Colorizer(括号对彩色化)功能:vscode中括号提醒比较简答,对于层级比较多的比较难看出他的结构,它为代码中的各种结对的括号提供了颜色高亮等功能,将括号对用线连 ...
- 【Python】random库
种子相同,随机数相同
- xrdp---远程桌面连接
xrdp is an Open Source Remote desktop Protocol server, which allows you to RDP to your Linux server ...
- C++-数据抽象入门
一.假定数据是如何存储的 隐藏某些实现逻辑时,我们是想要隐藏绘制子弹的细节.我们是通过使用一个可以调用的函数,而不是直接写出绘制子弹到屏幕上的代码来实现的.这里同样可以使用一个函数来隐藏棋盘存储的细节 ...
- Pandas初体验之数据结构——Series和DataFrame
Pandas是为了解决数据分析任务而创建的,纳入了大量的库和标准数据模型,提供了高效地操作大型数据集所需的工具. 对于Pandas包,在Python中常见的导入方法如下: from pandas im ...
- Android SDK安装环境变量配置
安卓tool: http://tools.android-studio.org/ SDK下载地址:http://dl.google.com/android/android-sdk_r24.4.1-wi ...
- c++ char转换成string
第一种:利用赋值号直接赋值 ; string b = a; /* 错误.因为string是一个指针,存储的值是所指向的地址,而char型存储的是内容,所以不可以直接用赋值号赋值 */ const ch ...
- 2.2 logistic回归损失函数(非常重要,深入理解)
上一节当中,为了能够训练logistic回归模型的参数w和b,需要定义一个成本函数 使用logistic回归训练的成本函数 为了让模型通过学习来调整参数,要给出一个含有m和训练样本的训练集 很自然的, ...
- Codeforces补题2020.2.28(Round624 Div 3)
A.Add Odd or Subtract Even 签到题~ #include<bits/stdc++.h> using namespace std; int T; int a,b; i ...