結果

問題 No.848 なかよし旅行
ユーザー firiexpfiriexp
提出日時 2019-08-29 23:51:25
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 94 ms / 2,000 ms
コード長 1,980 bytes
コンパイル時間 1,217 ms
コンパイル使用メモリ 107,996 KB
実行使用メモリ 10,688 KB
最終ジャッジ日時 2023-08-09 21:18:00
合計ジャッジ時間 3,132 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 94 ms
10,688 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 2 ms
4,384 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 19 ms
5,300 KB
testcase_12 AC 24 ms
5,680 KB
testcase_13 AC 32 ms
6,576 KB
testcase_14 AC 13 ms
4,380 KB
testcase_15 AC 30 ms
6,544 KB
testcase_16 AC 48 ms
8,172 KB
testcase_17 AC 33 ms
7,208 KB
testcase_18 AC 18 ms
5,224 KB
testcase_19 AC 16 ms
4,556 KB
testcase_20 AC 3 ms
4,380 KB
testcase_21 AC 39 ms
7,304 KB
testcase_22 AC 37 ms
7,376 KB
testcase_23 AC 9 ms
4,380 KB
testcase_24 AC 1 ms
4,376 KB
testcase_25 AC 51 ms
8,044 KB
testcase_26 AC 2 ms
4,376 KB
testcase_27 AC 2 ms
4,380 KB
testcase_28 AC 1 ms
4,376 KB
testcase_29 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <limits>
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <bitset>
#include <cmath>

static const int MOD = 1000000007;
using ll = long long;
using u32 = uint32_t;
using namespace std;

template<class T> constexpr T INF = ::numeric_limits<T>::max()/32*15+208;

template <typename T>
struct edge {
    int from, to; T cost;
    edge(int to, T cost) : from(-1), to(to), cost(cost) {}
    edge(int from, int to, T cost) : from(from), to(to), cost(cost) {}
};

template <typename T>
vector<T> dijkstra(int s,vector<vector<edge<T>>> &G){
    size_t n=G.size();
    vector<T> d(n, INF<T>);
    priority_queue<pair<T, int>,vector<pair<T, int>>,greater<>> Q;
    d[s]=0;
    Q.emplace(0,s);
    while(!Q.empty()){
        T cost; int i;
        tie(cost, i) = Q.top(); Q.pop();
        if(d[i] < cost) continue;
        for (auto &&e : G[i]) {
            auto cost2 = cost + e.cost;
            if(d[e.to] <= cost2) continue;
            d[e.to] = cost2;
            Q.emplace(d[e.to], e.to);
        }
    }
    return d;
}

int main() {
    int n, m, p, q, t;
    cin >> n >> m >> p >> q >> t;
    p--; q--;
    vector<vector<edge<ll>>> G(n);
    for (int i = 0; i < m; ++i) {
        int a, b, c;
        scanf("%d %d %d", &a, &b, &c);
        a--; b--;
        G[a].emplace_back(b, c);
        G[b].emplace_back(a, c);
    }
    auto d1 = dijkstra(0, G), dp = dijkstra(p, G), dq = dijkstra(q, G);
    if(d1[p]+dp[q]+dq[0] <= t){
        cout << t << "\n";
        return 0;
    }else if(2*max(d1[q], d1[p]) > t){
        cout << -1 << "\n";
        return 0;
    }
    ll ans = INF<ll>;
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            if(d1[i]+max(dp[i]+dp[j], dq[i]+dq[j])+d1[j] <= t){
                ans = min({ans, max(dp[i]+dp[j], dq[i]+dq[j])});
            }
        }
    }
    cout << max(-1LL, t-ans) << "\n";
    return 0;
}
0