BNUOJ 19792 Airport Express
Airport Express
This problem will be judged on UVA. Original ID: 11374
64-bit integer IO format: %lld Java class name: Main
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 Z minutes 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 <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
#include <cstdlib>
#include <string>
#include <set>
#include <stack>
#define LL long long
#define pii pair<int,int>
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = ;
int mp[maxn][maxn];
int N,S,E;
vector<int>g[maxn];
vector<int>path;
priority_queue< pii,vector< pii >,greater< pii > >q;
struct Dijkstra{
int d[maxn],p[maxn];
bool done[maxn];
void init(){
while(!q.empty()) q.pop();
for(int i = ; i <= N; i++){
done[i] = false;
d[i] = INF;
p[i] = -;
}
}
void go(int s){
d[s] = ;
q.push(make_pair(d[s],s));
while(!q.empty()){
int u = q.top().second;
q.pop();
if(done[u]) continue;
done[u] = true;
for(int i = ; i < g[u].size(); i++){
if(d[g[u][i]] > d[u]+mp[u][g[u][i]]){
d[g[u][i]] = d[u]+mp[u][g[u][i]];
p[g[u][i]] = u;
q.push(make_pair(d[g[u][i]],g[u][i]));
}
}
}
}
void getPath(vector<int>&path,int s,int e){
while(true){
path.push_back(e);
if(e == s) break;
e = p[e];
}
}
};
Dijkstra o[];
int main() {
int m,i,j,u,v,w,ans,x,y,k,cs = ;
while(~scanf("%d %d %d",&N,&S,&E)){
if(cs++) puts("");
for(i = ; i <= N; i++){
g[i].clear();
for(j = ; j <= N; j++)
mp[i][j] = INF;
}
scanf("%d",&m);
for(i = ; i < m; i++){
scanf("%d %d %d",&u,&v,&w);
if(mp[u][v] == INF){
g[u].push_back(v);
g[v].push_back(u);
}
if(w < mp[u][v]) mp[u][v] = mp[v][u] = w;
}
o[].init();
o[].go(S);
o[].init();
o[].go(E);
ans = o[].d[E];
x = y = -;
scanf("%d",&k);
for(i = ; i < k; i++){
scanf("%d %d %d",&u,&v,&w);
if(ans > o[].d[u]+o[].d[v]+w){
ans = o[].d[u]+o[].d[v]+w;
x = u;
y = v;
}
if(ans > o[].d[v]+o[].d[u]+w){
ans = o[].d[v]+o[].d[u]+w;
x = v;
y = u;
}
}
path.clear();
if(x == -){
o[].getPath(path,S,E);
reverse(path.begin(),path.end());
for(i = ; i < path.size(); i++){
printf("%d",path[i]);
if(i+ < path.size()) putchar(' ');
else putchar('\n');
}
puts("Ticket Not Used");
printf("%d\n",ans);
}else{
o[].getPath(path,S,x);
reverse(path.begin(),path.end());
o[].getPath(path,E,y);
for(i = ; i < path.size(); i++){
printf("%d",path[i]);
if(i+ < path.size()) putchar(' ');
else putchar('\n');
}
printf("%d\n%d\n",x,ans);
}
}
return ;
}
BNUOJ 19792 Airport Express的更多相关文章
- UVA - 11374 - Airport Express(堆优化Dijkstra)
Problem UVA - 11374 - Airport Express Time Limit: 1000 mSec Problem Description In a small city c ...
- UVA - 11374 Airport Express (Dijkstra模板+枚举)
Description Problem D: Airport Express In a small city called Iokh, a train service, Airport-Express ...
- UVA 11374 Airport Express SPFA||dijkstra
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...
- Airport Express UVA - 11374
In a small city called Iokh, a train service, Airport-Express, takes residents to the airport more q ...
- Uva11374 Airport Express
最短路问题. 从起点和终点开始各跑一次dijkstra,可以得到起点.终点到任意点的距离.枚举使用的商业线路,找最优解. 破题卡输出,记录前驱和输出什么的仿佛比算法本身还麻烦. /*by Silver ...
- UVA 11374 Airport Express(最短路)
最短路. 把题目抽象一下:已知一张图,边上的权值表示长度.现在又有一些边,只能从其中选一条加入原图,使起点->终点的距离最小. 当加上一条边a->b,如果这条边更新了最短路,那么起点st- ...
- UVA 11374 Airport Express 机场快线(单源最短路,dijkstra,变形)
题意: 给一幅图,要从s点要到e点,图中有两种无向边分别在两个集合中,第一个集合是可以无限次使用的,第二个集合中的边只能挑1条.问如何使距离最短?输出路径,用了第二个集合中的哪条边,最短距离. 思路: ...
- 【uva11374】Airport Express 最短路
题意: 在Iokh市中,机场快线是市民从市内去机场的首选交通工具.机场快线分为经济线和商业线两种,线路,速度和价钱都不同.你有一张商业线车票,可以坐一站商业线,而其他时候只能乘坐经济线.假设换乘时间忽 ...
- UVA 11374 Airport Express(枚举+最短路)
枚举每条商业线<a, b>,设d[i]为起始点到每点的最短路,g[i]为终点到每点的最短路,ans便是min{d[a] + t[a, b] + g[b]}.注意下判断是否需要经过商业线.输 ...
随机推荐
- CodeForces 731B Coupons and Discounts (水题模拟)
题意:有n个队参加CCPC,然后有两种优惠方式,一种是一天买再次,一种是买两天,现在让你判断能不能找到一种方式,使得优惠不剩余. 析:直接模拟,如果本次是奇数,那么就得用第二种,作一个标记,再去计算下 ...
- 符号修饰与函数签名、extern “C”(转载)
转自:http://www.cnblogs.com/monotone/archive/2012/11/16/2773772.html 参考资料: <程序员的自我修养>3.5.3以及3.5. ...
- PCB Genesis加邮票孔(弧与弧)实现算法
一.Genesis加邮票孔(弧与弧)实现算法 1.鼠标点击位置P点(可以确认搜索区域位置,确认点击位置周边元素分区,此所讲算法未应用到P点坐标) 2.求出:P1C与P2C (线与弧最近点距离的2个点) ...
- uml图六种箭头的含义(转载)
在看一些技术博客的时候,经常会见到博客里画上很多uml图.因为经常会被这几种表达关系的箭头搞混,这里我就把常见的6种箭头表达的含义理一下. 泛化 概念:泛化是一种一般与特殊.一般与具体之间关系的描述, ...
- [Swift]二分法的两种方式
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- [转]C语言常见错误总结1
指针与数组的对比c程序中,指针和数组在不少地方可以相互替换着用,让人产生一种错觉,以为两者是等价的 数组要么在静态存储区被创建(如全局数组),要么在栈上被创建.数组名对应着(而不是指向)一块内存,其地 ...
- 对数组名取地址&a和 数组首地址a
#include <iostream> using namespace std; ] = {,,,,}; int main() { cout<<a<<" ...
- 376 Wiggle Subsequence 摆动序列
A sequence of numbers is called a wiggle sequence if the differences between successive numbers stri ...
- 设计模式("大话设计模式"读书笔记 C#实现)
前言:毫无疑问 ,学习一些设计模式,对我们的编程水平的提高帮助很大.写这个博客的时候自己刚开始学习设计模式,难免有错,欢迎评论指正. 我学设计模式的第一本书是“大话设计模式”. 1.为什么要学设计模式 ...
- 【JAVA 学习笔记2】if使用例子
int a =3; if (a%2==0) { System.out.println(a+" 是偶数"); System.out.println(a+" 不是奇数&quo ...