結果

問題 No.848 なかよし旅行
ユーザー rogi52
提出日時 2022-10-12 22:24:35
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 100 ms / 2,000 ms
コード長 1,362 bytes
コンパイル時間 2,230 ms
コンパイル使用メモリ 204,736 KB
最終ジャッジ日時 2025-02-08 02:15:57
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 26
権限があれば一括ダウンロードができます

ソースコード

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