题意:

输入两个正整数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. vue.js中使用离线检测

    Html5在window.navigator对象上添加了一个属性onLine 返回布尔值 true表示在线.同时新增了两个事件: window.addEventListener('online', f ...

  2. 呼叫到达率100%,网易云信信令SDK免费上线!

    近期,网易云信推出一款稳定可靠.到达率高.扩展性较强的信令通道产品--信令SDK.它能够提供可靠的消息通道,可用于搭建音视频场景下的呼叫邀请机制.信令SDK目前兼容市面上所有主流的音视频SDK,呼叫到 ...

  3. 编码 - 设置 win10 下 cmd 编码格式

    概述 cmd 编码格式修改 背景 之前尝试过修改 gitbash(mingw) 的 Character Set 这次尝试修改一下 cmd 的编码格式 准备 os win10.1903 1. 查看 当前 ...

  4. 【Python】字符串处理函数

  5. linq和扩展方法

    c#的扩展方法 1.必须是在一个非嵌套.非泛型的静态类中的静态方法 2.至少一个参数,第一个参数附加this,不能有其他修饰符如out.ref 3.第一个参数不能是指针类型 上面例子是自定义的一个扩展 ...

  6. [BJWC2010] 外星联络 - 后缀数组

    [BJWC2010] 外星联络 Description 求一个 \(01\) 串中所有重复出现次数大于 \(1\) 的子串所出现的次数,按照字典序排序输出. Solution 预处理出后缀数组和高度数 ...

  7. dojo - 相关教程

    https://blog.csdn.net/dojotoolkit/article/details/6688058

  8. Jupyter Notebook 更改本地目录

    首先如果使用anaconda直接安装jupyter notebook的话,直接在windows的cmd中输入jupyter notebook是没有用的,参见下图: 原因可能是anaconda代理了所有 ...

  9. dfs题型一

    代码: #include <iostream> #include <algorithm> #include <vector> using namespace std ...

  10. 题解【CJOJ1371】[IOI2002]任务安排

    P1371 - [IOI2002]任务安排 Description N个任务排成一个序列在一台机器上等待完成(顺序不得改变),这N个任务被分成若干批,每批包含相邻的若干任务.从时刻0开始,这些任务被分 ...