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]}.注意下判断是否需要经过商业线.输 ...
随机推荐
- foxmail地址簿导入thunderbird的乱码问题 (转载)
转自:http://blog.csdn.net/gexueyuan/article/details/9032595 由于foxmail的地址簿格式和thunderbird的格式不一样,另外也存在编码问 ...
- 认识BACnet协议
一.什么是BACnet? BACnet,Building Automation and Control networks的简称,即楼宇自动化与控制网络.是用于智能建筑的通信协议. 一般楼宇自控设备从功 ...
- [Usaco2015 Jan]Moovie Mooving
Description Bessie is out at the movies. Being mischievous as always, she has decided to hide from F ...
- Sql 存储过程动态添加where条件
)= '2,3' )= '' ) if(@bussHallId is not null) set @strWhere = @strWhere + ' and bh.ID in ('+@bussHall ...
- python--12、数据库进阶
SQL语句关键词: #再次不做过多介绍 使用INSERT实现数据的插入 UPDATE实现数据的更新 使用DELETE实现数据的删除 使用SELECT查询数据以及. #示例中department为部门表 ...
- git Eclipse项目不显示当前分支
问题: 在Eclipse中,导入新的git项目,在项目上不再显示当前所处的分支,也不再显示修改了哪些文件 解决: 右键选中项目 --> Team --> Share Project ...
- 学习RFT之:TestObject.find方法的了解与使用
第一部分:了解TestObject.find 一.TestObject.find方法的作用 1.测试过程中动态的找到测试对象(控件.标签等),使我们的测试用例不再依赖RFT自带的对象地图(Object ...
- AI:IPPR的数学表示-CNN稀疏结构进化(Mobile、xception、Shuffle、SE、Dilated、Deformable)
接上一篇:AI:IPPR的数学表示-CNN基础结构进化(Alex.ZF.Inception.Res.InceptionRes). 抄自于各个博客,有大量修改,如有疑问,请移步各个原文..... 前言 ...
- Linux 软件编译、安装、删除
本文学习内容 手动安装软件 手动安装下载源码的软件 源码编译3步骤 deb包-包依赖管理 dekg -l 查看所以安装deb的包 apt-get仓库安装(自动处理依赖问题) 640?wx_fmt=gi ...
- es6常用的语法
刚开始用vue或者react,很多时候我们都会把ES6这个大兄弟加入我们的技术栈中.但是ES6那么多那么多特性,我们需要全部都掌握吗?秉着二八原则,掌握好常用的,有用的这个可以让我们快速起飞. 接下来 ...