結果

問題 No.848 なかよし旅行
ユーザー Y17Y17
提出日時 2019-07-05 22:41:39
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 1,023 ms / 2,000 ms
コード長 1,619 bytes
コンパイル時間 1,575 ms
コンパイル使用メモリ 165,752 KB
実行使用メモリ 12,448 KB
最終ジャッジ日時 2024-04-25 13:50:03
合計ジャッジ時間 4,908 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1,023 ms
12,448 KB
testcase_01 AC 2 ms
6,816 KB
testcase_02 AC 2 ms
6,816 KB
testcase_03 AC 1 ms
6,940 KB
testcase_04 AC 1 ms
6,944 KB
testcase_05 AC 2 ms
6,944 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 2 ms
6,948 KB
testcase_09 AC 3 ms
6,940 KB
testcase_10 AC 3 ms
6,944 KB
testcase_11 AC 20 ms
9,216 KB
testcase_12 AC 25 ms
7,664 KB
testcase_13 AC 34 ms
10,332 KB
testcase_14 AC 15 ms
10,260 KB
testcase_15 AC 31 ms
8,344 KB
testcase_16 AC 51 ms
11,964 KB
testcase_17 AC 37 ms
11,016 KB
testcase_18 AC 20 ms
9,212 KB
testcase_19 AC 17 ms
8,776 KB
testcase_20 AC 11 ms
7,736 KB
testcase_21 AC 41 ms
11,088 KB
testcase_22 AC 41 ms
9,384 KB
testcase_23 AC 10 ms
6,944 KB
testcase_24 AC 2 ms
6,940 KB
testcase_25 AC 55 ms
8,064 KB
testcase_26 AC 1 ms
6,944 KB
testcase_27 AC 2 ms
6,944 KB
testcase_28 AC 2 ms
6,944 KB
testcase_29 AC 2 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

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(){

    ios::sync_with_stdio(false);
    cin.tie(0);

    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));
    }

    min_dist(0);
    min_dist(p);
    min_dist(q);


    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++){
        for(int j = 0;j < n;j++){
            int ma = max(dist[q][i]+dist[q][j], dist[p][i]+dist[p][j]);
            if(dist[0][i]+dist[0][j] + ma <= t){
                ans = max(ans, t-ma);
            }
        }
    }

    cout << ans << endl;

    return 0;
}
0