結果

問題 No.848 なかよし旅行
ユーザー TAISA_TAISA_
提出日時 2019-07-05 22:04:34
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 791 ms / 2,000 ms
コード長 2,488 bytes
コンパイル時間 1,654 ms
コンパイル使用メモリ 177,684 KB
実行使用メモリ 10,560 KB
最終ジャッジ日時 2023-08-07 19:40:10
合計ジャッジ時間 5,130 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 791 ms
10,560 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,380 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,380 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 19 ms
5,116 KB
testcase_12 AC 24 ms
5,496 KB
testcase_13 AC 32 ms
6,412 KB
testcase_14 AC 14 ms
4,380 KB
testcase_15 AC 30 ms
6,160 KB
testcase_16 AC 48 ms
8,044 KB
testcase_17 AC 34 ms
7,080 KB
testcase_18 AC 19 ms
5,092 KB
testcase_19 AC 17 ms
4,656 KB
testcase_20 AC 10 ms
4,376 KB
testcase_21 AC 40 ms
7,100 KB
testcase_22 AC 38 ms
7,468 KB
testcase_23 AC 9 ms
4,376 KB
testcase_24 AC 2 ms
4,380 KB
testcase_25 AC 53 ms
7,844 KB
testcase_26 AC 1 ms
4,376 KB
testcase_27 AC 2 ms
4,376 KB
testcase_28 AC 2 ms
4,380 KB
testcase_29 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define all(vec) vec.begin(), vec.end()
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
constexpr ll INF = (1LL << 30) - 1LL;
constexpr ll LINF = (1LL << 60) - 1LL;
constexpr double eps = 1e-9;
constexpr ll MOD = 1000000007LL;
template <typename T>
bool chmin(T& a, T b) {
    if(a > b) {
        a = b;
        return true;
    }
    return false;
};
template <typename T>
bool chmax(T& a, T b) {
    if(a < b) {
        a = b;
        return true;
    }
    return false;
};
template <typename T>
ostream& operator<<(ostream& os, vector<T> v) {
    for(int i = 0; i < v.size(); i++) {
        os << v[i] << (i + 1 == v.size() ? "\n" : " ");
    }
    return os;
}
template <typename T>
vector<T> make_v(size_t a) {
    return vector<T>(a);
}
template <typename T, typename... Ts>
auto make_v(size_t a, Ts... ts) {
    return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));
}
template <typename T, typename V>
typename enable_if<is_class<T>::value == 0>::type fill_v(T& t, const V& v) {
    t = v;
}
template <typename T, typename V>
typename enable_if<is_class<T>::value != 0>::type fill_v(T& t, const V& v) {
    for(auto& e : t) {
        fill_v(e, v);
    }
};
ll n, m, p, q, t;
struct edge {
    ll to, cost;
};
vector<vector<edge>> G;
vector<ll> dijkstra(int s) {
    vector<ll> d(n, LINF);
    priority_queue<P, vector<P>, greater<P>> q;
    d[s] = 0;
    q.push(P(0, s));
    while(!q.empty()) {
        int v = q.top().second;
        q.pop();
        for(auto e : G[v]) {
            if(d[e.to] <= d[v] + e.cost)
                continue;
            d[e.to] = d[v] + e.cost;
            q.push(P(d[e.to], e.to));
        }
    }
    return d;
}
int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cin >> n >> m >> p >> q >> t;
    --p;
    --q;
    G.resize(n);
    for(int i = 0; i < m; i++) {
        ll a, b, c;
        cin >> a >> b >> c;
        --a;
        --b;
        G[a].push_back({b, c});
        G[b].push_back({a, c});
    }
    auto d = dijkstra(0);
    auto dp = dijkstra(p);
    auto dq = dijkstra(q);
    if(d[q] + dq[p] + dp[0] <= t) {
        cout << t << endl;
        return 0;
    }
    ll ans = -1;
    for(int i = 0; i < n; i++) {
        for(int j = 0; j < n; j++) {
            ll t1 = d[i] + max(dq[i] + dq[j], dp[i] + dp[j]);
            if(t1 + d[j] > t) {
                continue;
            }
            chmax(ans, d[i] + t - t1);
        }
    }
    cout << ans << endl;
}
0