結果

問題 No.848 なかよし旅行
ユーザー koi_kotyakoi_kotya
提出日時 2019-07-06 04:42:23
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 121 ms / 2,000 ms
コード長 1,880 bytes
コンパイル時間 1,585 ms
コンパイル使用メモリ 172,664 KB
実行使用メモリ 7,216 KB
最終ジャッジ日時 2024-04-25 13:54:09
合計ジャッジ時間 3,522 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 121 ms
7,216 KB
testcase_01 AC 1 ms
6,944 KB
testcase_02 AC 1 ms
6,940 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 1 ms
6,940 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 2 ms
6,944 KB
testcase_07 AC 2 ms
6,940 KB
testcase_08 AC 3 ms
6,944 KB
testcase_09 AC 3 ms
6,944 KB
testcase_10 AC 2 ms
6,940 KB
testcase_11 AC 33 ms
6,944 KB
testcase_12 AC 44 ms
6,944 KB
testcase_13 AC 59 ms
6,944 KB
testcase_14 AC 17 ms
6,940 KB
testcase_15 AC 53 ms
6,940 KB
testcase_16 AC 94 ms
6,944 KB
testcase_17 AC 67 ms
6,944 KB
testcase_18 AC 32 ms
6,944 KB
testcase_19 AC 28 ms
6,940 KB
testcase_20 AC 6 ms
6,944 KB
testcase_21 AC 81 ms
6,940 KB
testcase_22 AC 88 ms
6,944 KB
testcase_23 AC 10 ms
6,944 KB
testcase_24 AC 2 ms
6,940 KB
testcase_25 AC 100 ms
6,940 KB
testcase_26 AC 2 ms
6,944 KB
testcase_27 AC 1 ms
6,944 KB
testcase_28 AC 1 ms
6,944 KB
testcase_29 AC 2 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

typedef long long ll;
typedef pair<int,int> P;

#define p_ary(ary,a,b,i) do { cout << "["; for (int (i) = (a);(i) < (b);++(i)) cout << ary[(i)] << ((b)-1 == (i) ? "" : ", "); cout << "]\n"; } while(0)
#define p_map(map,it) do {cout << "{";for (auto (it) = map.begin();;++(it)) {if ((it) == map.end()) {cout << "}\n";break;}else cout << "" << (it)->first << "=>" << (it)->second << ", ";}}while(0)

int MAX_N = 2010;
ll INF = 1LL<<60;
vector<vector<P>> G(MAX_N);

void dijkstra(int s,vector<ll>& dist) {
    priority_queue<P,vector<P>,greater<P>> que;
    dist[s] = 0;
    que.push(P(0,s));
 
    while (!que.empty()) {
        P p = que.top();
        que.pop();
        int v =  p.second;
        if (dist[v] < p.first) continue;
        for (int i = 0;i < G[v].size();++i) {
            P e = G[v][i];
            if (dist[e.first] > dist[v]+e.second) {
                dist[e.first] = dist[v]+e.second;
                que.push(P(dist[e.first],e.first));
            }
        }
    }
}

int main() {
    int n,m,p,q,t;
    cin >> n >> m >> p >> q >> t;
    for (int i = 0;i < m;++i) {
        int u,v,w;
        cin >> u >> v >> w;
        u--;v--;
        G[u].push_back(P(v,w));
        G[v].push_back(P(u,w));
    }
    p--;q--;
    vector<ll> dist1(n,INF),dist2(n,INF),dist3(n,INF);
    dijkstra(0,dist1);
    dijkstra(p,dist2);
    dijkstra(q,dist3);
    if (dist1[p]*2 > t || dist1[q]*2 > t) {
        cout << -1 << endl;
        return 0;
    }
    if (dist1[p]+dist2[q]+dist3[0] <= t) {
        cout << t << endl;
        return 0;
    }
    ll ans = 0;
    for (int i = 0;i < n;++i) {
        for (int j = 0;j < n;++j) {
            ll d = max(dist2[i]+dist2[j],dist3[i]+dist3[j]);
            if (d+dist1[i]+dist1[j] > t) continue;
            ans = max(ans,t-d);
        }
    }
    cout << ans << endl;
    return 0;
}
0