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]}.注意下判断是否需要经过商业线.输 ...
随机推荐
- 国外知名IT网站(转载)
转自:http://supportopensource.iteye.com/blog/780566 =========================================== 1.Cnet ...
- P3343 [ZJOI2015]地震后的幻想乡
传送门 给积分大佬跪了 再给状压大佬也跪了 //minamoto #include<bits/stdc++.h> #define rint register int #define ll ...
- vue开发环境和生产环境里面解决跨域的几种方法
什么是跨域 跨域指浏览器不允许当前页面的所在的源去请求另一个源的数据.源指协议,端口,域名.只要这个3个中有一个不同就是跨域. 这里列举一个经典的列子: #协议跨域 http://a.baidu. ...
- [转]C++常用字符串分割方法实例汇总
本文实例汇总了C++常用字符串分割方法,分享给大家供大家参考.具体分析如下: 我们在编程的时候经常会碰到字符串分割的问题,这里总结下,也方便我们以后查询使用. 一.用strtok函数进行字符串分割 原 ...
- C#基础 结构体 枚举类型
结构体:就是一个自定义的集合,里面可以放各种类型的元素,用法大体跟集合一样. 一.定义的例子: struct student { public int nianling; public int fen ...
- Linux shell脚本中 数组的声明:
数组的声明: 1)array[key]=value # array[0]=one,array[1]=two 复制代码 2)declare -a array # array被当作数组名 复制代码 3)a ...
- C++学习笔记(一)之指针
指向指针的引用 ; int * p; int *&r = p; //r为对指针p的引用 r = &i; //r为对p的引用,故对r赋值即将p指向i *r = ; //更新i的值 通过* ...
- 动态排序JavaBean
Java中如果对对象排序可以考虑实现Comparable接口,但是需要排序的属性一旦指定就不能再修改.BeanUtils组件提供了对JavaBean动态排序的支持,即可以在运行时指定排序的属性.实例运 ...
- struts2之actionSupport学习
actionSupport在手工完成字段验证,显示错误消息,国际化等情况下推荐使用.
- Java 基础入门随笔(8) JavaSE版——静态static
面向对象(2) this:代表对象.代表哪个对象呢?当前对象. 当成员变量和局部变量重名,可以用关键字this来区分. this就是所在函数所属对象的引用.(简单说:哪个对象调用了this所在的函数, ...