UVa 11374 机场快线
https://vjudge.net/problem/UVA-11374
题意:
机场快线分为经济线和商业线两种,线路、速度和价格都不同。你有一张商业线车票,可以坐一站商业线,而其他时候只能乘坐经济线。你的任务是找一条去机场最快的线路。
思路:
因为商业线只能坐一站,所有可以枚举坐的是哪一站,用dijkstra算出起点到每个点的最短时间f(x)和终点到每个点的最短时间g(x),则总时间为f(a)+T(a,b)+g(b),其中T(a,b)为从a坐一站商业线到达b的时间。
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std; const int INF = ;
const int maxn = + ; struct Edge
{
int from, to, dist;
Edge(int u, int v, int d) :from(u), to(v), dist(d){}
}; struct HeapNode
{
int d, u;
HeapNode(int x, int y) :d(x), u(y){}
bool operator < (const HeapNode& rhs) const{
return d > rhs.d;
}
}; struct Dijkstra
{
int n, m; //点数和边数
vector<Edge> edges; //边列表
vector<int> G[maxn]; //每个结点出发的边编号(从0开始编号)
bool done[maxn]; //是否已永久标号
int d[maxn]; //s到各个点的距离
int p[maxn]; //最短路中的上一条边 void init(int n)
{
this->n = n;
for (int i = ; i < n; i++) G[i].clear();
edges.clear();
} void AddEdges(int from, int to, int dist)
{
edges.push_back(Edge(from,to,dist));
m = edges.size();
G[from].push_back((m - ));
} void dijkstra(int s)
{
priority_queue<HeapNode> Q;
for (int i = ; i < n; i++) d[i] = INF;
d[s] = ;
memset(done, , sizeof(done));
Q.push(HeapNode(,s));
while (!Q.empty())
{
HeapNode x = Q.top(); Q.pop();
int u = x.u;
if (done[u]) continue;
done[u] = true;
for (int i = ; 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] = e.from;
Q.push(HeapNode(d[e.to],e.to));
}
}
}
} void getpath(int s, int e, vector<int>& path)
{
int pos = e;
while (true)
{
path.push_back(pos);
if (pos == s)
break;
pos = p[pos];
}
} }t[]; int N, S, E;
vector<int> path; int main()
{
//freopen("D:\\input.txt", "r", stdin);
int time, kase = ;
while (scanf("%d%d%d", &N, &S, &E) != EOF)
{
S--; E--;
if (kase != ) printf("\n");
kase++;
t[].init(N);
t[].init(N);
path.clear();
int M, K;
int u, v, d;
scanf("%d", &M);
while (M--)
{
scanf("%d%d%d", &u, &v, &d);
u--; v--;
t[].AddEdges(u, v, d);
t[].AddEdges(u, v, d);
t[].AddEdges(v, u, d);
t[].AddEdges(v, u, d);
}
t[].dijkstra(S);
t[].dijkstra(E);
int ks = -, ke = -;
time = t[].d[E];
scanf("%d", &K);
while (K--)
{
scanf("%d%d%d", &u, &v, &d);
u--;v--;
if (d + t[].d[u] + t[].d[v] < time){
time = d + t[].d[u] + t[].d[v];
ks = u; ke = v;
}
if (d + t[].d[v] + t[].d[u] < time){
time = d + t[].d[v] + t[].d[u];
ks = v; ke = u;
}
}
if (ks == -)
{
t[].getpath(S, E, path);
reverse(path.begin(), path.end());
for (int i = ; i < path.size() - ; i++)
printf("%d ", path[i]+);
printf("%d\n", E+);
printf("Ticket Not Used\n");
printf("%d\n", time);
}
else
{
t[].getpath(S, ks, path);
reverse(path.begin(), path.end());
t[].getpath(E, ke, path);
for (int i = ; i < path.size() - ; i++)
printf("%d ", path[i]+ );
printf("%d\n", E+);
printf("%d\n", ks + );
printf("%d\n", time);
}
}
return ;
}
UVa 11374 机场快线的更多相关文章
- 训练指南 UVA - 11374(最短路Dijkstra + 记录路径 + 模板)
layout: post title: 训练指南 UVA - 11374(最短路Dijkstra + 记录路径 + 模板) author: "luowentaoaa" catalo ...
- UVA 11374 Airport Express SPFA||dijkstra
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...
- UVA - 11374 - Airport Express(堆优化Dijkstra)
Problem UVA - 11374 - Airport Express Time Limit: 1000 mSec Problem Description In a small city c ...
- uva 11374 最短路+记录路径 dijkstra最短路模板
UVA - 11374 Airport Express Time Limit:1000MS Memory Limit:Unknown 64bit IO Format:%lld & %l ...
- UVA 11374 Airport Express 机场快线(单源最短路,dijkstra,变形)
题意: 给一幅图,要从s点要到e点,图中有两种无向边分别在两个集合中,第一个集合是可以无限次使用的,第二个集合中的边只能挑1条.问如何使距离最短?输出路径,用了第二个集合中的哪条边,最短距离. 思路: ...
- UVA 11374 Airport Express(最短路)
最短路. 把题目抽象一下:已知一张图,边上的权值表示长度.现在又有一些边,只能从其中选一条加入原图,使起点->终点的距离最小. 当加上一条边a->b,如果这条边更新了最短路,那么起点st- ...
- UVA - 11374 Airport Express (Dijkstra模板+枚举)
Description Problem D: Airport Express In a small city called Iokh, a train service, Airport-Express ...
- uva 11374
Problem D: Airport Express In a small city called Iokh, a train service, Airport-Express, takes resi ...
- UVA 11374 Halum (差分约束系统,最短路)
题意:给定一个带权有向图,每次你可以选择一个结点v 和整数d ,把所有以v为终点的边权值减少d,把所有以v为起点的边权值增加d,最后要让所有的边权值为正,且尽量大.若无解,输出结果.若可无限大,输出结 ...
随机推荐
- ios ASIHTTPRequest类库简介和使用说明
官方网站: http://allseeing-i.com/ASIHTTPRequest/ .可以从上面下载到最新源码,以及获取到相关的资料. 使用iOS SDK中的HTTP网络请求API,相当的复杂, ...
- MDK软件仿真常见问题
一直不知道MDK该怎么仿真调试程序,之前试了好几次都没有成功.因为有个程序一直不知道里面的变量对应着外部怎么的模式,今天想起可以用仿真调试的方法查看当外部设置某种模式的时候, 内部变量的变化,这样想来 ...
- 170605、防止sql注入(二)
java filter防止sql注入攻击 原理,过滤所有请求中含有非法的字符,例如:, & < select delete 等关键字,黑客可以利用这些字符进行注入攻击,原理是后台实现使 ...
- Oracle之catalog恢复目录的创建于维护(51CTO风哥rman课程)
catalog恢复目录配置过程 1,创建一个表空间 2,创建rman用户并授权 3,创建恢复目录 4,配置TNS 5,注册数据库 6,检查 创建ramn表空间 首先查看一下其他表空间位置 create ...
- HI3518E用J-link烧写裸板fastboot u-boot流程
Hi3518E的裸板烧写fastboot是不能像HI3531那样,可以通过FB直接烧写.遵循ARM9的烧写流程.其中一般u-boot的烧写流程可分为几类:第一:通过编程器芯片直接烧写:第二通过RVDS ...
- 对10进制16位长的主键的缩短处理 NULL
# 对问题表去除旧有主键,新建自增主键:ALTER TABLE `question`CHANGE COLUMN `id` `id16` bigint(20) NULL COMMENT 'id_to_d ...
- Linux知识总汇
Linux相关教程 Linux的安装以及基础配置 Linux上安装Python3 Linux上安装pip以及setuptools Linux上安装MySQL Linux上安装Django Linux上 ...
- mysql 数据操作 单表查询 concat_ws() 定义显示格式
有个需求用concat以这种格式打印查询 mysql> select concat(name,':',age) from employee; +----------------------+ | ...
- centos linux 系统日常管理4 scp,rsync,md5sum,sha1sum,strace ,find Rsync 常见错误及解决方法 第十七节课
centos linux 系统日常管理4 scp,rsync,md5sum,sha1sum,strace ,find Rsync 常见错误及解决方法 第十七节课 rsync可以增量同步,scp不行 ...
- 跟我学Makefile(三)
紧接着跟我学Makefile(二)继续学习:变量高级用法 (1)变量值的替换 :替换变量中的共有的部分,其格式是“$(var:a=b)”或是“${var:a=b}”,把变量“var”中所有以“a”字串 ...