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]}.注意下判断是否需要经过商业线.输 ...
随机推荐
- Akka源码分析-Cluster-DistributedData
上一篇博客我们研究了集群的分片源码,虽然akka的集群分片的初衷是用来解决actor分布的,但如果我们稍加改造就可以很轻松的开发出一个简单的分布式缓存系统,怎么做?哈哈很简单啊,实体actor的id就 ...
- (DP)51NOD 1118 机器人走方格
M * N的方格,一个机器人从左上走到右下,只能向右或向下走.有多少种不同的走法?由于方法数量可能很大,只需要输出Mod 10^9 + 7的结果. Input 第1行,2个数M,N,中间用空格隔开.( ...
- hexo简易脚本
!/bin/bash 检查是否为master分支.目录是否正确 function git-branch-name { git symbolic-ref --short -q HEAD } functi ...
- [Usaco2007 Mar]Gold Balanced Lineup 平衡的队列
Description N(1<=N<=100000)头牛,一共K(1<=K<=30)种特色,每头牛有多种特色,用二进制01表示它的特色ID.比如特色ID为13(1101),则 ...
- 如何快速部署Oracle Database
Oracle Database在Linux系统上的安装是每一个初学者都必须面临的问题,只有正确的配置好了环境,才能进行后续的深入学习.本文旨在说明如何快速的部署Oracle的单实例环境,对于初学者,还 ...
- JavaScript 把函数作为参数进行传值
JavaScript 响应式编程模式有点类似 WebForm 中的事件驱动模式(传相应的处理函数给委托,通过事件来触发来进行相关的处理),在 AngularJs 2.x 框架中,应用了 RxJS AP ...
- JD笔试
题目表述: 给定n道题目,以及每道题目答对的概率,问小明能及格的概率. 样例: 40 50 50 50 50 0.31250 思路: 递归枚举对的题目个数,最后TLE之过40%: 知道正确解法是DP, ...
- 牛客网-3 网易编程题(1拓扑&2二叉树的公共最近祖先&3快排找第K大数)
1. 小明陪小红去看钻石,他们从一堆钻石中随机抽取两颗并比较她们的重量.这些钻石的重量各不相同.在他们们比较了一段时间后,它们看中了两颗钻石g1和g2.现在请你根据之前比较的信息判断这两颗钻石的哪颗更 ...
- SQLServer2005 维护计划 无法删除
1.查看"维护计划"对象的ID use msdbselect * from sysmaintplan_plansselect * from sysmaintplan_logsele ...
- js解析地址栏参数
/** * 获取地址栏中url后面拼接的参数 * eg: * 浏览器地址栏中的地址:http://1.1.1.1/test.html?owner=2db08226-e2fa-426c-91a1-66e ...