結果

問題 No.848 なかよし旅行
ユーザー rogi52rogi52
提出日時 2022-10-12 22:24:35
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 99 ms / 2,000 ms
コード長 1,362 bytes
コンパイル時間 2,443 ms
コンパイル使用メモリ 210,460 KB
実行使用メモリ 10,684 KB
最終ジャッジ日時 2023-09-08 18:40:31
合計ジャッジ時間 4,040 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 99 ms
10,684 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 19 ms
5,068 KB
testcase_12 AC 24 ms
5,548 KB
testcase_13 AC 32 ms
6,344 KB
testcase_14 AC 14 ms
4,380 KB
testcase_15 AC 29 ms
6,268 KB
testcase_16 AC 47 ms
7,940 KB
testcase_17 AC 32 ms
7,048 KB
testcase_18 AC 19 ms
4,948 KB
testcase_19 AC 17 ms
4,592 KB
testcase_20 AC 12 ms
4,380 KB
testcase_21 AC 37 ms
6,972 KB
testcase_22 AC 33 ms
7,444 KB
testcase_23 AC 11 ms
4,380 KB
testcase_24 AC 1 ms
4,380 KB
testcase_25 AC 50 ms
7,976 KB
testcase_26 AC 1 ms
4,384 KB
testcase_27 AC 2 ms
4,376 KB
testcase_28 AC 2 ms
4,380 KB
testcase_29 AC 1 ms
4,384 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;

// g <- pair < v , cost > 
template < class T >
vector< T > dijkstra(vector<vector<pair<int, T>>> &graph, int s) {
    T INF = numeric_limits< T >::max();
    vector<T> dist(graph.size(), INF);
    priority_queue<pair<T,int>, vector<pair<T,int>>, greater<pair<T,int>>> q;
    q.push({dist[s] = T(0), s});
    while(!q.empty()){
        auto [uc, ui] = q.top(); q.pop();
        if(uc != dist[ui]) continue;
        for(auto [vi, vc] : graph[ui]) if(dist[vi] > uc + vc) 
            q.push({dist[vi] = uc + vc, vi});
    }
    return dist;
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(0);
    
    ll N,M,P,Q,T; cin >> N >> M >> P >> Q >> T; P--, Q--;
    vector<vector<pair<int,ll>>> G(N);
    rep(_,M) {
        int a,b,c; cin >> a >> b >> c; a--, b--;
        G[a].push_back({b, c});
        G[b].push_back({a, c});
    }

    auto dist0 = dijkstra(G, 0);
    auto distP = dijkstra(G, P);
    auto distQ = dijkstra(G, Q);
    ll ans = -1;
    rep(i,N)rep(j,N) {
        if(dist0[i] + max(distP[i] + distP[j], distQ[i] + distQ[j]) + dist0[j] <= T) {
            ans = max(ans, T - max(distP[i] + distP[j], distQ[i] + distQ[j]));
        }
    }

    if(dist0[P] + distP[Q] + distQ[0] <= T) ans = max(ans, T);
    cout << ans << endl;
}
0