uva_11374_Airport Express(最短路+枚举)

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;
}
时间: 2024-11-10 21:49:47

uva_11374_Airport Express(最短路+枚举)的相关文章

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

POJ 1161 Walls(最短路+枚举)

POJ 1161 Walls(最短路+枚举) 题目背景 题目大意:题意是说有 n个小镇,他们两两之间可能存在一些墙(不是每两个都有),把整个二维平面分成多个区域,当然这些区域都是一些封闭的多边形(除了最外面的一个),现在,如果某几个小镇上的人想要聚会,为选择哪个区域为聚会地点,可以使他们所有人总共需要穿过的墙数最小,题目上有说明,不在某个点上聚会(聚会点在某个多边形内部),行进过程中不穿过图中的点(也就是除出发点外的其他小镇). 输入第1行代表m(2<=M<=200)个区域 第2行代表n(3&

CJOI 05新年好 (最短路+枚举)

CJOI 05新年好 (最短路+枚举) 重庆城里有n个车站,m条双向公路连接其中的某些车站.每两个车站最多用一条公路连接,从任何一个车站出发都可以经过一条或者多条公路到达其他车站,但不同的路径需要花费的时间可能不同.在一条路径上花费的时间等于路径上所有公路需要的时间之和. 佳佳的家在车站1,他有五个亲戚,分别住在车站a,b,c,d,e.过年了,他需要从自己的家出发,拜访每个亲戚(顺序任意),给他们送去节日的祝福.怎样走,才需要最少的时间? 输入 第一行:n(n<=50,000),m(m<=10

UVALive 6885 Flowery Trails 最短路枚举

题目连接: http://acm.hust.edu.cn/vjudge/problem/visitOriginUrl.action?id=129723 题意: 给你一个n点m图的边 1到n有多条最短路,问你所有经过的边的总和*2是多少 题解: 对1,n分别求单源最短路径上spfa 枚举某条边是否为最短上的边 即 边权+disA[i] + disB[i] = 最短路长度,就是答案 #include<bits/stdc++.h> using namespace std; const int N =

POJ #1062 昂贵的聘礼 有限制的最短路 枚举+dijkstra求最短路

Description 问题:链接 更多的测试用例:POJ DISCUSS 思路 这道题的关键点有两个:图的构建与物品能进行交换的等级区间. 对于前一个问题,需要以探险家作为起始点,设其编号为0,暂且计各物品编号为顶点编号,物品为各顶点,由于探险家可以用金币买各个物品,所以 0 点可以直接到达其余各点,边的权为购买该物品的金币数:而物品 i 与物品 j 间可能进行交换,所以让 i -> j 表示用"物品 j + 物品 i 优惠后的价格 = 物品 i" .而原问题-- "

BNUOJ 19792 Airport Express

Airport Express Time Limit: 1000ms Memory Limit: 131072KB This problem will be judged on UVA. Original ID: 1137464-bit integer IO format: %lld      Java class name: Main In a small city called Iokh, a train service, Airport-Express, takes residents t

图论练习们~

/* hdu 1599 ( find the mincost route ) Floyed求最小环 每个环一定是 由 i j k 构成 假设k是环中的max 要成环 就要保证不是链(md废话) 利用Floyed的最外层循环含义 i-j最短路经过的点编号<k 那个我们在 限制i<j<k 那么f[i][j]+g[i][k]+g[k][j] 一定能构成一个环 并且点数>=3 因为i j k 互不相同 */ #include<iostream> #include<cstd

POJ 1556 The Doors

计算几何+最短路 枚举线段是否相交建图,然后跑最短路 #include<cstdio> #include<cstring> #include<vector> #include<cmath> #include<queue> #include<algorithm> using namespace std; const int maxn=1000+10; const double eps=1e-8; int n; int totP,totL

【2017杭二联考】 图的有向环

P2555 - [2017杭二联考]图的有向环 Description 题目背景: 幻想乡的亡灵公主,西行寺幽幽子,在幻想乡很受欢迎,经常有妖怪来拜访她,但是幽 幽子并不喜欢被打扰,她希望从白玉楼出发,散步之后再回到白玉楼,同时路上遇到的妖怪 越少越好(有趣的是道路两边的妖怪数量并不相同,分别从两个方向经过同一条道路遇到的 妖怪数量是不同的).当然,作为冥界的公主,她是不会重复经过同一条道路的. 问题描述: 给定一个有 n 个点 m 条无向边的图,每条无向边最多只能经过一次. 对于边(ui, v