uva 11374 Airport Express(最短路),11374airport

來源:互聯網
上載者:User

uva 11374 Airport Express(最短路),11374airport
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
4
1 2 2
1 3 3
2 4 4
3 4 5
1
2 4 3

Sample Output

1 2 4
2
5

題目大意:有兩種車票,商務車票和經濟車票。由於經濟原因,商務車票只能買一張,經濟車票可以買多張。車票都是雙向的。現在問從起點到終點,的最短路徑,以及商務車票在哪個網站用最划算和最後花費的總時間。在不使用商務車票的情況下計算兩次最短路,分別算出起點s到每一點的最短路ds[N], 以及終點t到每一點的最短路dt[N],並記錄起點到終點的最短路Min。然後枚舉商務車票(商務車票初始網站u, 終止網站v, 花費cos),使Min = min(ds[u] + dt[v] + cos, ds[v] + dt[u] + cos),並記錄下使用的商務車票的u和v。維護過後的Min就是在最優的情況下使用商務車票的最短路花費,然後根據pre數組,以及記錄的u和v輸出路徑。
#include <cstdio>#include <cstring>#include <queue>using namespace std;const int N = 505;const int M = 5005;const int INF = 0x3f3f3f3f;typedef long long ll;int n, s, t;int vis[N], d[2][N], en;int head[M];int pre[2][N];struct node {      int to, dis, next;  }edge[N * M];  void init();void addEdge(int u,int v,int x) {      edge[en].to = v;      edge[en].next = head[u];      edge[en].dis = x;      head[u] = en++;      edge[en].to = u;      edge[en].next = head[v];      edge[en].dis = x;      head[v] = en++;  }  void SPFA(int flag) {      queue<int> Q;      for(int i = 1; i <= n; i++) {          d[flag][i] = INF;          vis[i] = 0;          pre[flag][i] = -1;      }      d[flag][s] = 0;      vis[s] = 1;      pre[flag][s] = s;      Q.push(s);      while(!Q.empty()) {          int u = Q.front();          vis[u] = 0;          Q.pop();          for(int i = head[u]; i != -1; i = edge[i].next) {              int v = edge[i].to;              if(d[flag][u] + edge[i].dis < d[flag][v]) {                  d[flag][v] = d[flag][u] + edge[i].dis;                  pre[flag][v] = u;                  if(!vis[v]) {                      Q.push(v);                      vis[v] = 1;                  }              }          }      }  } void input() {    int num, a, b, c;       scanf("%d", &num);    for (int i = 0; i < num; i++) {        scanf("%d %d %d", &a, &b, &c);          addEdge(a, b, c);    }}void print(int x) {    if (pre[0][x] == x) {        printf("%d", x);            return;    }    print(pre[0][x]);    printf(" %d", x);}void solve() {    int num, a, b, c;    int Min = d[0][s], tU = -1, tV = -1;    scanf("%d", &num);    for (int i = 0; i < num; i++) {        scanf("%d %d %d", &a, &b, &c);          if (d[0][a] + d[1][b] + c < Min) {            Min = d[0][a] + d[1][b] + c;            tU = a, tV = b;        }        if (d[0][b] + d[1][a] + c < Min) {            Min = d[0][b] + d[1][a] + c;            tU = b, tV = a;        }    }    if (tU == -1) {        print(s);        puts("");           printf("Ticket Not Used\n");    } else {        print(tU);        for (int i = tV; i != s; i = pre[1][i]) printf(" %d", i);        printf(" %d\n", s);        printf("%d\n", tU);    }    printf("%d\n", Min);}int main() {    int Case = 1;    while (scanf("%d %d %d", &n, &s ,&t) == 3) {        memset(head, -1, sizeof(head));        if (Case != 1) puts("");        Case++;        input();                    SPFA(0);        s = t;        SPFA(1);        solve();        }       return 0;}

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.