結果

問題 No.2477 Drifting
コンテスト
ユーザー rin204
提出日時 2023-09-08 19:17:20
言語 C++23
(gcc 15.2.0 + boost 1.89.0)
コンパイル:
g++-15 -O2 -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 357 ms / 2,000 ms
コード長 1,221 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 3,057 ms
コンパイル使用メモリ 360,848 KB
実行使用メモリ 33,024 KB
最終ジャッジ日時 2026-03-13 05:56:39
合計ジャッジ時間 8,846 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge1_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 14
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <bits/stdc++.h>
using namespace std;
using ll = long long;

int main() {
    int n, m;
    cin >> n >> m;
    vector<vector<pair<int, ll>>> edges(n);
    for (int i = 0; i < m; i++) {
        int u, v;
        ll w;
        cin >> u >> v >> w;
        edges[u - 1].emplace_back(v - 1, w);
    }
    int k;
    cin >> k;
    vector<set<pair<int, int>>> ng(n);
    for (int i = 0; i < k; i++) {
        int a, b, c;
        cin >> a >> b >> c;
        ng[b - 1].emplace(a - 1, c - 1);
    }

    vector<vector<pair<ll, int>>> cand(n, vector<pair<ll, int>>());
    cand[0].emplace_back(0, 0);
    for (int l = 0; l < n; l++) {
        sort(cand[l].begin(), cand[l].end());
        for (auto [d, a] : cand[l]) {
            vector<pair<int, ll>> nedges;
            for (auto [r, w] : edges[l]) {
                if (ng[l].count({a, r})) {
                    nedges.emplace_back(r, w);
                } else {
                    cand[r].emplace_back(d + w, l);
                }
            }
            edges[l] = nedges;
        }
    }

    const ll inf = 1LL << 60;
    ll ans       = inf;
    for (auto [d, _] : cand[n - 1]) {
        ans = min(ans, d);
    }
    cout << (ans == inf ? -1 : ans) << endl;
}
0