题意:

输入两个正整数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+路径记录)的更多相关文章

  1. PAT甲级——1111 Online Map (单源最短路经的Dijkstra算法、priority_queue的使用)

    本文章同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90041078   1111 Online Map (30 分) ...

  2. PAT甲级1111. Online Map

    PAT甲级1111. Online Map 题意: 输入我们当前的位置和目的地,一个在线地图可以推荐几条路径.现在你的工作是向你的用户推荐两条路径:一条是最短的,另一条是最快的.确保任何请求存在路径. ...

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

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

  5. PAT 甲级 1049 Counting Ones (30 分)(找规律,较难,想到了一点但没有深入考虑嫌麻烦)***

    1049 Counting Ones (30 分)   The task is simple: given any positive integer N, you are supposed to co ...

  6. PAT 甲级 1080 Graduate Admission (30 分) (简单,结构体排序模拟)

    1080 Graduate Admission (30 分)   It is said that in 2011, there are about 100 graduate schools ready ...

  7. PAT 甲级 1026 Table Tennis (30 分)(坑点很多,逻辑较复杂,做了1天)

    1026 Table Tennis (30 分)   A table tennis club has N tables available to the public. The tables are ...

  8. PAT 甲级 1022 Digital Library (30 分)(字符串读入getline,istringstream,测试点2时间坑点)

    1022 Digital Library (30 分)   A Digital Library contains millions of books, stored according to thei ...

  9. PAT Advanced 1111 Online Map (30) [Dijkstra算法 + DFS]

    题目 Input our current position and a destination, an online map can recommend several paths. Now your ...

随机推荐

  1. Linux常用命令: zip、unzip 压缩和解压缩命令

    zip基本用法是: zip [参数] [打包后的文件名] [打包的目录路径] 常用参数: -a 将文件转成ASCII模式 -F 尝试修复损坏的压缩文件 -h 显示帮助界面 -m 将文件压缩之后,删除源 ...

  2. ES6标准入门(第三版).pdf----推荐指数⭐⭐⭐⭐⭐

    链接: https://pan.baidu.com/s/13RHsyTMNx7s1oMqQeYCm3Q 提取码: ikg3 -------------------------------------- ...

  3. ubuntu Redis安装及配置

    1.安装 1.1 下载压缩包:wget http://download.redis.io/releases/redis-5.0.4.tar.gz1.2 解压:tar xzf redis-5.0.4.t ...

  4. Mac下Charles的安装和配置

    一.安装与破解 官网下载,破解方法参考其他,此处略 二.配置 1.电脑端安装 Charles 的根证书 注意:此时钥匙串默认为不信任,需设置为始终信任 2.配置代理:勾选enable transpre ...

  5. Javascript数组与字符串常用api

    目录 javaScript(api学习) 数组有关的api 创建数组 数组的增删改查 indexOf(); push(),pop(),unshift(),shift() forEach() map() ...

  6. JS高级---复习和课程介绍

    课程介绍 浅拷贝 深拷贝----------|======>递归 遍历DOM树-------|======>递归------晚上能够把代码写出来是最好的   正则表达式-------很重要 ...

  7. python | 网络编程(socket、udp、tcp)

    一.套接字 socket 1.1 作用:实现不同主机间的进程间通信(不同电脑.手机等设备之间收发数据) 1.2 分类:udp.tcp 1.3 创建 socket import socket socke ...

  8. 并查集路径减半优化 UnionFind PathHalving (C++)

    /* * UnionFind.h * 有两种实现方式,QuickFind和QuickUnion * QuickFind: * 查找O(1) * 合并O(n) * QuickUnion:(建议使用) * ...

  9. pve proxmox 常见问题,perl warning

    pve命令,如下报错 perl: warning: Setting locale failed. perl: warning: Please check that your locale settin ...

  10. LED Keychain - Widely Used Logo Item

    The LED keychain makes it easy for people to carry their keys with them and carry them with them. It ...