結果

問題 No.848 なかよし旅行
ユーザー hipopohipopo
提出日時 2020-04-14 15:41:43
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,258 bytes
コンパイル時間 1,635 ms
コンパイル使用メモリ 146,712 KB
実行使用メモリ 10,748 KB
最終ジャッジ日時 2024-04-09 05:24:58
合計ジャッジ時間 3,977 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 146 ms
10,748 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 AC 2 ms
6,948 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 AC 2 ms
6,944 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 AC 57 ms
6,948 KB
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 AC 5 ms
6,944 KB
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 AC 2 ms
6,944 KB
testcase_25 WA -
testcase_26 AC 2 ms
6,944 KB
testcase_27 AC 2 ms
6,944 KB
testcase_28 AC 2 ms
6,948 KB
testcase_29 AC 2 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <cmath>
#include <complex>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
#include <functional>
#include <cstring>
#include <regex>
#include <random>
#include <cassert>
#include <stack>
using namespace std;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repr(i, s, n) for (int i = (s); i < (int)(n); i++)
#define revrep(i, n) for (int i = (n) - 1; i >= 0; i--)
#define revrepr(i, s, n) for (int i = (n) - 1; i >= s; i--)
#define debug(x) cerr << #x << ": " << x << "\n"
#define popcnt(x) __builtin_popcount(x)

using ll = long long;
using P = pair<int, int>;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

template<class T>
istream& operator >>(istream &is, vector<T> &v) {
    for (int i = 0; i < (int)v.size(); i++) cin >> v.at(i);
    return is;
}

template<class T, class U>
ostream& operator <<(ostream &os, pair<T, U> p) {
    cout << '(' << p.first << ", " << p.second << ')';
    return os;
}

template<class T>
void print(const vector<T> &v, const string &delimiter) { rep(i, v.size()) cout << (0 < i ? delimiter : "") << v.at(i); cout << endl; }

template<class T>
void print(const vector<vector<T>> &vv, const string &delimiter) { for (const auto &v: vv) print(v, delimiter); }

template<class Weight>
struct WeightedGraph {
    struct Edge{
        int to;
        Weight weight;
    };
    int n;
    vector<vector<Edge>> edges;
    Weight zero;
    Weight inf;
    WeightedGraph(int n, Weight zero, Weight inf) : n(n), edges(n), zero(zero), inf(inf) {}
    void add_edge(int u, int v, Weight w) { edges[u].push_back({v, w}); }
};

template<class Weight>
vector<Weight> dijkstra(int s, const WeightedGraph<Weight> &g/*, vector<Weight> &d*/) {
    using Node = pair<Weight, int>;
    priority_queue<Node, vector<Node>, greater<Node>> que;
    vector<Weight> d(g.n, g.inf);
    d[s] = g.zero;
    que.push(Node(g.zero, s));

    while (!que.empty()) {
        Node p = que.top(); que.pop();
        int v = p.second;
        if (d[v] < p.first) continue;
        for (int i = 0; i < (int)g.edges[v].size(); i++) {
            auto e = g.edges[v][i];
            if (d[e.to] > d[v] + e.weight) {
                d[e.to] = d[v] + e.weight;
                que.push(Node(d[e.to], e.to));
            }
        }
    }
    return d;
}

int main() {
    int n, m, p, q, t;
    cin >> n >> m >> p >> q >> t;
    p--; q--;
    WeightedGraph<ll> graph(n, 0, 1e18);
    rep(i, m) {
        int a, b, w;
        cin >> a >> b >> w;
        a--; b--;
        graph.add_edge(a, b, w);
        graph.add_edge(b, a, w);
    }
    
    auto dist_p_to = dijkstra(p, graph);
    auto dist_q_to = dijkstra(q, graph);
    auto dist_0_to = dijkstra(0, graph);
    
    if (dist_0_to[p] + dist_p_to[q] + dist_q_to[0] <= t || dist_0_to[q] + dist_q_to[p] + dist_p_to[0] <= t) {
        cout << t << endl;
        return 0;
    }
    
    ll ans = -1;
    rep(v, n) {
        ll solo = max(dist_p_to[v] * 2, dist_q_to[v] * 2);
        if (dist_0_to[v] * 2 + solo <= t) {
            chmax(ans, t - solo);
        }
    }
    cout << ans << endl;
}
0