题意:

输入两个正整数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. Hadoop的完全分布式搭建

    一.准备虚拟机两台 1.将虚拟机进行克隆https://www.cnblogs.com/the-roc/p/12336745.html 2.1将克隆虚拟机的IP修改一下 vi /etc/sysconf ...

  2. hive创建表时报错

    这是因为mysql字符集的原因.修改mysql的字符集. mysql> alter database hive character set latin1; 参考博客:https://blog.c ...

  3. DVR

    DVR,全称为Digital Video Recorder(硬盘录像机),即数字视频录像机,相对于传统的模拟视频录像机,采用硬盘录像,故常常被称为硬盘录像机.它是一套进行图像计算存储处理的计算机系统, ...

  4. SpringMVC起步

    SpringMVC: SpringMVC是Spring的一个组件,作为控制器,可以替代Servlet SpringMVC的开发过程: 请求发送 DispatcherServlet查询一个或多个Hand ...

  5. C++构造函数和重载函数运算符如何区分

    构造函数和重载函数运算符如何区分: class Distance { private: int feet; int inches; public: Distance(){ feet = ; inche ...

  6. AE接口编程

    [转]原文链接:https://malagis.com/arcgis-engine-10-develop-handbook-2-1.html 使用 ArcGIS Engine,也就意味着使用里面的接口 ...

  7. Unable to load authentication plugin 'caching_sha2_password'

    Caused by: com.mysql.cj.core.exceptions.WrongArgumentException: Unable to load authentication plugin ...

  8. 浅析State-Thread

    State-Thread(以下简称st),是一个由C语言编写的小巧.简洁却高效的开源协程库.这个库基于单线程运作.不强制占用用户线程,给予了开发者最大程度的轻量级和较低的侵入性.本篇文章中,网易云信音 ...

  9. [NOI2014] 魔法森林 - Link Cut Tree

    [NOI2014] 魔法森林 Description 给定一张图,每条边 \(i\) 的权为 \((a_i,b_i)\), 求一条 \(1 \sim n\) 路径,最小化 \(\max_{i\in P ...

  10. scw——01 java.lang.IllegalStateException: Could not initialize plugin: interface org.mockito.plugins.MockMake

    错误: java.lang.IllegalStateException: Could not initialize plugin: interface org.mockito.plugins.Mock ...