結果

問題 No.160 最短経路のうち辞書順最小
ユーザー finefine
提出日時 2019-10-22 02:50:34
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,123 bytes
コンパイル時間 1,953 ms
コンパイル使用メモリ 179,184 KB
実行使用メモリ 5,200 KB
最終ジャッジ日時 2023-09-15 15:17:50
合計ジャッジ時間 2,994 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

using ll = long long;

struct State {
    int at;
    ll cost;
    int prev;
    State(int at, ll cost, int prev) : at(at), cost(cost), prev(prev) {}
    bool operator>(const State& s) const {
        if (cost != s.cost) return cost > s.cost;
        if (prev != s.prev) return prev > s.prev; //最短経路を辞書順最小にする(省略可)
        return at > s.at;
        //return cost > s.cost;
    }
};

struct Edge {
    int to;
    ll cost;
    Edge(int to, ll cost) : to(to), cost(cost) {}
};

using Graph = vector<vector<Edge> >; //隣接リスト

const ll INF = 1e15;
const int NONE = -1;

//sは始点、mincは最短経路のコスト、prevsは最短経路をたどる際の前の頂点
void dijkstra(int s, const Graph& graph, vector<ll>& minc, vector<int>& prevs){
    minc.assign(graph.size(), INF);
    prevs.assign(graph.size(), NONE);

    priority_queue<State, vector<State>, greater<State> > pq;
    pq.emplace(s, 0, NONE);
    minc[s] = 0;

    while(!pq.empty()) {
        State cur = pq.top();
        pq.pop();
        //if (minc[cur.at] < cur.cost) continue; 

        for(const Edge& e : graph[cur.at]) {
            ll cost = cur.cost + e.cost;
            if (minc[e.to] < cost || minc[e.to] == cost && prevs[e.to] <= cur.at) continue;
            minc[e.to] = cost;
            prevs[e.to] = cur.at;
            pq.emplace(e.to, cost, cur.at);
        }
    }
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n, m, s, g;
    cin >> n >> m >> s >> g;
    Graph graph(n);
    for (int i = 0; i < m; i++) {
        int a, b;
        ll c;
        cin >> a >> b >> c;
        graph[a].emplace_back(b, c);
        graph[b].emplace_back(a, c);
    }

    vector<ll> minc;
    vector<int> prevs;
    dijkstra(s, graph, minc, prevs);

    vector<int> ans;
    int cur = g;
    while (cur != NONE) {
        ans.push_back(cur);
        cur = prevs[cur];
    }
    reverse(ans.begin(), ans.end());
    int sz = ans.size();
    for (int i = 0; i < sz; i++) {
        cout << ans[i] << " \n"[i + 1 == sz];
    }
    return 0;
}
0