結果

問題 No.160 最短経路のうち辞書順最小
ユーザー krotonkroton
提出日時 2015-03-04 06:24:26
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 40 ms / 5,000 ms
コード長 1,569 bytes
コンパイル時間 2,424 ms
コンパイル使用メモリ 159,660 KB
実行使用メモリ 9,184 KB
最終ジャッジ日時 2023-09-06 06:44:14
合計ジャッジ時間 3,087 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

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

struct Dist {
    int cost;
    vector<int> path;

    bool operator<(const Dist& other) const {
    	if(cost != other.cost){
            return cost < other.cost;
        }

        return path < other.path;
    }
	bool operator>(const Dist& other) const {
		return (other < *this);
	}

    Dist operator+(const Dist& other) const {
        Dist res = *this;
        res.cost += other.cost;
        res.path.insert(res.path.end(), other.path.begin(), other.path.end());

        return res;
    }
};

typedef vector<vector<Dist>> Graph;

Dist dijkstra(const Graph &g, int S, int G){
    const int V = g.size();

    vector<Dist> dist(V, {1 << 25});
    dist[S] = {0, {S}};

    priority_queue<Dist, vector<Dist>, greater<Dist>> Q;
    Q.push({0, {S}});

    while(!Q.empty()){
        auto d   = Q.top(); Q.pop();
        auto pos = d.path.back();

        if(dist[pos] < d){
            continue;
        }

        for(const auto& e : g[pos]){
            auto nd   = d + e;
            auto npos = e.path.back();

            if(nd < dist[npos]){
                dist[npos] = nd;
                Q.push(nd);
            }
        }
    }

    return dist[G];
}

int main(){
    int N, M, S, G;
	cin >> N >> M >> S >> G;

    vector<vector<Dist>> g(N);
    for(int i=0;i<M;i++){
        int a, b, c;
        cin >> a >> b >> c;

        g[a].push_back(Dist{c, {b}});
		g[b].push_back(Dist{c, {a}});
    }

    auto res = dijkstra(g, S, G);
    for(int v : res.path){
        cout << v << " ";
    }
    
    return 0;
}
0