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]}.注意下判断是否需要经过商业线.输 ...
随机推荐
- bzoj 1666: [Usaco2006 Oct]Another Cow Number Game 奶牛的数字游戏【模拟】
模拟 #include<iostream> #include<cstdio> using namespace std; int n,ans; int main() { scan ...
- 洛谷P4241 采摘毒瘤
传送门 完了我连背包都不会了…… 考虑暴力,先枚举最小的数是哪个,设大小为$d_i$,个数为$k_i$,所有比它小的数的总和是$sum$,然后把所有比它小的全都装进背包,它以及比他大的做一个多重背包, ...
- tfs
安装Team Foundation Server 2012过程截图 专题图 1,下载Team Foundation Server 2012 官方下载: http://www.microsoft.co ...
- [笔试面试题] 3-C++关键字篇
C/C++关键字篇 语言是编程的基础,掌握基本的语言知识是编程的前提条件.关键字是组成语言的最基本单位,对关键字的理解,有助于编写高质量的代码. 1 static(静态)变量有什么作用? 在函数体 ...
- CodeForces - 7D Palindrome Degree
最近接触了一点字符串算法,其实也就是一个简单的最大回文串算法,给定字符串s,求出最大字符串长度. 算法是这样的, 用'#'将s字符串中的每个字符分隔,比如s = "aba",分割后 ...
- ACM_Fibonacci数(同余)
Fibonacci数 Time Limit: 2000/1000ms (Java/Others) Problem Description: 斐波那契数列定义如下:f(0)=0,f(1)=1,f(n+2 ...
- 405 Convert a Number to Hexadecimal 数字转换为十六进制数
给定一个整数,编写一个算法将这个数转换为十六进制数.对于负整数,我们通常使用 补码运算 方法.注意: 十六进制中所有字母(a-f)都必须是小写. 十六进制字符串中不能包含多余的前导零.如果 ...
- Xml学习笔记(3)利用递归解析Xml文档添加到TreeView中
利用递归解析Xml文档添加到TreeView中 private void Form1_Load(object sender, EventArgs e) { XmlDocument doc = new ...
- [ USACO 2017 FEB ] Why Did the Cow Cross the Road III (Gold)
\(\\\) \(Description\) 给定长度为\(2N\)的序列,\(1\text ~N\)各出现过\(2\)次,\(i\)第一次出现位置记为\(a_i\),第二次记为\(b_i\),求满足 ...
- opencv3.31+vs2015终于配置成功了
风萧萧兮易水寒, 熬了几个夜晚,终于把opencv配好了, 来图一 唉试了很多方法,终于成功. 教程和资料会发在个人网站里. 测试 代码 #include <iostream> #incl ...