結果

問題 No.848 なかよし旅行
ユーザー けーむけーむ
提出日時 2020-03-27 14:19:41
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 96 ms / 2,000 ms
コード長 2,851 bytes
コンパイル時間 1,763 ms
コンパイル使用メモリ 182,052 KB
実行使用メモリ 10,616 KB
最終ジャッジ日時 2023-08-07 20:00:26
合計ジャッジ時間 3,643 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 96 ms
10,616 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,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 3 ms
4,376 KB
testcase_11 AC 19 ms
5,156 KB
testcase_12 AC 24 ms
5,656 KB
testcase_13 AC 31 ms
6,376 KB
testcase_14 AC 13 ms
4,380 KB
testcase_15 AC 28 ms
6,168 KB
testcase_16 AC 46 ms
7,844 KB
testcase_17 AC 33 ms
7,108 KB
testcase_18 AC 18 ms
4,876 KB
testcase_19 AC 16 ms
4,720 KB
testcase_20 AC 10 ms
4,376 KB
testcase_21 AC 37 ms
7,104 KB
testcase_22 AC 35 ms
7,392 KB
testcase_23 AC 10 ms
4,376 KB
testcase_24 AC 2 ms
4,380 KB
testcase_25 AC 48 ms
7,900 KB
testcase_26 AC 2 ms
4,376 KB
testcase_27 AC 1 ms
4,380 KB
testcase_28 AC 2 ms
4,380 KB
testcase_29 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for(ll i = 0, i##_len = (n); i < i##_len; i++)
#define reps(i, s, n) for(ll i = (s), i##_len = (n); i < i##_len; i++)
#define rrep(i, n) for(ll i = (n) - 1; i >= 0; i--)
#define rreps(i, e, n) for(ll i = (n) - 1; i >= (e); i--)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((ll)(x).size())
#define len(x) ((ll)(x).length())

template<class T>
struct Dijkstra {
    const T inf = numeric_limits<T>::max() / 2 - 1;
    vector<T> dist;
    vector<int> path;
    
    Dijkstra() {}
    Dijkstra(int n) {
        graph.resize(n);
        dist.resize(n);
        prev.resize(n);
    }
    
    void add_edge(int from, int to, T cost) {
        graph[from].emplace_back(to, cost);
    }
    
    void get_distance(int from) {
        priority_queue<pair<T, int>, vector<pair<T, int>>, greater<pair<T, int>>> pq;
        fill(dist.begin(), dist.end(), inf);
        fill(prev.begin(), prev.end(), -1);
        dist[from] = 0;
        pq.push(make_pair(0, from));
        while(!pq.empty()) {
            pair<T, int> p = pq.top(); pq.pop();
            if (dist[p.second] < p.first) continue;
            for (auto e : graph[p.second]) {
                if (dist[e.first] > (dist[p.second] + e.second)) {
                    dist[e.first] = dist[p.second] + e.second;
                    prev[e.first] = p.second;
                    pq.push(make_pair(dist[e.first], e.first));
                }
            }
        }
    }
    
    void get_path(int to) {
        path = vector<int>(0);
        for (; to != -1; to = prev[to]) {
            path.push_back(to);
        }
        reverse(path.begin(), path.end());
    }
    
private:
    vector<vector<pair<int, T>>> graph;
    vector<T> prev;
};

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    // ifstream in("input.txt");
    // cin.rdbuf(in.rdbuf());
    ll n, m, p, q, t;
    cin >> n >> m >> p >> q >> t;
    p--; q--;
    Dijkstra<ll> di(n);
    rep(i, m) {
        ll a, b, c;
        cin >> a >> b >> c;
        a--; b--;
        di.add_edge(a, b, c);
        di.add_edge(b, a, c);
    }
    vector<ll> fs(n), fp(n), fq(n);
    di.get_distance(0);
    rep(i, n) fs[i] = di.dist[i];
    di.get_distance(p);
    rep(i, n) fp[i] = di.dist[i];
    di.get_distance(q);
    rep(i, n) fq[i] = di.dist[i];
    ll ans = -1;
    // ずっと一緒
    if ((fs[p] + fp[q] + fq[0]) <= t) {
        cout << t << endl;
        return 0;
    }
    // 街iで分かれ、街jで合流する
    rep(i, n) {
        rep(j, n) {
            ll sp = fp[i] + fp[j];
            ll sq = fq[i] + fq[j];
            ll sep = max(sp, sq);
            ll tot = fs[i] + sep + fs[j];
            if (tot > t) continue;
            ans = max(ans, t - sep);
        }
    }
    cout << ans << endl;
    return 0;
}
0