結果

問題 No.848 なかよし旅行
ユーザー face4face4
提出日時 2019-07-06 20:46:51
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 649 ms / 2,000 ms
コード長 1,969 bytes
コンパイル時間 930 ms
コンパイル使用メモリ 82,940 KB
実行使用メモリ 7,360 KB
最終ジャッジ日時 2024-04-25 13:54:59
合計ジャッジ時間 4,178 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 649 ms
7,360 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 1 ms
6,944 KB
testcase_04 AC 2 ms
6,940 KB
testcase_05 AC 2 ms
6,944 KB
testcase_06 AC 2 ms
6,944 KB
testcase_07 AC 2 ms
6,944 KB
testcase_08 AC 2 ms
6,944 KB
testcase_09 AC 3 ms
6,940 KB
testcase_10 AC 2 ms
6,944 KB
testcase_11 AC 31 ms
6,944 KB
testcase_12 AC 42 ms
6,944 KB
testcase_13 AC 56 ms
6,944 KB
testcase_14 AC 17 ms
6,944 KB
testcase_15 AC 53 ms
6,940 KB
testcase_16 AC 96 ms
6,944 KB
testcase_17 AC 67 ms
6,940 KB
testcase_18 AC 32 ms
6,940 KB
testcase_19 AC 28 ms
6,944 KB
testcase_20 AC 5 ms
6,940 KB
testcase_21 AC 82 ms
6,940 KB
testcase_22 AC 90 ms
6,944 KB
testcase_23 AC 20 ms
6,944 KB
testcase_24 AC 2 ms
6,940 KB
testcase_25 AC 107 ms
6,940 KB
testcase_26 AC 2 ms
6,940 KB
testcase_27 AC 2 ms
6,940 KB
testcase_28 AC 1 ms
6,940 KB
testcase_29 AC 1 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<vector>
#include<queue>
using namespace std;
typedef long long ll;

// (N+M)log(N) + N^2 ?
int main(){
    int n, m, p, q, t;
    cin >> n >> m >> p >> q >> t;
    p--, q--;
    vector<pair<int,int>> v[n];
    while(m-- > 0){
        int a, b, c;
        cin >> a >> b >> c;
        a--, b--;
        v[a].push_back({b, c});
        v[b].push_back({a, c});
    }
    const ll INF = 1ll<<60;
    vector<ll> O(n, INF), P(n, INF), Q(n, INF);
    O[0] = P[p] = Q[q] = 0;
    priority_queue<pair<ll,int>> pq;
    
    pq.push({-0, 0});
    while(!pq.empty()){
        auto x = pq.top();  pq.pop();
        for(auto next : v[x.second]){
            ll nc = -x.first + next.second;
            if(nc < O[next.first]){
                O[next.first] = nc;
                pq.push({-nc, next.first});
            }
        }
    }
    pq.push({-0, p});
    while(!pq.empty()){
        auto x = pq.top();  pq.pop();
        for(auto next : v[x.second]){
            ll nc = -x.first + next.second;
            if(nc < P[next.first]){
                P[next.first] = nc;
                pq.push({-nc, next.first});
            }
        }
    }
    pq.push({-0, q});
    while(!pq.empty()){
        auto x = pq.top();  pq.pop();
        for(auto next : v[x.second]){
            ll nc = -x.first + next.second;
            if(nc < Q[next.first]){
                Q[next.first] = nc;
                pq.push({-nc, next.first});
            }
        }
    }

    if(2*O[p] > t || 2*O[q] > t){
        cout << -1 << endl;
        return 0;
    }

    ll ans = 0;
    if(O[p] + O[q] + P[q] <= t){
        ans = t;
    }

    for(int i = 0; i < n; i++){ // where the two separates
        for(int j = 0; j < n; j++){ // where the two meets
            ll tmp = O[i] + max(P[i] + P[j], Q[i] + Q[j]) + O[j];
            if(tmp <= t){
                ans = max(ans, t-tmp + O[i] + O[j]);
            }
        }
    }

    cout << ans << endl;

    return 0;
}
0