训练指南 UVA - 11374(最短路Dijkstra + 记录路径 + 模板)
layout: post
title: 训练指南 UVA - 11374(最短路Dijkstra + 记录路径 + 模板)
author: "luowentaoaa"
catalog: true
mathjax: true
tags:
- 最短路
- Dijkstra
- 图论
- 训练指南
Airport Express
题意
机场快线有经济线和商业线,现在分别给出经济线和商业线的的路线,现在只能坐一站商业线,其他坐经济线,问从起点到终点的最短用时是多少,还有路线是怎样的;
题解
预处理出起点到所有站的最短距离和终点到所有站的最短距离,枚举要坐的那趟商业线,然后里面最小的就是答案了;
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod=998244353;
const int maxn=550;
const ll inf=0x3f3f3f3f3f3f3f3fLL;
struct Edge{
int from,to,dist;
};
struct HeapNode{
int d,u;
bool operator <(const HeapNode& rhs)const{
return d>rhs.d;
}
};
struct Dijkstra{
int n,m; //点数和边数 点编号0~N-1
vector<Edge>edges;
vector<int>G[maxn];
bool done[maxn];
int d[maxn];
int p[maxn];
void init(int n){
this->n=n;
for(int i=0;i<n;i++)G[i].clear();
edges.clear();
}
void AddEdge(int from,int to,int dist){
edges.push_back((Edge){from,to,dist});
m=edges.size();
G[from].push_back(m-1);
}
void dijkstra(int s){
priority_queue<HeapNode>Q;
for(int i=0;i<n;i++)d[i]=inf;
d[s]=0;
memset(done,0,sizeof(done));
Q.push((HeapNode){0,s});
while(!Q.empty()){
HeapNode x=Q.top();Q.pop();
int u=x.u;
if(done[u])continue;
done[u]=true;
for(int i=0;i<G[u].size();i++){
Edge& e=edges[G[u][i]];
if(d[e.to]>d[u]+e.dist){
d[e.to]=d[u]+e.dist;
p[e.to]=G[u][i];
Q.push((HeapNode){d[e.to],e.to});
}
}
}
}
void GetShortestPaths(int s,int* dist,vector<int>* paths){//paths是二维链表
dijkstra(s);
for(int i=0;i<n;i++){
dist[i]=d[i];
paths[i].clear();
int t=i;
paths[i].push_back(t);
while(t!=s){
paths[i].push_back(edges[p[t]].from);
t=edges[p[t]].from;
}
reverse(paths[i].begin(),paths[i].end());
}
}
};
Dijkstra solver;
int d1[maxn],d2[maxn];
vector<int>paths1[maxn],paths2[maxn];
int main()
{
// std::ios::sync_with_stdio(false);
// std::cin.tie(0);
// std::cout.tie(0);
int kase=0,N,S,E,M,K,X,Y,Z;
while(scanf("%d%d%d%d", &N, &S, &E, &M) == 4) {
solver.init(N);
S--;E--;
for(int i=0;i<M;i++){
scanf("%d%d%d", &X, &Y, &Z); X--; Y--;
solver.AddEdge(X,Y,Z);
solver.AddEdge(Y,X,Z);
}
solver.GetShortestPaths(S,d1,paths1);
solver.GetShortestPaths(E,d2,paths2);
int ans=d1[E];
vector<int>path=paths1[E];
int midpoint=-1;
scanf("%d", &K);
for(int i=0;i<K;i++){
scanf("%d%d%d", &X, &Y, &Z); X--; Y--;
for(int j=0;j<2;j++){
if(d1[X]+d2[Y]+Z<ans){
ans=d1[X]+d2[Y]+Z;
path=paths1[X];
for(int p=paths2[Y].size()-1;p>=0;p--)
path.push_back(paths2[Y][p]);
midpoint=X;
}
swap(X,Y);
}
}
if(kase != 0) printf("\n");
kase++;
for(int i = 0; i < path.size()-1; i++) printf("%d ", path[i]+1);
printf("%d\n", E+1);
if(midpoint == -1) printf("Ticket Not Used\n"); else printf("%d\n", midpoint+1);
printf("%d\n", ans);
}
return 0;
}
训练指南 UVA - 11374(最短路Dijkstra + 记录路径 + 模板)的更多相关文章
- 训练指南 UVA - 10917(最短路Dijkstra + 基础DP)
layout: post title: 训练指南 UVA - 10917(最短路Dijkstra + 基础DP) author: "luowentaoaa" catalog: tr ...
- 训练指南 UVA - 11478(最短路BellmanFord+ 二分+ 差分约束)
layout: post title: 训练指南 UVA - 11478(最短路BellmanFord+ 二分+ 差分约束) author: "luowentaoaa" catal ...
- 训练指南 UVA - 11090(最短路BellmanFord+ 二分判负环)
layout: post title: 训练指南 UVA - 11090(最短路BellmanFord+ 二分判负环) author: "luowentaoaa" catalog: ...
- 训练指南 UVA - 11419(二分图最小覆盖数)
layout: post title: 训练指南 UVA - 11419(二分图最小覆盖数) author: "luowentaoaa" catalog: true mathjax ...
- 训练指南 UVA - 11383(KM算法的应用 lx+ly >=w(x,y))
layout: post title: 训练指南 UVA - 11383(KM算法的应用 lx+ly >=w(x,y)) author: "luowentaoaa" cata ...
- 训练指南 UVA - 11354(最小生成树 + 倍增LCA)
layout: post title: 训练指南 UVA - 11354(最小生成树 + 倍增LCA) author: "luowentaoaa" catalog: true ma ...
- 训练指南 UVA - 11324(双连通分量 + 缩点+ 基础DP)
layout: post title: 训练指南 UVA - 11324(双连通分量 + 缩点+ 基础DP) author: "luowentaoaa" catalog: true ...
- uva 11374 最短路+记录路径 dijkstra最短路模板
UVA - 11374 Airport Express Time Limit:1000MS Memory Limit:Unknown 64bit IO Format:%lld & %l ...
- 算法竞赛入门经典训练指南——UVA 11300 preading the Wealth
A Communist regime is trying to redistribute wealth in a village. They have have decided to sit ever ...
随机推荐
- TCP/IP Note4
TCP/IP邮件 你的电子邮件程序会使用不同的TCP/IP协议: 使用SMTP来发送邮件: 使用POP从邮件服务器下载邮件: 使用IMAP连接到邮件服务器 1. SMTP - 简单邮件传输协议 SMT ...
- [NOIP2018 TG D2T1]旅行
题目大意:$NOIP\;TG\;D2T1$ 题解:一棵树的很简单,第一个点一定是$1$,只需要对每个节点,找最小的没有访问过的节点访问即可,我写的是$O(n\log_2n)$. 考虑基环树的部分,一个 ...
- 解决IE下页面空白或者报错:[vuex] vuex requires a Promise polyfill in this browser
[vuex] vuex requires a Promise polyfill in this browser 上述错误的原因是不支持 Promise 方法,导致页面出现空白无法加载. 解决方法如下: ...
- 门户系统整合sso cookie共享及显示用户信息
1.1 门户系统整合sso 在门户系统点击登录连接跳转到登录页面.登录成功后,跳转到门户系统的首页,在门户系统中需要从cookie中 把token取出来.所以必须在登录成功后把token写入cooki ...
- bzoj 1517 [POI2006]Met 贪心
[POI2006]Met Time Limit: 15 Sec Memory Limit: 162 MBSubmit: 203 Solved: 108[Submit][Status][Discus ...
- WebOS系列-了解Wekbit【邓侃】
注:[转载请注明文章来源.保持原样] 出处:http://www.cnblogs.com/jyli/archive/2010/02/02/1660634.html 作者:李嘉昱 这是Kan老大的We ...
- nodejs与sqlite
//打开数据库var db = new sqlite3.Database('xx.db'); // 关闭数据库db.close(); db.run('xx'); // 数据库对象的run函数可以执行 ...
- Docker原理 -- namespace与CGroup
命名空间 PID(Process ID) 进程隔离 NET(Network) 管理网络隔离 IPC(InterProcess Communication) 管理跨进程通信的访问 MNT(Mount) ...
- centos6.4 nginx+mysql+php整合phpmyadmin出错解决方案
今天在centos下整合phpmyadmin出错,错误提示如下: Error during session start; please check your PHP and/or webserver ...
- wiki 2490 导弹拦截塔
2013-09-23 21:16 二分答案+匈牙利判断 对于每一个时间,我们重新建一张二分图,由于每个塔可能打多次,所以要拆点, 对于每个拆的点的可行飞行距离为(mid-t1)-(ll-1)*(t1+ ...