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 NS and E (2 ≤ N ≤ 500, 1 ≤ SE ≤ 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 XY and Z (XY ≤ 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的更多相关文章

  1. UVA - 11374 - Airport Express(堆优化Dijkstra)

    Problem    UVA - 11374 - Airport Express Time Limit: 1000 mSec Problem Description In a small city c ...

  2. 训练指南 UVA - 11374(最短路Dijkstra + 记录路径 + 模板)

    layout: post title: 训练指南 UVA - 11374(最短路Dijkstra + 记录路径 + 模板) author: "luowentaoaa" catalo ...

  3. uva 11374 最短路+记录路径 dijkstra最短路模板

    UVA - 11374 Airport Express Time Limit:1000MS   Memory Limit:Unknown   64bit IO Format:%lld & %l ...

  4. UVA 11374 Airport Express(最短路)

    最短路. 把题目抽象一下:已知一张图,边上的权值表示长度.现在又有一些边,只能从其中选一条加入原图,使起点->终点的距离最小. 当加上一条边a->b,如果这条边更新了最短路,那么起点st- ...

  5. UVA 11374 Airport Express SPFA||dijkstra

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...

  6. UVA 11374 Halum (差分约束系统,最短路)

    题意:给定一个带权有向图,每次你可以选择一个结点v 和整数d ,把所有以v为终点的边权值减少d,把所有以v为起点的边权值增加d,最后要让所有的边权值为正,且尽量大.若无解,输出结果.若可无限大,输出结 ...

  7. UVA 11374 Airport Express 机场快线(单源最短路,dijkstra,变形)

    题意: 给一幅图,要从s点要到e点,图中有两种无向边分别在两个集合中,第一个集合是可以无限次使用的,第二个集合中的边只能挑1条.问如何使距离最短?输出路径,用了第二个集合中的哪条边,最短距离. 思路: ...

  8. UVA 11374 Airport Express(枚举+最短路)

    枚举每条商业线<a, b>,设d[i]为起始点到每点的最短路,g[i]为终点到每点的最短路,ans便是min{d[a] + t[a, b] + g[b]}.注意下判断是否需要经过商业线.输 ...

  9. UVa 11374 机场快线

    https://vjudge.net/problem/UVA-11374 题意: 机场快线分为经济线和商业线两种,线路.速度和价格都不同.你有一张商业线车票,可以坐一站商业线,而其他时候只能乘坐经济线 ...

随机推荐

  1. 常用的PC/SC接口函数

    PC/SC规范是一个基于WINDOWS平台的一个标准用户接口(API),提供了一个从个人电脑(Personal Computer)到智能卡(SmartCard)的整合环境,PC/SC规范建立在工业标准 ...

  2. 掌握这两个技术点,你可以玩转AppCan前端开发

    “AppCan的前端开发其实非常简单,只要掌握两方面的技术即可.一方面是会使用栅格布局完成UI的界面排版,另外一方面就是使用AppCan MVVM模型来完成整个页面构造和用户操作逻辑.” 在2016A ...

  3. pb对Web Service的操作可使用两种方式实现

    从PB8.0/9.0开始,就已经提供Web Service Proxy功能,能够直接进行相关程序的编写. 但是,部分老项目使用PB6.5开发 研究后发现,其实PB6.5要操作Web Service也挺 ...

  4. P3245: 最快路线

    这道题其实还是不难的,只是自己搞混了=-=//晕,做了好久啊,其实就是个spfa,关键是存储路径搞昏了.输出格式要求太严了,航模不能有空格啊,所以因为格式WA了三次,哭啊/(ㄒoㄒ)/~~.贴上代码吧 ...

  5. 微软职位内部推荐-Sr SDE

    微软近期Open的职位: MSN reaches nearly half a billion people across the globe where we are the #1 portal in ...

  6. mozilla css developer center

    https://developer.mozilla.org/en-US/docs/Web/CSS

  7. COS中访问文件的三种方式

    1.通过FID来访问文件(比如EF,DF) 2.通过SFI来访问文件(有些COS命令可以通过SFI来快速访问文件,而不需要事先选中文件) 3.通过文件名来访问文件(只能是DF文件)

  8. 随堂作业——到底有几个“1”(C++)

    一.设计思路 在课堂上讨论的时候,老师提出的思路是利用之前的结果计算出比它更大的数字的“1”.但是我不是这么想的,我是把输入的正整数每位上的数都分解出来计算.如abc,就先算c,再加上b,最后再加上a ...

  9. hdu 2629 Identity Card (字符串解析模拟题)

    这题是一个字符串模拟水题,给12级学弟学妹们找找自信的,嘿嘿; 题目意思就是要你讲身份证的上的省份和生日解析出来输出就可以了: http://acm.hdu.edu.cn/showproblem.ph ...

  10. Analyzer使用第二Y轴,以及同一分析图不同量值使用不同的图形样式

    Analyzer的建立分析图后,图中有两个量值,希望能显示成不同的图形样式,如一个是柱图.一个是线图. 1.设置显示多个量值: 3.设置显示出图例,即表明图中量值内容的说明: 2.右键图例中要修改为不 ...