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 题意: 机场快线分为经济线和商业线两种,线路.速度和价格都不同.你有一张商业线车票,可以坐一站商业线,而其他时候只能乘坐经济线 ...
随机推荐
- 网络开发库从libuv说到epoll
引言 这篇博文可能有点水,主要将自己libuv的学习过程和理解. 简单谈方法. 有点杂. 那我们开始吧. 首先介绍 githup . 这个工具特别好用. 代码托管. 如果不FQ可能有点卡. 但是应该试 ...
- struts2框架基本操作总结
struts技术说明 一:第一配置开发环境 1.struts.xml文件 <?xml version="1.0" encoding="UTF-8" ?&g ...
- 关于DataSource的一些记录
今天看WWDC的232_hd_advanced_user_interfaces_with_collection_views,里面花了一般的时间来讲如何合理的设计程序的datesource,将的很有道理 ...
- swift 基于SDK8.0 获取当前时间
var date = NSDate.date() var timeFormatter = NSDateFormatter() timeFormatter.dateFormat = "MM-d ...
- C# 字符串转换值类型
bool status = int.TryParse(m_Judge(12)+"ds",out j); int iParse = int.Parse("4"); ...
- [原]Java修炼 之 基础篇(一)Java语言特性
学习软件开发,首先要选择的就是选择需要采用的编程语言,考虑语言本身的优缺点和实际需求,综合评价之后选择相关的语言进行系统开发.本篇博客开始就从近年来比较流行的Java开始为大家讲起. 背景 1995年 ...
- JS中showModalDialog 详细使用
基本介绍: showModalDialog() (IE 4+ 支持) showModelessDialog() (IE 5+ 支持) window.showModalDialog() 方法用来创建一个 ...
- JAVA栈实例—括号匹配
import java.util.Stack; public class test { public static void main(String[] args){ System.out.print ...
- LinuxC 文件与目录 打印文件操作错误信息
打印文件操作错误信息 在进行文件操作是,会遇到权限不足.找不到文件等错误,可以在程序中设置错误捕捉语句并显示错误.错误捕捉和错误输出使用用错误号和streero实现. 函数原型 : char *str ...
- Android 锁屏软件MemoryDebris测试报告
目 录 项目基本信息 第1章 引言 1.1 编写目的 1.2 项目背景 1.3 参考资料 1.4 术语和缩略语 第2章 ...