BNUOJ 19792 Airport Express

Airport Express

Time Limit: 1000ms

Memory Limit: 131072KB

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 NS and E (2 ≤ N ≤ 500, 1 ≤ SE ≤ 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 XY and Z (XY ≤ 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

解题:双向求经济快车线路最短路+枚举每条商务快车线路

  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cstring>
  4 #include <cmath>
  5 #include <algorithm>
  6 #include <climits>
  7 #include <vector>
  8 #include <queue>
  9 #include <cstdlib>
 10 #include <string>
 11 #include <set>
 12 #include <stack>
 13 #define LL long long
 14 #define pii pair<int,int>
 15 #define INF 0x3f3f3f3f
 16 using namespace std;
 17 const int maxn = 510;
 18 int mp[maxn][maxn];
 19 int N,S,E;
 20 vector<int>g[maxn];
 21 vector<int>path;
 22 priority_queue< pii,vector< pii >,greater< pii > >q;
 23 struct Dijkstra{
 24     int d[maxn],p[maxn];
 25     bool done[maxn];
 26     void init(){
 27         while(!q.empty()) q.pop();
 28         for(int i = 0; i <= N; i++){
 29             done[i] = false;
 30             d[i] = INF;
 31             p[i] = -1;
 32         }
 33     }
 34     void go(int s){
 35         d[s] = 0;
 36         q.push(make_pair(d[s],s));
 37         while(!q.empty()){
 38             int u = q.top().second;
 39             q.pop();
 40             if(done[u]) continue;
 41             done[u] = true;
 42             for(int i = 0; i < g[u].size(); i++){
 43                 if(d[g[u][i]] > d[u]+mp[u][g[u][i]]){
 44                     d[g[u][i]] = d[u]+mp[u][g[u][i]];
 45                     p[g[u][i]] = u;
 46                     q.push(make_pair(d[g[u][i]],g[u][i]));
 47                 }
 48             }
 49         }
 50     }
 51     void getPath(vector<int>&path,int s,int e){
 52         while(true){
 53             path.push_back(e);
 54             if(e == s) break;
 55             e = p[e];
 56         }
 57     }
 58 };
 59 Dijkstra o[2];
 60 int main() {
 61     int m,i,j,u,v,w,ans,x,y,k,cs = 0;
 62     while(~scanf("%d %d %d",&N,&S,&E)){
 63         if(cs++) puts("");
 64         for(i = 0; i <= N; i++){
 65             g[i].clear();
 66             for(j = 0; j <= N; j++)
 67                 mp[i][j] = INF;
 68         }
 69         scanf("%d",&m);
 70         for(i = 0; i < m; i++){
 71             scanf("%d %d %d",&u,&v,&w);
 72             if(mp[u][v] == INF){
 73                 g[u].push_back(v);
 74                 g[v].push_back(u);
 75             }
 76             if(w < mp[u][v]) mp[u][v] = mp[v][u] = w;
 77         }
 78         o[0].init();
 79         o[0].go(S);
 80         o[1].init();
 81         o[1].go(E);
 82         ans = o[0].d[E];
 83         x = y = -1;
 84         scanf("%d",&k);
 85         for(i = 0; i < k; i++){
 86             scanf("%d %d %d",&u,&v,&w);
 87             if(ans > o[0].d[u]+o[1].d[v]+w){
 88                 ans = o[0].d[u]+o[1].d[v]+w;
 89                 x = u;
 90                 y = v;
 91             }
 92             if(ans > o[0].d[v]+o[1].d[u]+w){
 93                 ans = o[0].d[v]+o[1].d[u]+w;
 94                 x = v;
 95                 y = u;
 96             }
 97         }
 98         path.clear();
 99         if(x == -1){
100             o[0].getPath(path,S,E);
101             reverse(path.begin(),path.end());
102             for(i = 0; i < path.size(); i++){
103                 printf("%d",path[i]);
104                 if(i+1 < path.size()) putchar(‘ ‘);
105                 else putchar(‘\n‘);
106             }
107             puts("Ticket Not Used");
108             printf("%d\n",ans);
109         }else{
110             o[0].getPath(path,S,x);
111             reverse(path.begin(),path.end());
112             o[1].getPath(path,E,y);
113             for(i = 0; i < path.size(); i++){
114                 printf("%d",path[i]);
115                 if(i+1 < path.size()) putchar(‘ ‘);
116                 else putchar(‘\n‘);
117             }
118             printf("%d\n%d\n",x,ans);
119         }
120     }
121     return 0;
122 }

BNUOJ 19792 Airport Express

时间: 2024-10-10 15:30:58

BNUOJ 19792 Airport Express的相关文章

UVA 11374 Airport Express 机场快线 Dijistra+路径

题目链接:UVA 11374 Airport Express Airport Express Time Limit: 1000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu [Submit]   [Go Back]   [Status] Description Problem D: Airport Express In a small city called Iokh, a train service, Airport-Expr

UVA 11374 Airport Express(优先队列优化dijstra + 枚举)

UVA Airport Express 题意:在Iokh市机场快线分为经济线和商业线.线路和速度价格都不同.你只有一张商业线车票,即最多只能坐一站商业线,其他时候只能坐经济线.找出一条去机场最快的线路. 思路:因为商业线只能坐一站,假如乘坐一条商业线(a,b),那么起点到a,b到终点都必须是最短路.所以先预处理起点和终点到其他所有点的最短路,分别记为f()和g(),两次dijstra即可.那么我们只需枚举每条商业线求出所有的f(a)+T(a,b)+g(b)然后求最小即可. 1w是TLE,改成了优

Airport Express UVA - 11374(dijkstra)

Airport Express UVA - 11374 题意:n个点,有m条普通路径,k条高速路径,但是k条只能选一条走.问从s到e最短时间. 如果选a-->b这条高速,那么s-->a和b--->e必然也要是最短路. 于是我们可以先用两次dijkstra预处理出s到各点的最短路和e到各点的最短路,然后枚举k条高速走哪条. 输出路径的时候,可以递归~ 1 #include <bits/stdc++.h> 2 using namespace std; 3 const int in

uva 11374 Airport Express(最短路)

uva 11374 Airport Express 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

UVA 11374 - Airport Express(dijstra)

UVA 11374 - Airport Express 题目链接 题意:给定一些经济线,和一些商务线,商务线最多坐一次,每个线有一个时间,问最短时间 思路:从起点,终点各做一次dijstra,然后枚举商务线,就可以算出总时间,最后求出总时间最少 代码: #include <cstdio> #include <cstring> #include <vector> #include <queue> using namespace std; #define INF

uva 11374 Airport Express(spfa 邻接表+队列)

Problem D: Airport Express 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 theCommercial-Xpress

UVA - 11374 Airport Express (Dijkstra模板+枚举)

Description Problem D: Airport Express 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 Comm

UVA11374 Airport Express

题意:n个点,m条边,两种边,第二种边只能走其中的一条,问起始位置到终点的最短距离 题解:模板题,只要枚举每一条边,预处理两边的最短路,Dijkstra单源最短路 #include <bits/stdc++.h> #define ll long long #define maxn 100010 #define INF 1e9+7 using namespace std; struct edge{ int from,to,dist; }; struct node{ int d,u; bool o

UVA - 11374 Airport Express(dijkstra)

题目大意:机场快线分为经济线和商业线两种,线路,速度和价钱都不同.你有一张商业线车票,可以坐一站商业线,而其他时候只能坐经济线 现在给你起点和终点,要求你找出一条最快的线路 解题思路:不得不说,这题还是有点恶心的 要进行两次的dijkstra,一次以起点为源点,得到的数组d1表示结点和起点最近距离 另一次以终点为源点,得到数组d2,表示结点和终点最近的距离 现在M张商业票,给出的地点为x,y,z 那么有两种选择方式,一种是从起点走到x,然后使用商业票,然后再从y走到终点,那么距离就为 d1[x]