9-27 (最短路,两端思路)

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

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

题意:

某个人要从起点到终点, 有m个经济站和k个商业站, 只能乘坐一次商业站, 求最短时间。

代码写的很乱啊。。很多细节开始都没有考虑到,好好回顾一下

#include <bits/stdc++.h>
using namespace std;
const int maxn = 510;
const int INF = 0x3f3f3f3f;
struct edge{
    int to, from, c;
};

struct spfa{
    int d[maxn][maxn];
    bool vis[maxn];
    vector<edge> vec[maxn];
    vector<int> path;
    int n;

    void init(int n){
        this->n = n;
        for(int i = 0; i <= n; i++)
            for(int j = i; j <= n; j++)
                d[i][j] = d[j][i] = INF;
        path.clear();
        for(int i = 0; i <= n; i++) vec[i].clear();
    }

    void add_edge(int u, int v, int c){
        vec[u].push_back(edge{v,u,c});
        vec[v].push_back(edge{u,v,c});
    }

    void solve(int s){
        queue<int>  que;
        memset(vis, 0, sizeof(vis));
        d[s][s] = 0;
        que.push(s);
        vis[s] = true;
        while(!que.empty()){
            int u = que.front();
            que.pop();
            vis[u] = false;
            for(int i = 0; i < vec[u].size(); i++){
                int v = vec[u][i].to;
                if(d[s][v] > d[s][u]+vec[u][i].c){
                    d[s][v] = d[s][u]+vec[u][i].c;
                    if(!vis[v]) que.push(v), vis[v] = true;
                } 
            }
        }
    }

    void print_ans(int s, int v, int flag){
        path.clear();
        int u = v;
        bool f = true;
        while(u != s && f){
            f = false;
            //printf("%d  t\n", u);
            path.push_back(u);
            for(int i=0; i < vec[u].size(); i++){
                int to = vec[u][i].to;
                if(d[s][u] == d[s][to]+vec[u][i].c){
                    u = to;
                    f = true;
                    break;
                }
            }
        }
        path.push_back(s);

        if(flag){
            for(int i = path.size()-1; i>=0; i--){
                printf("%d", path[i]);
                if(i)  printf(" ");
            }
        }
        else{
            for(int i = 0; i < path.size(); i++){
                if(i) printf(" ");
                printf("%d", path[i]);
            }
        }
    }
}sol;

int main()
{
    int n, s, e;
    int m, k, x, y, z;
    int cas = 0;
    while(~scanf("%d%d%d", &n, &s, &e) && n){
        if(cas++) printf("\n");
        scanf("%d", &m);
        sol.init(n);
        for(int i = 0; i < m; i++){
            scanf("%d%d%d", &x, &y, &z);
            sol.add_edge(x, y, z);
        }
        sol.solve(s);
        sol.solve(e);
        int ans = sol.d[s][e];
        int a = s, b = e;
        scanf("%d", &k);
        for(int i = 0; i < k; i++){
            scanf("%d%d%d", &x, &y, &z);
            if(ans > sol.d[s][x]+sol.d[e][y]+z){
                ans = sol.d[s][x]+sol.d[e][y]+z;
                a = x, b = y;
            }
            else if(ans > sol.d[s][y]+sol.d[e][x]+z){
                ans = sol.d[s][y]+sol.d[e][x]+z;
                a = y, b = x;
            }
        }
        if(ans != sol.d[s][e]){
            sol.print_ans(s, a, 1); printf(" ");
            sol.print_ans(e, b, 0);
            printf("\n%d\n", a);
        }
        else{
            sol.print_ans(s, e, 1);
            printf("\nTicket Not Used\n");
        }
        printf("%d\n", ans);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值