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

1 2 2 
1 3 3 
2 4 4 
3 4 5 

2 4 3

Sample Output

1 2 4 

5

 

题意:从家去机场有两种方式,一个是坐经济线,另一个是坐商业线,由于经济有限,最多只能够坐一站商业线,问从家到机场花费的最少时间以及路线。

 

思路:提前预处理出从S点和E点到各点的最短路径,然后枚举商业线,设S到各点的最短路径用dist[]表示,E到各点的路径用dis[]表示,商业线的站点和往返时间用x,y,z表示,则可得ans = min(ans,dist[x] + z + dis[y]);

详细代码如下:

 

#include <cstdio>
#include <queue>
#include <cstring>
#include <algorithm>
#define inf 0x3f3f3f3f
using namespace std;

const int maxn = 5010;
struct Edge{
    int next,to,w;
}edge[maxn];
struct HeadNode{
    int dis,u;
    friend bool operator < (HeadNode A, HeadNode B){
        return A.dis > B.dis;
    }
};
int head[maxn],cas=0;
int N,S,E,M,K,cnt,dist[maxn],dis[maxn],vis[maxn],pre[maxn],flag,rpre[maxn];

void add(int u,int v, int w){
    edge[cnt].to = v;
    edge[cnt].w = w;
    edge[cnt].next = head[u];
    head[u] = cnt ++;
}

void dijkstra(int x){
    for(int i = 1; i <= N; i ++) dis[i] = inf;
    memset(vis,0,sizeof(vis));
    memset(pre,-1,sizeof(pre));
    dis[x] = 0;
    priority_queue<HeadNode > q;
    HeadNode no;
    no.dis = 0; no.u = x;
    q.push(no);
    while(!q.empty()){
        no = q.top(); q.pop();
        int u = no.u;
        if(vis[u]) continue;
        vis[u] = 1;
        for(int i = head[u]; i != -1; i = edge[i].next){
            int v = edge[i].to;
            if(dis[v] > dis[u] + edge[i].w){
                dis[v] = dis[u] + edge[i].w;
                pre[v] = u;
                HeadNode nod;
                nod.dis = dis[v]; nod.u = v;
                q.push(nod);
            }
        }
    }
}

void display(int x){
    if(rpre[x] == -1){
        printf("%d",x);
        return ;
    }
    display(rpre[x]);
    printf(" %d",x);
}

int main(){
    while(~scanf("%d%d%d",&N,&S,&E)){
        memset(head,-1,sizeof(head));
        cnt = 0;
        if(cas ++) printf("\n");
        scanf("%d",&M);
        int x,y,z;
        for(int i = 1; i <= M; i ++){
            scanf("%d%d%d",&x,&y,&z);
            add(x,y,z);
            add(y,x,z);
        }
        scanf("%d",&K);
        dijkstra(S);//dijkstra求S到其他各点的最短路径
        for(int i = 1; i <= N; i ++) dist[i] = dis[i];
        for(int i = 1; i <= N; i ++) rpre[i] = pre[i];
        flag = 0;
        dijkstra(E);//dijkstra求E到其他各点的最短路径
        int ans = dist[E], flagy = 0;
        for(int i = 1; i <= K; i ++){
            scanf("%d%d%d",&x,&y,&z);//因为是无向图,所以有两个if判断
            if(ans > dist[x] + z + dis[y]){
                flag = x; flagy = y; ans = dist[x] + z + dis[y];
            }
           if(ans > dis[x] + z + dist[y]){
                flag = y; flagy = x; ans = dist[y] + z + dis[x];
            }
        }
        if(flag){//表示换乘了商业线
            display(flag);//输出路径
            for(int i = flagy; i != E; i = pre[i]) printf(" %d",i);
            printf(" %d\n",E);
            printf("%d\n",flag);
        }
        else{//没有换乘商业线
            display(E);
            printf("\n");
            printf("Ticket Not Used\n");
        }
        printf("%d\n",ans);
    }
    return 0;
}

 

 

 

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值