結果

問題 No.160 最短経路のうち辞書順最小
ユーザー finefine
提出日時 2016-04-05 22:41:50
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 10 ms / 5,000 ms
コード長 1,893 bytes
コンパイル時間 1,497 ms
コンパイル使用メモリ 171,228 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-14 22:13:37
合計ジャッジ時間 2,661 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

struct node {
    int at;
    ll cost; //オーバーフローを避けるためlong longに
    int prev;
    node(int at, ll cost, int prev) : at(at), cost(cost), prev(prev) {}
    bool operator>(const node &s) const {
        if (cost != s.cost) return cost > s.cost;
        if (prev != s.prev) return prev > s.prev;
        return at > s.at;
    }
};

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

typedef vector<vector<Edge> > AdjList; //隣接リスト
typedef vector<Edge>::iterator Edge_it;

const ll INF = 100000000000;
const int NONE = 1000;

AdjList graph;
vector<ll> minc; //最短経路のコスト
vector<int> Prev; //最短経路をたどる際の前の頂点

void dijkstra(int n, int s){ //nは頂点数、sは始点
    minc = vector<ll>(n, INF);
    Prev = vector<int>(n, NONE);
    priority_queue<node, vector<node>, greater<node> > pq;
    pq.push(node(s, 0, NONE));
    while(!pq.empty()) {
        node x = pq.top();
        pq.pop();
        if (minc[x.at] != INF) continue;
        minc[x.at] = x.cost;
        Prev[x.at] = x.prev;
        for(Edge_it i = graph[x.at].begin(), e = graph[x.at].end(); i != e; ++i) {
            if (minc[(*i).to] == INF) {
            	pq.push(node((*i).to, x.cost + (*i).cost, x.at));
            }
        }
    }
}
int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n, m, s, g;
    cin >> n >> m >> s >> g;
    graph = AdjList(n);
    for(int i = 0; i < m; i++) {
        int from, to;
        ll cost;
        cin >> from >> to >> cost;
        graph[from].push_back(Edge(to, cost));
        graph[to].push_back(Edge(from, cost));
    }
    dijkstra(n, g);
    int tmp = s;
    while(tmp != g){
        cout << tmp << " ";
        tmp = Prev[tmp];
    }
    cout << g << "\n";
    return 0;
    }
0