結果

問題 No.848 なかよし旅行
ユーザー Y17Y17
提出日時 2019-07-05 22:33:46
言語 C++11
(gcc 11.4.0)
結果
TLE  
実行時間 -
コード長 1,528 bytes
コンパイル時間 1,525 ms
コンパイル使用メモリ 165,772 KB
実行使用メモリ 24,484 KB
最終ジャッジ日時 2024-04-16 07:49:47
合計ジャッジ時間 8,193 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 TLE -
testcase_01 -- -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

#define int long long

vector<pair<int, int> > graph[2010];

int n, m, p, q, t;
int dist[2010][2010];

void min_dist(int st){
    for(int i = 0;i < n;i++){
        dist[st][i] = LLONG_MAX/4;
    }

    dist[st][st] = 0;
    priority_queue<pair<int, int>, vector<pair<int, int> >, greater<pair<int, int> > > que;
    que.push(make_pair(0, st));

    while(!que.empty()){
        int cost = que.top().first;
        int idx = que.top().second;
        que.pop();

        for(int i = 0;i < graph[idx].size();i++){
            int next = graph[idx][i].first;
            int ncost = graph[idx][i].second + cost;
            if(dist[st][next] > ncost){
                dist[st][next] = ncost;
                que.push(make_pair(ncost, next));
            }
        }
    }

    return;
}

signed main(){
    cin >> n >> m >> p >> q >> t;

    p--;
    q--;

    int a, b, c;
    for(int i = 0;i < m;i++){
        cin >> a >> b >> c;
        a--;
        b--;
        graph[a].push_back(make_pair(b, c));
        graph[b].push_back(make_pair(a, c));
    }

    for(int i = 0;i < n;i++) min_dist(i);


    if(dist[0][p] + dist[p][q] + dist[q][0] <= t){
        cout << t << endl;
        return 0;
    }

    int ans = -1;
    for(int i = 0;i < n;i++){
        if(i != q && i != p){
            int ma = max(dist[i][q]*2, dist[i][p]*2);
            if(dist[0][i]*2 + ma <= t){
                ans = max(ans, t-ma);
            }
        }
    }

    cout << ans << endl;

    return 0;
}
0