結果

問題 No.160 最短経路のうち辞書順最小
ユーザー yaoshimaxyaoshimax
提出日時 2015-03-02 00:05:36
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 21 ms / 5,000 ms
コード長 1,752 bytes
コンパイル時間 1,493 ms
コンパイル使用メモリ 98,060 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-06 06:05:59
合計ジャッジ時間 2,401 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <cstring>

using namespace std;

int main(){
   int N,M,S,G;
   cin >> N >> M >> S >> G;
   vector<pair<int,int> > edge[N];
   int dist[N][N];
   for( int i = 0 ; i <N; i++ )for( int j = 0 ; j <N; j++ ) dist[i][j]=INT_MAX/2;
   for( int i = 0; i < M ; i++ ){
      int a,b,c;
      cin >> a >> b >> c;
      edge[a].push_back(make_pair(b,c));
      edge[b].push_back(make_pair(a,c));
      dist[a][b]=c;
      dist[b][a]=c;
   }
   for( int i = 0 ; i < N; i++ ){
      for( int j = 0 ; j < N ; j++ ){
         for( int k = 0 ; k < N ; k++ ){
            dist[j][k] = min(dist[j][k],dist[j][i]+dist[i][k]);
         }
      }
   }
   for( int i = 0 ; i < N ; i++ ){
      dist[i][i]=0;
      sort(edge[i].begin(),edge[i].end());
   }
   int mindist = dist[S][G];
   vector<int> ans;
   ans.push_back(S);
   int prev = S;
   int curDist =0;
   while( prev!= G){
      for( int i = 0 ; i < (int) edge[prev].size(); i++ ){
         pair<int,int> dw = edge[prev][i];
         //cout << dw.first<<", "<<dw.second<<" "<< mindist<<endl;
         if( curDist + dw.second + dist[dw.first][G] == mindist ){
            //cout << dw.first << endl;
            ans.push_back(dw.first);
            prev = dw.first;
            curDist += dw.second;
            break;
         }
      }
      //cout << endl;
   }
   for( int i = 0 ; i <(int)ans.size(); i++ ){
      cout << ans[i] << " ";
   }
   cout << endl;
   return 0;
}
0