結果

問題 No.160 最短経路のうち辞書順最小
ユーザー 東前頭十一枚目東前頭十一枚目
提出日時 2018-08-19 18:41:47
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,901 bytes
コンパイル時間 1,693 ms
コンパイル使用メモリ 173,788 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-29 00:54:34
合計ジャッジ時間 2,890 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

// Dijkstra法(枝刈りあり)(無向木経路探索)
// 「終点」から全点間の最短距離を求める
// 最短経路を辞書順最小で表示
// 計算量O((E+V)logE)

#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
const int INF=INT_MAX,MOD=1e9+7;

const int VMAX=300;
int V,E,start,goal;
int board[VMAX][VMAX];
int dist[VMAX];
bool used[VMAX];
vector<int> route;

void Dijkstra(){
    // 距離をINFで初期化
    rep(to,VMAX) dist[to]=INF;
    // 始点は0
    dist[goal]=0;
    // フラグを下げる
    rep(v,VMAX) used[v]=false;
    // 優先度付きキュー:[距離,頂点]←距離の昇順
    priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> q;
    // 距離0,始点
    q.push(make_pair(0,goal));
    // dijkstra
    while(q.size()){
        // 取り出してd=距離,v=頂点
        pair<int,int> p=q.top(); q.pop();
        int d=p.first,v=p.second;
        if(used[v]) continue;
        used[v]=true;
        // 頂点から出る辺すべてについて
        for(int to=0;to<V;to++){
            // 辺が存在して短縮できるとき
            if(board[v][to]>0 && d+board[v][to]<dist[to]){
                dist[to]=d+board[v][to];
                q.push(make_pair(dist[to],to));
            }
        }
    }
}

void Route(int from){
    route.push_back(from);
    if(from==goal) return;
    rep(to,V){
        // 最短距離から逆算する
        if(dist[from]==dist[to]+board[from][to]){
            Route(to);
            return;
        }
    }
}

int main(){
    rep(i,VMAX)rep(j,VMAX) board[i][j]=-1;
    cin>>V>>E>>start>>goal;
    rep(i,E){
        int from,to,distance;
        cin>>from>>to>>distance;
        board[from][to]=board[to][from]=distance;
    }
    Dijkstra();
    Route(start);
    for(auto i:route) cout<<i<<(i!=goal?" ":"");
    cout<<endl;
    return 0;
}
0