結果

問題 No.848 なかよし旅行
ユーザー 🍮かんプリン🍮かんプリン
提出日時 2020-06-14 19:54:37
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 155 ms / 2,000 ms
コード長 2,244 bytes
コンパイル時間 1,387 ms
コンパイル使用メモリ 174,384 KB
実行使用メモリ 18,204 KB
最終ジャッジ日時 2024-04-25 14:16:56
合計ジャッジ時間 3,501 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 155 ms
18,204 KB
testcase_01 AC 1 ms
6,940 KB
testcase_02 AC 1 ms
6,944 KB
testcase_03 AC 1 ms
6,944 KB
testcase_04 AC 1 ms
6,940 KB
testcase_05 AC 1 ms
6,944 KB
testcase_06 AC 1 ms
6,940 KB
testcase_07 AC 1 ms
6,944 KB
testcase_08 AC 2 ms
6,944 KB
testcase_09 AC 3 ms
6,944 KB
testcase_10 AC 2 ms
6,940 KB
testcase_11 AC 37 ms
8,448 KB
testcase_12 AC 48 ms
9,600 KB
testcase_13 AC 66 ms
12,160 KB
testcase_14 AC 18 ms
6,944 KB
testcase_15 AC 64 ms
11,904 KB
testcase_16 AC 112 ms
17,152 KB
testcase_17 AC 77 ms
14,208 KB
testcase_18 AC 37 ms
7,808 KB
testcase_19 AC 31 ms
6,940 KB
testcase_20 AC 10 ms
6,940 KB
testcase_21 AC 94 ms
14,592 KB
testcase_22 AC 100 ms
14,976 KB
testcase_23 AC 20 ms
6,944 KB
testcase_24 AC 2 ms
6,944 KB
testcase_25 AC 128 ms
16,896 KB
testcase_26 AC 2 ms
6,940 KB
testcase_27 AC 1 ms
6,940 KB
testcase_28 AC 2 ms
6,944 KB
testcase_29 AC 1 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/**
 *   @FileName	a.cpp
 *   @Author	kanpurin
 *   @Created	2020.06.14 19:54:32
**/

#include "bits/stdc++.h" 
using namespace std; 
typedef long long ll;




template < typename T >
struct Dijkstra {
private:
    int V;
    struct edge {
        int to;
        T cost;
    };
    vector< vector< edge > > G;
public:
    const T inf = numeric_limits< T >::max();
    
    
    vector< T > d;  
    Dijkstra(int V) : V(V) {
        G.resize(V);
    }
    
    
    void add_edge(int from, int to, T weight, bool directed = false) {
        G[from].push_back({to, weight});
        if (!directed) G[to].push_back({from, weight});
    }
    void build(int s) {
        d.assign(V, inf);           
        typedef tuple< T, int > P;  
        priority_queue< P, vector< P >, greater< P > > pq;
        d[s] = 0;             
        pq.push(P(d[s], s));  
        while (!pq.empty()) {
            P p = pq.top();
            pq.pop();
            int v = get< 1 >(p);
            
            if (d[v] < get< 0 >(p)) continue;  
            for (const edge &e : G[v]) {
                
                if (d[e.to] > d[v] + e.cost) {
                    d[e.to] = d[v] + e.cost;
                    pq.push(P(d[e.to], e.to));
                }
            }
        }
    }
};
int main() {
    int n, m, p, q, t;
    cin >> n >> m >> p >> q >> t;
    p--;
    q--;
    Dijkstra< ll > g1(n), gp(n), gq(n);
    for (int i = 0; i < m; i++) {
        int u, v, c;
        cin >> u >> v >> c;
        u--;
        v--;
        g1.add_edge(u, v, c);
        gp.add_edge(u, v, c);
        gq.add_edge(u, v, c);
    }
    g1.build(0);
    gp.build(p);
    gq.build(q);
    if (g1.d[p] + gp.d[q] + gq.d[0] <= t) {
        cout << t << endl;
        return 0;
    }
    ll ans = -1;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            ll t11 = g1.d[i];
            ll tp1 = gp.d[i];
            ll tq1 = gq.d[i];
            ll t12 = g1.d[j];
            ll tp2 = gp.d[j];
            ll tq2 = gq.d[j];
            if (t11 + tp1 + tp2 + t12 > t) continue;
            if (t11 + tq1 + tq2 + t12 > t) continue;
            ans = max(ans,t - max(tp1 + tp2,tq1 + tq2));
        }
    }
    cout << ans << endl;
    return 0;
}
0