UVA 11374 Airport Express


dijkstra+枚举+恶心的格式.....


Airport Express
Time Limit: 1000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu

[Submit]   [Go Back]   [Status]  

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 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 NS and E (2 ≤ N ≤ 500, 1 ≤ SE ≤ 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 XY and Z (XY ≤ 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
4
1 2 2
1 3 3
2 4 4
3 4 5
1
2 4 3

Sample Output

1 2 4
2
5

Problemsetter: Raymond Chun
Originally appeared in CXPC, Feb. 2004

[Submit]   [Go Back]   [Status]  


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>

using namespace std;

const int maxn=12000,INF=0x3f3f3f3f;
typedef pair<int,int> pII;
typedef pair<pII,int> pIII;

struct Edge
{
    int to,next,cost;
}edge[maxn];

int Adj[maxn],Size;

void init()
{
    Size=0; memset(Adj,-1,sizeof(Adj));
}

void Add_Edge(int u,int v,int c)
{
    edge[Size].to=v;
    edge[Size].cost=c;
    edge[Size].next=Adj[u];
    Adj[u]=Size++;
}

int dist_S[maxn],dist_E[maxn],S,E;
int pre_S[maxn],pre_E[maxn];
bool vis[maxn];
int n,M,K;
pIII CX[maxn];

void dijkstra_S()
{
    memset(dist_S,63,sizeof(dist_S));
    memset(vis,false,sizeof(vis));
    memset(pre_S,-1,sizeof(pre_S));
    dist_S[S]=0;

    for(int i=1;i<=n;i++)
    {
        int mark=-1,mindist=INF;
        for(int j=1;j<=n;j++)
        {
            if(!vis[j]&&dist_S[j]<mindist)
            {
                mindist=dist_S[j];
                mark=j;
            }
        }
        if(mark==-1) break;
        vis[mark]=1;
        for(int j=Adj[mark];~j;j=edge[j].next)
        {
            int u=edge[j].to;
            if(!vis[u])
            {
                if(dist_S[u]>dist_S[mark]+edge[j].cost)
                {
                    dist_S[u]=dist_S[mark]+edge[j].cost;
                    pre_S[u]=mark;
                }
            }
        }
    }
}

void dijkstra_E()
{
    memset(dist_E,63,sizeof(dist_E));
    memset(vis,false,sizeof(vis));
    memset(pre_E,-1,sizeof(pre_E));
    dist_E[E]=0;

    for(int i=1;i<=n;i++)
    {
        int mark=-1,mindist=INF;
        for(int j=1;j<=n;j++)
        {
            if(!vis[j]&&dist_E[j]<mindist)
            {
                mindist=dist_E[j];
                mark=j;
            }
        }
        if(mark==-1) break;
        vis[mark]=1;
        for(int j=Adj[mark];~j;j=edge[j].next)
        {
            int u=edge[j].to;
            if(!vis[u])
            {
                if(dist_E[u]>dist_E[mark]+edge[j].cost)
                {
                    dist_E[u]=dist_E[mark]+edge[j].cost;
                    pre_E[u]=mark;
                }
            }
        }
    }
}

vector<int> road;

void next_S(int x)
{
    road.push_back(x);
    if(pre_S[x]!=-1)   next_S(pre_S[x]);
}

void next_E(int x)
{
    road.push_back(x);
    if(pre_E[x]!=-1)   next_E(pre_E[x]);
}

int main()
{
    int cas=0;
    while(scanf("%d%d%d",&n,&S,&E)!=EOF)
    {
        init();
        scanf("%d",&M);
        for(int i=0;i<M;i++)
        {
            int a,b,c;
            scanf("%d%d%d",&a,&b,&c);
            Add_Edge(a,b,c);
            Add_Edge(b,a,c);
        }
        scanf("%d",&K);
        for(int i=0;i<K;i++)
        {
            int a,b,c;
            scanf("%d%d%d",&a,&b,&c);
            CX[i]=make_pair(make_pair(a,b),c);
        }
        if(cas) putchar(10);
        cas++;
        dijkstra_S(); dijkstra_E();
        int ans=dist_S[E],CE=-1;
        int ST=-1,ED=-1;
        for(int i=0;i<K;i++)
        {
            int u=CX[i].first.first;
            int v=CX[i].first.second;
            int c=CX[i].second;
            int temp=dist_S[u]+c+dist_E[v];
            if(ans>temp)
            {
                ST=u; ED=v;
                ans=temp; CE=i;
            }
            temp=dist_S[v]+c+dist_E[u];
            if(ans>temp)
            {
                ST=v; ED=u;
                ans=temp; CE=i;
            }
        }
        road.clear();
        if(ST==-1)
        {
            next_S(E);
            int sz=road.size();
            for(int i=sz-1;i>=0;i--)
            {
                if(i!=sz-1) putchar(32);
                printf("%d",road[i]);
            }
        }
        else
        {
            next_S(ST);
            int sz=road.size();
            for(int i=sz-1;i>=0;i--)
            {
                if(i!=sz-1) putchar(32);
                printf("%d",road[i]);
            }
            road.clear();
            next_E(ED);
            sz=road.size();
            for(int i=0;i<sz;i++)
            {
                putchar(32);
                printf("%d",road[i]);
            }
        }
        if(ST!=-1)  printf("\n%d\n%d\n",ST,ans);
        else printf("\nTicket Not Used\n%d\n",ans);
    }
    return 0;
}





你可能感兴趣的:(UVA 11374 Airport Express)