結果

問題 No.160 最短経路のうち辞書順最小
ユーザー finefine
提出日時 2016-04-05 21:44:52
言語 C++11
(gcc 11.4.0)
結果
MLE  
実行時間 -
コード長 2,052 bytes
コンパイル時間 1,490 ms
コンパイル使用メモリ 172,740 KB
実行使用メモリ 813,780 KB
最終ジャッジ日時 2024-04-14 22:13:14
合計ジャッジ時間 5,283 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

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;
        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 = -1;

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] >= x.cost) { 
            minc[x.at] = x.cost;
            if(Prev[x.at] == NONE || Prev[x.at] > x.prev) 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] > x.cost + (*i).cost) {
            	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, s);
    vector<int> ans;
    int tmp = g;
    while(tmp != NONE){
        ans.push_back(tmp);
        tmp = Prev[tmp];
    }
    for(auto i = ans.end() - 1, e = ans.begin(); i != e; --i){
        cout << (*i) << " ";    
    }
    cout << ans.front() << "\n";
    return 0;
    }
0