• Airport Express UVA


    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.

    题解:

      这道题思路还是十分巧妙,考虑如果使用商业票的路径,一定是从原点出发,跑一条最短路到某条边的端点,经过这条边然后再走最短路到终点,那么既然只有一条边就可以用枚举这条边,我们考虑预处理出起点和终点的最短路,就可以O1算解了,当然这个题目可以考虑分层图的做法,而且输出时候也方便一些,格式十分繁琐,看代码吧,不让会一直格式错误。

    代码:

    #include<iostream>
    #include<stdio.h>
    #include<algorithm>
    #include<cstring>
    #include<stdlib.h>
    #include<queue>
    #define ll long long
    #define MAXN 3000
    using namespace std;
    int dis[2][MAXN],pre[2][MAXN],have[MAXN],hh[MAXN];
    int n,m,k,s,t,num=0;
    queue<int> q;
    struct edge{
        int first;
        int next;
        int to;
        int quan;
    }a[MAXN*2];
    
    void addedge(int from,int to,int quan){
        a[++num].to=to;
        a[num].quan=quan;
        a[num].next=a[from].first;
        a[from].first=num;
    }
    
    void spfa(int s,int dis[],int pre[]){
        for(int i=1;i<=n;i++) have[i]=pre[i]=0,dis[i]=1<<30;
        while(!q.empty()) q.pop();
        dis[s]=0;q.push(s);
        while(!q.empty()){
            int now=q.front();
            q.pop();
            have[now]=0;
            for(int i=a[now].first;i;i=a[i].next){
                int to=a[i].to,quan=a[i].quan;
                if(dis[to]>dis[now]+quan){
                    dis[to]=dis[now]+quan;
                    pre[to]=now;
                    if(!have[to]){
                        have[to]=1;
                        q.push(to);
                    }
                }
            }
        }
    }
    
    void print(int ss){
        memset(hh,0,sizeof(hh));int numm=0;
        for(int now=ss;pre[0][now]!=0;now=pre[0][now]){
            hh[++numm]=now;
        }
        hh[++numm]=s;
        for(int i=numm;i>=2;i--) printf("%d ",hh[i]);printf("%d",hh[1]); 
    }
    
    int main(){
        int Case=0;
        while(scanf("%d%d%d",&n,&s,&t)!=EOF){
            if(Case++) printf("
    ");
            memset(a,0,sizeof(a)),num=0;
            scanf("%d",&m);
            for(int i=1;i<=m;i++){
                int x,y,z;scanf("%d%d%d",&x,&y,&z);
                addedge(x,y,z),addedge(y,x,z);
            }
            spfa(s,dis[0],pre[0]),spfa(t,dis[1],pre[1]);
            scanf("%d",&k);
            int ans=dis[0][t],from=0,to=0;
            for(int i=1;i<=k;i++){
                int x,y,z;scanf("%d%d%d",&x,&y,&z);
                if((ll)ans>(ll)dis[0][x]+(ll)z+(ll)dis[1][y]){
                    ans=dis[0][x]+z+dis[1][y],from=x,to=y;
                }
                if((ll)ans>(ll)dis[0][y]+(ll)z+(ll)dis[1][x]){
                    ans=dis[0][y]+z+dis[1][x],from=y,to=x;
                }
            }
            if(!from){
                print(t);
                printf("
    Ticket Not Used
    ");
                printf("%d
    ",dis[0][t]);
            }
            else{
                print(from);printf(" ");
                for(int now=to;pre[1][now]!=0;now=pre[1][now]){
                    printf("%d ",now);
                }
                printf("%d",t);
                printf("
    %d
    ",from);
                printf("%d
    ",ans);
            }
        }
    } 
  • 相关阅读:
    WP7 可以在XAML中使用的 缓存图片控件(适合静态)
    wp7蛋疼的90M内存限制,优化图片使用内存(4)[解决]
    Caliburn.Micro for wp7 学习笔记(2) : 响应 Butto事件
    Caliburn.Micro for wp7 学习笔记(3) : 响应 Butto事件原理
    Caliburn.Micro for wp7 学习笔记(1) : 建立 Caliburn.Micro 框架的 wp7 项目
    anthem.net 简单学习
    新公司研发能力低下,何去何从?
    C/C++指针和取地址操作
    利用bochs 调试(转)
    winform模式对话框和非模式对话框
  • 原文地址:https://www.cnblogs.com/renjianshige/p/7467699.html
Copyright © 2020-2023  润新知