团体程序设计天梯赛(L3-007 天梯地图 (30 分))

题目:

思路分析:

一个最短路的题目 不过是多个多个关键字

同时要约束其他条件

注意细节(这道题花了2个小时!fwfwfw 

代码实现:

/*
*@Author:   GuoJinlong
*@Language: C++
*/
//#include <bits/stdc++.h>
/*
 *                                                     __----~~~~~~~~~~~------___
 *                                    .  .   ~~//====......          __--~ ~~
 *                    -.            \_|//     |||\\  ~~~~~~::::... /~
 *                 ___-==_       _-~o~  \/    |||  \\            _/~~-
 *         __---~~~.==~||\=_    -_--~/_-~|-   |\\   \\        _/~
 *     _-~~     .=~    |  \\-_    '-~7  /-   /  ||    \      /
 *   .~       .~       |   \\ -_    /  /-   /   ||      \   /
 *  /  ____  /         |     \\ ~-_/  /|- _/   .||       \ /
 *  |~~    ~~|--~~~~--_ \     ~==-/   | \~--===~~        .\
 *           '         ~-|      /|    |-~\~~       __--~~
 *                       |-~~-_/ |    |   ~\_   _-~            /\
 *                            /  \     \__   \/~                \__
 *                        _--~ _/ | .-~~____--~-/                  ~~==.
 *                       ((->/~   '.|||' -_|    ~~-/ ,              . _||
 *                                  -_     ~\      ~~---l__i__i__i--~~_/
 *                                  _-~-__   ~)  \--______________--~~
 *                                //.-~~~-~_--~- |-------~~~~~~~~
 *                                       //.-~~~--\
 *                       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 *
 *                               神兽保佑            永无BUG
 */



typedef pair<int, int> P;
const int maxn = 505;
const int INF = 1 << 30;
struct edge { int to, len, tm; };
int n, m, _s, _t, t[maxn], d[maxn], par[maxn], ans1[maxn], ans2[maxn];
vector<edge> G[maxn];
vector<int> route[2];

void print_route(int u, int i) { if(u != _s) print_route(par[u], i); route[i].push_back(u); }
void t_dijkstra() {
    for(int i = 0; i < n; i++) t[i] = ans1[i] = INF;
    t[_s] = ans1[_s] = 0;
    priority_queue<P, vector<P>, greater<P> > que;
    que.push(P(0, _s));
    while(!que.empty()) {
        P p = que.top(); que.pop();
        int v = p.second;
        if(t[v] < p.first) continue;
        for(edge & e : G[v])
            if(t[e.to] == t[v] + e.tm ? ans1[e.to] > ans1[v] + e.len : t[e.to] > t[v] + e.tm) {
                par[e.to] = v, t[e.to] = t[v] + e.tm, ans1[e.to] = ans1[v] + e.len;
                que.push(P(t[e.to], e.to));
            }
    }
    print_route(_t, 0);
}
void d_dijkstra() {
    for(int i = 0; i < n; i++) d[i] = ans2[i] = INF;
    d[_s] = ans2[_s] = 0;
    priority_queue<P, vector<P>, greater<P> > que;
    que.push(P(0, _s));
    while(!que.empty()) {
        P p = que.top(); que.pop();
        int v = p.second;
        if(d[v] < p.first) continue;
        for(edge & e : G[v])
            if(d[e.to] == d[v] + e.len ? ans2[e.to] > ans2[v] + 1 : d[e.to] > d[v] + e.len) {
                d[e.to] = d[v] + e.len, par[e.to] = v, ans2[e.to] = ans2[v] + 1;
                que.push(P(d[e.to], e.to));
            }
    }
    print_route(_t, 1);
}

int main() {
#ifdef MyTest
    freopen("Sakura.txt", "r", stdin);
#endif
    cin >> n >> m;
    for(int i = 0; i < m; i++) {
        int a, b, f, len, tm;
        cin >> a >> b >> f >> len >> tm;
        G[a].push_back(edge{b, len, tm});
        if(!f) G[b].push_back(edge{a, len, tm});
    }
    cin >> _s >> _t;
    t_dijkstra(), d_dijkstra();
    if(route[0] == route[1]) printf("Time = %d; ", t[_t]);
    else{
        printf("Time = %d: %d", t[_t], _s);
        for(int i = 1; i < route[0].size(); i++) printf(" => %d", route[0][i]);
        putchar('\n');
    }
    printf("Distance = %d: %d",  d[_t], _s);
    for(int i = 1; i < route[1].size(); i++) printf(" => %d", route[1][i]);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/m0_57006708/article/details/121235321