• 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('
    ');
    106             }
    107             puts("Ticket Not Used");
    108             printf("%d
    ",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('
    ');
    117             }
    118             printf("%d
    %d
    ",x,ans);
    119         }
    120     }
    121     return 0;
    122 }
    View Code
  • 相关阅读:
    Java中的System类
    关于Java IO流学习总结
    Java InputStream、String、File相互转化
    Java 里把 InputStream 转换成 String 的几种方法
    详细讲解JAVA中的IO流
    获取 request 中 json 数据
    oracle count 百万级 分页查询记要总数、总条数优化
    ORACLE分页SQL语句
    ORACLE中用rownum分页并排序的SQL语句
    day7
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/3919308.html
Copyright © 2020-2023  润新知