zoukankan      html  css  js  c++  java
  • UVA 11374 dijkstra预处理+枚举

      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. 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 CommercialXpress 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


    输出格式:

      对于每组数据,输出3行。第一行表示按访问顺序给出经过的各个车站(包括起点和终点),第二行是换乘商业线的车站编号,(如果没有商业险车票,输出Ticket Not Used),第三行输出总时间

    分析:

      商用站只能坐一站,枚举出所有的可能

      比如商用站从a到b,那么从起点到a,从a到终点两条路线都应该是最短的,所以我们只需从七点开始,到终点结束做两条单源最短路,记录从起点开始到每个节点的x的最短时间f(x)和从每个点到终点的最短时间g(x),则总时间为f(a)+T(a,b)+f(b),

    其中T(a,b)表示a到b商用路线所用的时间


    #include <iostream>
    #include <cstdlib>
    #include <cstdio>
    #include <cstring>
    #include <cmath>
    #include <stack>
    #include <vector>
    #include <queue>
    #include <string>
    #include <algorithm>
    
    const int inf = 0x3f3f3f;
    const int MAXN = 5e2+10;
    const int MAXNN = 1e3+10;
    struct edge{
        int st;
        int to;
        int dist;
        int next;
    };
    struct heapnode{    //优先队列节点
        int st;
        int dist;
        bool operator < (const heapnode& rhs)const {
            return dist>rhs.dist;
        }
    };
    using namespace std;
    int first[MAXN];
    edge e[2*MAXNN];
    int top;
    int d1[MAXN];   //->S
    int d2[MAXN];   //->E
    int p1[2*MAXN];
    int p2[2*MAXN];
    int done[MAXN];
    int n,m,k;
    int cs,ce;
     int na,nb;
    
    void init(){
        memset(first,-1,sizeof(first));
        top = 0;
    }
    
    void addedge(int u,int v,int dist){
        e[top].st = u;
        e[top].to = v;
        e[top].dist = dist;
        e[top].next = first[u];
        first[u] = top++;
    }
    
    void dijkstra_s(int s){
        priority_queue<heapnode>Q;
        heapnode a,tmp;
        memset(done,0,sizeof(done));
        for(int i=0;i<n;i++){
            d1[i] = inf;
        }
        d1[s] = 0;
        a.st = s;
        a.dist = 0;
        Q.push(a);
        int u;
        while(!Q.empty()){
            tmp = Q.top();
            Q.pop();
            u = tmp.st;
            if(done[u])continue;
            done[u] = 1;
            for(int i=first[u];i!=-1;i=e[i].next){
                if(d1[e[i].to]>d1[u]+e[i].dist){
                    d1[e[i].to] = d1[u]+e[i].dist;
                    p1[e[i].to] = i;
                    a.st = e[i].to;
                    a.dist = d1[e[i].to];
                    Q.push(a);
                }
            }
        }
    }
    
    void dijkstra_e(int s){ 
        priority_queue<heapnode>Q;
        heapnode a,tmp;
        memset(done,0,sizeof(done));
        for(int i=0;i<n;i++){
            d2[i] = inf;
        }
        d2[s] = 0;
        a.st = s;
        a.dist = 0;
        Q.push(a);
        int u;
        while(!Q.empty()){
            tmp = Q.top();
            Q.pop();
            u = tmp.st;
            if(done[u])continue;
            done[u] = 1;
            for(int i=first[u];i!=-1;i=e[i].next){
                if(d2[e[i].to]>d2[u]+e[i].dist){
                    d2[e[i].to] = d2[u]+e[i].dist;
                    p2[e[i].to] = i;
                    a.st = e[i].to;
                    a.dist = d2[e[i].to];
                    Q.push(a);
                }
            }
        }
    }
    
    void printA(int u){
        if(u==cs){
            cout<<u+1;
            return;
        }
        printA(e[p1[u]].st);
        cout<<" "<<u+1;
    }
    
    void printB(int u){
        if(u==ce){
            cout<<" "<<u+1<<endl;
            return ;
        }
        cout<<" "<<u+1;
        printB(e[p2[u]].st);
    }
    
    int main()
    {
        int a,b,td;
        int mc= 0;
        while(scanf("%d%d%d",&n,&cs,&ce)!=EOF){
            if(mc++)cout<<endl;
            init();
            scanf("%d",&m);
            for(int i=0;i<m;i++){
                scanf("%d%d%d",&a,&b,&td);
                a--;
                b--;
                addedge(a,b,td);
                addedge(b,a,td);
            }
            cs--;
            ce--;
            dijkstra_s(cs);
            dijkstra_e(ce);
            int ans = d1[ce];
            na = nb = -1;
            /*cout<<"****"<<endl;
            cout<<ans<<endl;
            cout<<d2[cs]<<endl;
            cout<<"****"<<endl;*/
            scanf("%d",&k);
            for(int i=0;i<k;i++){
                scanf("%d%d%d",&a,&b,&td);
                a--;
                b--;
                if(d1[a]+d2[b]+td<ans){
                    na = a;
                    nb = b;
                    ans = d1[a]+d2[b]+td;
                }
                if(d1[b]+d2[a]+td<ans){
                    na = b;
                    nb = a;
                    ans = d1[b]+d2[a]+td;
                }
            }
    
            if(na==-1){
                printA(ce);
                cout<<endl;
            }else{
                printA(na);
                printB(nb);
            }
    
            if(ans!=d1[ce]){
                cout<<na+1<<endl;
            }else{
                cout<<"Ticket Not Used"<<endl;
            }
            cout<<ans<<endl;
            //cout<<endl;
        }
    
        //cout << "Hello world!" << endl;
        return 0;
    }
    View Code
    在一个谎言的国度,沉默就是英雄
  • 相关阅读:
    表单元素的required,autocomplete,list用法
    通用triggerEvent方法
    通用addEventListener方法
    关于动态加载js
    Node路由简单的处理
    As3截图转换为ByteArray传送给后台node的一种方法
    JS中的“==”符号及布尔值转换规则
    解释JS变量作用域的范例
    .NET异步委托
    正则表达式入门
  • 原文地址:https://www.cnblogs.com/EdsonLin/p/5472364.html
Copyright © 2011-2022 走看看