uva 11374
|
Problem D: Airport Express |

In a small city called Iokh, a train service, Airport-Express, takes residents to the airport more quickly than other transports. There are two types of trains in Airport-Express, the Economy-Xpress and the Commercial-Xpress. They travel at different speeds, take different routes and have different costs.
Jason is going to the airport to meet his friend. He wants to take the Commercial-Xpress which is supposed to be faster, but he doesn't have enough money. Luckily he has a ticket for the Commercial-Xpress which can take him one station forward. If he used the ticket wisely, he might end up saving a lot of time. However, choosing the best time to use the ticket is not easy for him.
Jason now seeks your help. The routes of the two types of trains are given. Please write a program to find the best route to the destination. The program should also tell when the ticket should be used.
Input
The input consists of several test cases. Consecutive cases are separated by a blank line.
The first line of each case contains 3 integers, namely N, S and E (2 ≤ N ≤ 500, 1 ≤ S, E ≤ N), which represent the number of stations, the starting point and where the airport is located respectively.
There is an integer M (1 ≤ M ≤ 1000) representing the number of connections between the stations of the Economy-Xpress. The next M lines give the information of the routes of the Economy-Xpress. Each consists of three integers X, Y and Z (X, Y ≤ N, 1 ≤ Z ≤ 100). This means X and Y are connected and it takes Zminutes to travel between these two stations.
The next line is another integer K (1 ≤ K ≤ 1000) representing the number of connections between the stations of the Commercial-Xpress. The next K lines contain the information of the Commercial-Xpress in the same format as that of the Economy-Xpress.
All connections are bi-directional. You may assume that there is exactly one optimal route to the airport. There might be cases where you MUST use your ticket in order to reach the airport.
Output
For each case, you should first list the number of stations which Jason would visit in order. On the next line, output "Ticket Not Used" if you decided NOT to use the ticket; otherwise, state the station where Jason should get on the train of Commercial-Xpress. Finally, print the total time for the journey on the last line. Consecutive sets of output must be separated by a blank line.
Sample Input
4 1 4
4
1 2 2
1 3 3
2 4 4
3 4 5
1
2 4 3
Sample Output
1 2 4
2
5 最短路
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue> using namespace std; const int MAX_N = ;
const int edge = ;
const int INF = ( << );
struct node {
int d,u;
bool operator < (const node &rhs) const {
return d > rhs.d;
}
}; int N,S,E,M,K;
int first[MAX_N],v[edge],Next[edge];
int w[edge],d1[MAX_N],d2[MAX_N];
int x[MAX_N * ],y[MAX_N * ],w1[MAX_N * ];
bool done[MAX_N];
int p1[MAX_N],p2[MAX_N];
int ou[MAX_N],tem[MAX_N]; void add_edge(int id,int u) {
int e = first[u];
Next[id] = e;
first[u] = id;
} void dijkstra(int s,int *d,int *p) {
fill(p + ,p + N + ,-);
for(int i = ; i <= N; ++i) d[i] = INF;
d[s] = ;
memset(done,,sizeof(done));
priority_queue<node > q;
q.push(node {d[s],s}); while(!q.empty()) {
node x = q.top(); q.pop();
int u = x.u;
if(done[u]) continue;
done[u] = ;
for(int e = first[u]; e != -; e = Next[e]) {
if(d[ v[e] ] > d[u] + w[e]) {
d[ v[e] ] = d[u] + w[e];
q.push(node { d[ v[e] ],v[e]});
p[ v[e] ] = u; }
} }
} void output(int id) {
int len = ;
int len1 = ; if(id != ) {
int a,b;
a = id < ? y[-id] :x[id];
b = id < ? x[-id] :y[id];
for(int p = a; p != -; p = p1[p]) {
tem[len++] = p;
}
for(int i = len - ; i >= ; --i) {
ou[len1++] = tem[i];
}
//printf("b= %d\n",p2[b]);
for(int p = b; p != -; p = p2[p]) {
ou[len1++] = p;
}
} else { for(int p = E; p != -; p = p1[p]) {
ou[len1++] = p;
}
for(int i = ,j = len1 - ; i < j; ++i,--j) {
swap(ou[i],ou[j]);
}
} for(int i = ; i < len1; ++i) {
printf("%d%c",ou[i],i == len1 - ? '\n' : ' ');
}
} void solve() {
dijkstra(S,d1,p1);
dijkstra(E,d2,p2);
int ans = d1[E],id = ;
//printf("ans = %d\n",ans);
for(int i = ; i <= K; ++i) {
if(ans > d1[ x[i]] + d2[ y[i] ] + w1[i]) {
ans = d1[ x[i] ] + d2[ y[i] ] + w1[i];
id = i;
}
if(ans > d1[ y[i] ] + d2[ x[i] ] + w1[i]) {
ans = d1[ y[i] ] + d2[ x[i] ] + w1[i];
id = -i;
}
} output(id);
if(id == ) {
printf("Ticket Not Used\n");
} else {
printf("%d\n",id < ? y[-id] : x[id]);
} printf("%d\n",ans); } int main()
{
//freopen("sw.in","r",stdin);
bool ok = ;
while(~scanf("%d%d%d",&N,&S,&E)) {
if(ok) printf("\n");
ok = ;
scanf("%d",&M);
for(int i = ; i <= N; ++i) first[i] = -;
for(int i = ; i < * M; i += ) {
int u;
scanf("%d%d%d",&u,&v[i],&w[i]);
v[i + ] = u;
w[i + ] = w[i];
add_edge(i,u);
add_edge(i + ,v[i]);
} scanf("%d",&K);
for(int i = ; i <= K; ++i) {
scanf("%d%d%d",&x[i],&y[i],&w1[i]);
} solve(); } //cout << "Hello world!" << endl;
return ;
}
uva 11374的更多相关文章
- UVA - 11374 - Airport Express(堆优化Dijkstra)
Problem UVA - 11374 - Airport Express Time Limit: 1000 mSec Problem Description In a small city c ...
- 训练指南 UVA - 11374(最短路Dijkstra + 记录路径 + 模板)
layout: post title: 训练指南 UVA - 11374(最短路Dijkstra + 记录路径 + 模板) author: "luowentaoaa" catalo ...
- uva 11374 最短路+记录路径 dijkstra最短路模板
UVA - 11374 Airport Express Time Limit:1000MS Memory Limit:Unknown 64bit IO Format:%lld & %l ...
- UVA 11374 Airport Express(最短路)
最短路. 把题目抽象一下:已知一张图,边上的权值表示长度.现在又有一些边,只能从其中选一条加入原图,使起点->终点的距离最小. 当加上一条边a->b,如果这条边更新了最短路,那么起点st- ...
- UVA 11374 Airport Express SPFA||dijkstra
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...
- UVA 11374 Halum (差分约束系统,最短路)
题意:给定一个带权有向图,每次你可以选择一个结点v 和整数d ,把所有以v为终点的边权值减少d,把所有以v为起点的边权值增加d,最后要让所有的边权值为正,且尽量大.若无解,输出结果.若可无限大,输出结 ...
- UVA 11374 Airport Express 机场快线(单源最短路,dijkstra,变形)
题意: 给一幅图,要从s点要到e点,图中有两种无向边分别在两个集合中,第一个集合是可以无限次使用的,第二个集合中的边只能挑1条.问如何使距离最短?输出路径,用了第二个集合中的哪条边,最短距离. 思路: ...
- UVA 11374 Airport Express(枚举+最短路)
枚举每条商业线<a, b>,设d[i]为起始点到每点的最短路,g[i]为终点到每点的最短路,ans便是min{d[a] + t[a, b] + g[b]}.注意下判断是否需要经过商业线.输 ...
- UVa 11374 机场快线
https://vjudge.net/problem/UVA-11374 题意: 机场快线分为经济线和商业线两种,线路.速度和价格都不同.你有一张商业线车票,可以坐一站商业线,而其他时候只能乘坐经济线 ...
随机推荐
- 刀哥多线程串行队列gcd-04-dispatch_queue_serial
串行队列 特点 以先进先出的方式,顺序调度队列中的任务执行 无论队列中所指定的执行任务函数是同步还是异步,都会等待前一个任务执行完成后,再调度后面的任务 队列创建 dispatch_queue_t q ...
- 删除redo所有日志,数据库无法启动
半夜在itpub上看到有人发贴,说不小心删除了redo所有日志,导致数据库无法启动,因此模拟了一下. 如下: OS: Oracle Linux Server release 5.7 DB: O ...
- CentOS 7 + nginx + uwsgi + web2py (502 bad gateway nginx)
Web2py开发包中自带的setup-web2py-nginx-uwsgi-centos64.sh脚本, 只能运行在CentOS 6.4中使用, 如果直接在CentOS 7 中使用该脚本布署后, 访问 ...
- Python: 函数参数小结
参数的类型: 函数的参数有2种类型: 1. 函数定义时用于接收值的形式参数Parameters. 2. 函数调用时用于传递值的实际参数Arguments. 参数的传递: 传递方式有2种: 1. 值传递 ...
- AOP在 .NET中的七种实现方法
7Approaches for AOP in .Net AOP在 .NET中的七种实现方法 Here are all the ways that I can think of to add AOPto ...
- Objective-C编码规范
参考 http://www.csdn.net/article/2015-06-01/2824818-objective-c-style-guide/1 介绍 我们制定Objective-C编码规范的原 ...
- Haskell 趣学指南 入门笔记(二)
显示类型声明,Haskell是不用定义类型的原因,很像python 想要确定某个表达式的类型 *Main> :t 'a' 'a' :: Char *Main> :t True True : ...
- color the python console text
//install termcolor module cd \ cd python27 cd scripts pip install termcolor pip install colorama // ...
- 用Keytool和OpenSSL生成和签发数字证书
一)keytool生成私钥文件(.key)和签名请求文件(.csr),openssl签发数字证书 J2SDK在目录%JAVA_HOME%/bin提供了密钥库管理工具Keytool,用于管理密 ...
- asp.net中父子页面通过gridview中的按钮事件进行回传值的问题
这两天写BS程序,遇到父子页面传值的问题,以前没写过web系统,用了几天时间才将问题解决,总结下记录下来: 问题描述: 父页面A中有一个gridview,每行6个列,有5列中均有一个按钮,单击按钮,会 ...