結果

問題 No.848 なかよし旅行
コンテスト
ユーザー WutongDeath
提出日時 2026-07-26 14:57:06
言語 C++23(gcc16)
(gcc 16.1.0 + boost 1.90.0)
コンパイル:
g++-16 -O2 -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 50 ms / 2,000 ms
+ 176µs
コード長 1,549 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 2,046 ms
コンパイル使用メモリ 211,020 KB
実行使用メモリ 7,744 KB
最終ジャッジ日時 2026-07-26 14:57:12
合計ジャッジ時間 3,856 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge2_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 26
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <functional>
#include <queue>
#include <utility>
#include <vector>

using namespace std;

typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL, int> PLI;

const int N = 2010;
const LL INF = 0x3f3f3f3f3f3f3f3fLL;

vector<PII> g[N];
priority_queue<PLI, vector<PLI>, greater<PLI>> pq;
LL d1[N], dp[N], dq[N], ans;
int n, m, p, q, t;

void Dijkstra(int bg, LL dist[]) {
    fill(dist + 1, dist + n + 1, INF);
    dist[bg] = 0LL;
    pq.push({ 0LL, bg });
    while (!pq.empty()) {
        auto [dis, u] = pq.top();
        pq.pop();
        if (dis != dist[u]) continue;
        for (auto [v, w] : g[u]) {
            if (dist[v] <= dis + w) continue;
            dist[v] = dis + w;
            pq.push({ dist[v], v });
        }
    }
}

int main() {
    // freopen("friend.in", "r", stdin);
    // freopen("friend.out", "w", stdout);

    scanf("%d%d%d%d%d", &n, &m, &p, &q, &t);
    for (int i = 1, a, b, c; i <= m; ++i) {
        scanf("%d%d%d", &a, &b, &c);
        g[a].push_back({ b, c });
        g[b].push_back({ a, c });
    }

    Dijkstra(1, d1);
    Dijkstra(p, dp);
    Dijkstra(q, dq);

    if (d1[p] + dp[q] + d1[q] <= t) {
        printf("%d\n", t);
        return 0;
    }

    ans = -1LL;
    for (int x = 1; x <= n; ++x) {
        for (int y = x; y <= n; ++y) {
            LL sep = max(dp[x] + dp[y], dq[x] + dq[y]);
            if (d1[x] + sep + d1[y] <= t) ans = max(ans, 1LL * t - sep);
        }
    }

    printf("%lld\n", ans);

    return 0;
}
0