码迷,mamicode.com
首页 > 其他好文 > 详细

uva_11374_Airport Express(最短路+枚举)

时间:2015-05-12 09:25:59      阅读:95      评论:0      收藏:0      [点我收藏+]

标签:acm   algorithm   uva   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, EN), 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, YN, 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

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


题意:机场分为经济线和商业线两种。假设你现在有一张商业线的火车票,可以坐一站商业线,其他的时候只能坐经济线,现在你的任务是找一条去机场的最快的路线。

分析:最短路问题。首先用起点和终点都跑一次dijkstra得到起点到任意一点(Sdis[])和任意一点到终点(Edis[])的最短路径,然后对每一个商业线进行枚举,但是注意的是题目中的两点间事双向的,所以枚举的时候要枚举两次,即ans=min(ans,Sdis[X]+Z+Edis[Y]),ans=min(ans,Sdis[Y]+Z+Edis[X]);说到这题目大概处理完了,不过还有一个注意点,就是题目中说了每两个样例的答案之间输出一个空格(注:在这个地方wa了几次也是心碎,uva不会判PE,直接判WA,也是Orz地五体投地了,Orz……)

题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=22966

代码清单:

#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<ctime>
#include<cctype>
#include<string>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;

const int maxv = 500 + 5;
const int max_dis = 999999999;
struct Edge{
    int to;
    int dis;
    Edge(int to,int dis){
        this -> to = to;
        this -> dis = dis;
    }
};

int N,S,E,M,K;
int X,Y,Z,cases=0;

vector<Edge>G[maxv];
int Sdis[maxv],Edis[maxv];
int Shead[maxv],Ehead[maxv];
int path[maxv],ans,Start,End,cnt;
typedef pair<int,int>P;

void init(){
    Start=-1; cnt=0;
    for(int i=0;i<maxv;i++) G[i].clear();
    memset(Shead,-1,sizeof(Shead));
    memset(Ehead,-1,sizeof(Ehead));
    fill(Sdis+1,Sdis+1+N,max_dis);
    fill(Edis+1,Edis+1+N,max_dis);
}

void dijkstra(int dis[],int head[],int s){
    priority_queue<P,vector<P>,greater<P> >q;
    while(!q.empty()) q.pop();
    dis[s]=0;
    q.push(P(0,s));
    while(!q.empty()){
        P p=q.top(); q.pop();
        int v=p.second;
        if(p.first>dis[v]) continue;
        for(int i=0;i<G[v].size();i++){
            Edge& e=G[v][i];
            if(dis[v]+e.dis<dis[e.to]){
                head[e.to]=v;
                dis[e.to]=dis[v]+e.dis;
                q.push(P(dis[e.to],e.to));
            }
        }
    }
}

void doit(){
    dijkstra(Sdis,Shead,S);
    dijkstra(Edis,Ehead,E);
    ans=Sdis[E];
}


int main(){
    while(scanf("%d%d%d",&N,&S,&E)!=EOF){
        init();
        scanf("%d",&M);
        for(int i=0;i<M;i++){
            scanf("%d%d%d",&X,&Y,&Z);
            G[X].push_back(Edge(Y,Z));
            G[Y].push_back(Edge(X,Z));
        }
        doit();
        scanf("%d",&K);
        for(int i=0;i<K;i++){
            scanf("%d%d%d",&X,&Y,&Z);
            if(Sdis[X]+Edis[Y]+Z<ans){
                Start=X; End=Y;
                ans=Sdis[X]+Edis[Y]+Z;
            }
            if(Sdis[Y]+Edis[X]+Z<ans){
                Start=Y; End=X;
                ans=Sdis[Y]+Edis[X]+Z;
            }
        }

        if(cases) printf("\n");
        cases++;
        if(Start==-1){
            for(int i=E;i!=-1;i=Shead[i]) path[cnt++]=i;
            for(int i=cnt-1;i>=0;i--){
                if(i==0) printf("%d\n",path[i]);
                else printf("%d ",path[i]);
            }
            printf("Ticket Not Used\n%d\n",ans);
        }
        else{
            for(int i=Start;i!=-1;i=Shead[i]) path[cnt++]=i;
            reverse(path,path+cnt);
            for(int i=End;i!=-1;i=Ehead[i]) path[cnt++]=i;
            for(int i=0;i<cnt;i++){
                if(i==cnt-1) printf("%d\n",path[i]);
                else printf("%d ",path[i]);
            }
            printf("%d\n%d\n",Start,ans);
        }
    }return 0;
}


uva_11374_Airport Express(最短路+枚举)

标签:acm   algorithm   uva   dijkstra   枚举   

原文地址:http://blog.csdn.net/jhgkjhg_ugtdk77/article/details/45652575

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!