uva 11374 - Airport Express 机场快线 (单源最短路+枚举)

来源:互联网 发布:easyrecovery mac 编辑:程序博客网 时间:2024/05/11 09:06

Problem Description

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
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然后枚举换乘的商业线就可以了。因为只有一张商业票所以枚举时间复杂度O(K*2)再加上两次Diskstra的时间O(2*n^2)总共O(k*2+2*n^2);
题目中要求的是多组数据,下面的代码处理的是单组数据,只需要加上一个while就行了(别忘了把数组初始化);

#include<iostream>#include<algorithm>#include<cstring>#include<vector>#include<queue>#include<stack>using namespace std;struct Edges{        int x;        int y;        int t;};struct heapnode{    int id;    int d;    bool operator <(const heapnode& rhs)const{return d > rhs.d;}};const int maxn = 500+10;const int INF = 1000000000;int N,S,L,M,K;int d[2][maxn],w[maxn][maxn],fa[2][maxn];vector<Edges>E;vector<int>G[maxn];bool done[maxn];void dijkstra(int begin,int no){    priority_queue<heapnode>Q;    fa[no][begin] = -1;    for(int i = 1;i <= N; i++ )d[no][i]=INF;    d[no][begin]=0;    memset(done,0,sizeof(done));    Q.push((heapnode){begin,0});    while(!Q.empty())    {        heapnode x = Q.top();        Q.pop();        int u = x.id;        if(done[u])continue;        done[u] = true;        for(int i = 0;i < G[u].size(); i++ )        {            int v = G[u][i];            if(d[no][v]>d[no][u]+w[u][v])            {                d[no][v]=d[no][u]+w[u][v];                fa[no][v]=u;                Q.push((heapnode){v,d[no][v]});            }        }     }}int main(){    cin >> N >> S >> L;    cin >> M;    for(int i = 0;i < M; i++ )    {        int x,y,z;        cin >> x >> y >> z;        G[x].push_back(y);        G[y].push_back(x);        w[x][y] = z;    }    cin >> K;    for(int i = 0;i < K; i++ )    {        int x,y,z;        cin >> x >> y >> z;        E.push_back((Edges){x,y,z});        E.push_back((Edges){y,x,z});    }    dijkstra(S,0);    dijkstra(L,1);    int ans = d[0][L];    int temp = -1;    for(int i = 0;i < E.size(); i++ )        if(ans > d[0][E[i].x]+d[1][E[i].y]+E[i].t)        {            ans = d[0][E[i].x]+d[1][E[i].y]+E[i].t;            temp = i;        }    int end;    if(temp==-1)        end = L;    else end = E[temp].x;    stack<int>a;    while(fa[0][end]!=-1)    {        a.push(end);        end = fa[0][end];    }    cout << S;    while(!a.empty())    {        cout <<" "<< a.top();        a.pop();    }    if(temp!=-1)    {        int x=E[temp].y;        while(fa[1][x]!=-1)        {            cout <<" "<< x;            x = fa[1][x];        }        cout <<" "<< L <<endl;        cout << E[temp].x << endl;    }    else     {        cout << endl;        cout << "Ticket Not Used"<< endl;    }    cout << ans;    return 0;}

代码有点长,我很渣的原因,见谅;

0 0
原创粉丝点击