結果

問題 No.848 なかよし旅行
ユーザー Y17Y17
提出日時 2019-07-05 22:41:39
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 924 ms / 2,000 ms
コード長 1,619 bytes
コンパイル時間 1,579 ms
コンパイル使用メモリ 151,132 KB
実行使用メモリ 13,212 KB
最終ジャッジ日時 2023-08-07 19:41:53
合計ジャッジ時間 5,044 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 924 ms
12,588 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 2 ms
5,644 KB
testcase_09 AC 3 ms
5,620 KB
testcase_10 AC 2 ms
5,504 KB
testcase_11 AC 19 ms
9,248 KB
testcase_12 AC 23 ms
5,708 KB
testcase_13 AC 31 ms
10,416 KB
testcase_14 AC 12 ms
8,156 KB
testcase_15 AC 29 ms
8,152 KB
testcase_16 AC 48 ms
11,896 KB
testcase_17 AC 34 ms
13,212 KB
testcase_18 AC 18 ms
9,216 KB
testcase_19 AC 16 ms
8,780 KB
testcase_20 AC 8 ms
5,644 KB
testcase_21 AC 39 ms
11,168 KB
testcase_22 AC 38 ms
9,436 KB
testcase_23 AC 20 ms
5,600 KB
testcase_24 AC 1 ms
4,380 KB
testcase_25 AC 60 ms
9,824 KB
testcase_26 AC 2 ms
4,380 KB
testcase_27 AC 2 ms
4,376 KB
testcase_28 AC 1 ms
4,380 KB
testcase_29 AC 2 ms
4,380 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