結果

問題 No.1301 Strange Graph Shortest Path
ユーザー trineutron
提出日時 2020-11-27 22:56:17
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 2,086 bytes
コンパイル時間 2,452 ms
コンパイル使用メモリ 210,704 KB
最終ジャッジ日時 2025-01-16 08:17:21
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 28 WA * 5
権限があれば一括ダウンロードができます

ソースコード

diff #

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

using point = pair<int64_t, int>;
using edge = int;
using graph = vector<vector<edge>>;

int main() {
    constexpr int64_t inf = 1e18;
    int n, m;
    cin >> n >> m;
    vector<int64_t> u(m), v(m), c(m), d(m), distance(n, inf);
    graph to(n);
    for (int i = 0; i < m; i++) {
        cin >> u.at(i) >> v.at(i) >> c.at(i) >> d.at(i);
        u.at(i)--; v.at(i)--;
        to.at(u.at(i)).push_back(i);
        to.at(v.at(i)).push_back(i);
    }
    priority_queue<point, vector<point>, greater<point>> q;
    q.emplace(0, 0);
    while (not q.empty()) {
        auto [cost, vertex] = q.top();
        q.pop();
        if (cost >= distance.at(vertex)) continue;
        distance.at(vertex) = cost;
        for (auto next : to.at(vertex)) {
            int v_next = u.at(next) + v.at(next) - vertex;
            int64_t cost_next = cost + c.at(next);
            if (cost_next >= distance.at(v_next)) continue;
            q.emplace(cost_next, v_next);
        }
    }
    vector<bool> passed(m);
    int current = n - 1;
    while (current) {
        for (auto prev : to.at(current)) {
            int v_prev = u.at(prev) + v.at(prev) - current;
            if (distance.at(current) == distance.at(v_prev) + c.at(prev)) {
                current = v_prev;
                passed.at(prev) = true;
                break;
            }
        }
    }
    q.emplace(distance.at(n - 1), n - 1);
    for (int i = 0; i < n; i++) {
        distance.at(i) = inf;
    }
    while (not q.empty()) {
        auto [cost, vertex] = q.top();
        q.pop();
        if (cost >= distance.at(vertex)) continue;
        distance.at(vertex) = cost;
        for (auto next : to.at(vertex)) {
            int v_next = u.at(next) + v.at(next) - vertex;
            int64_t cost_next = cost + c.at(next);
            if (passed.at(next)) {
                cost_next = cost + d.at(next);
            }
            if (cost_next >= distance.at(v_next)) continue;
            q.emplace(cost_next, v_next);
        }
    }
    cout << distance.at(0) << endl;
}
0