結果

問題 No.848 なかよし旅行
ユーザー noisy_noiminnoisy_noimin
提出日時 2019-07-05 23:03:41
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 141 ms / 2,000 ms
コード長 2,305 bytes
コンパイル時間 944 ms
コンパイル使用メモリ 101,672 KB
実行使用メモリ 10,744 KB
最終ジャッジ日時 2023-08-07 19:42:05
合計ジャッジ時間 3,357 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 141 ms
10,744 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 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,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 3 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 32 ms
5,320 KB
testcase_12 AC 43 ms
5,532 KB
testcase_13 AC 58 ms
6,492 KB
testcase_14 AC 19 ms
4,380 KB
testcase_15 AC 54 ms
6,380 KB
testcase_16 AC 95 ms
8,016 KB
testcase_17 AC 67 ms
7,172 KB
testcase_18 AC 32 ms
5,032 KB
testcase_19 AC 28 ms
4,628 KB
testcase_20 AC 12 ms
4,380 KB
testcase_21 AC 79 ms
7,216 KB
testcase_22 AC 85 ms
7,612 KB
testcase_23 AC 11 ms
4,376 KB
testcase_24 AC 2 ms
4,380 KB
testcase_25 AC 101 ms
8,152 KB
testcase_26 AC 2 ms
4,376 KB
testcase_27 AC 2 ms
4,376 KB
testcase_28 AC 2 ms
4,380 KB
testcase_29 AC 1 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cassert>
#include <cstdint>
#include <iostream>
#include <iomanip>
#include <string>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <bitset>

using namespace std;

using ll =  long long;
using Pll = pair<ll, ll>;
using Pii = pair<int, int>;

constexpr int MAX_N = 2000;

vector< vector<Pll> > edges(MAX_N);

class Dijkstra {
    public: 
    ll d[MAX_N];
    ll start = -1;

    Dijkstra(ll start): start(start) {
        fill(d, d+MAX_N, 1LL<<30);
        d[start] = 0;

        priority_queue< Pll, vector<Pll>, greater<Pll> > que;
        que.push(Pll(0, start));

        while(!que.empty()){
            Pll v = que.top(); que.pop();
            if(d[v.second] < v.first) continue;
            for(Pll e: edges[v.second]){
                if(d[e.first] > d[v.second] + e.second){
                    d[e.first] = d[v.second] + e.second;
                    que.push(Pll(d[e.first], e.first));
                }
            }
        }
    }

    ll get(ll x) {
        return d[x];
    }
};

int main() {
    int n,m,p,q; ll t;
    cin >> n >> m >> p >> q >> t;
    --p; --q;
    int a,b; ll c;
    for(int i=0;i<m;++i){
        cin >> a >> b >> c;
        --a; --b;
        edges[a].emplace_back(b, c);
        edges[b].emplace_back(a, c);
    }

    Dijkstra d_0 = Dijkstra(0), d_p = Dijkstra(p), d_q = Dijkstra(q);
    assert(d_0.get(p) == d_p.get(0));
    assert(d_p.get(q) == d_q.get(p));
    assert(d_q.get(0) == d_0.get(q));

    ll ans = -1LL;
    if(d_0.get(p) + d_p.get(q) + d_q.get(0) <= t) {
        ans = t;
    } else {
        for(int r1=0;r1<n;++r1) {
            ll tt = 2LL * (d_0.get(r1) + max(d_p.get(r1), d_q.get(r1)));
            if(tt <= t) {
                ans = max(t-2LL*max(d_p.get(r1), d_q.get(r1)), ans);
            }

            for(int r2=0;r2<n;++r2) {
                if(r1 == r2) continue;
                tt = d_0.get(r1) + max(d_p.get(r1)+d_p.get(r2), d_q.get(r1)+d_q.get(r2)) + d_0.get(r2);
                if(tt <= t) {
                    ans = max(t - max(d_p.get(r1)+d_p.get(r2), d_q.get(r1)+d_q.get(r2)), ans);
                }
            }
        }
    }

    cout << ans << endl;
}
0