結果

問題 No.160 最短経路のうち辞書順最小
ユーザー knewknowlknewknowl
提出日時 2015-03-02 21:08:43
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,014 bytes
コンパイル時間 480 ms
コンパイル使用メモリ 66,060 KB
実行使用メモリ 4,504 KB
最終ジャッジ日時 2023-09-06 06:31:59
合計ジャッジ時間 1,769 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int G[200][200];
int pre[200],d[200];
int n;
bool visited[200];
void dijk(int s,int g){
  for(int i=0;i<n;++i) pre[i]=-1,d[i]=1<<25;
  d[s]=0;
  while(true){
    int idx = -1;
    int m = 1<<25;
    for(int i=0;i<n;++i){
      if(m>d[i]&&!visited[i]){
	  m=d[i];
	  idx = i;
      }
    }
    if(idx==-1) break;
    visited[idx]=true;
    for(int i=0;i<n;++i){
      if(d[i]==d[idx]+G[i][idx] && idx<pre[i]) pre[i] = idx;
      if(d[i]>d[idx]+G[i][idx]){
	d[i]=d[idx]+G[i][idx];
	pre[i]=idx;
      }
    }
  }
  
  vector<int> root;
  int node = g;
  while(node!=-1){
    root.push_back(node);
    node = pre[node];
  }
  reverse(root.begin(),root.end());
  for(int i=0,m=root.size();i<m;++i) cout << root[i] << " ";
  cout << endl;
}
int main(){
  int m,s,g;
  cin>>n>>m>>s>>g;
  for(int i=0;i<n;++i) for(int j=0;j<n;++j) G[i][j]=1<<25;
  while(m--){
    int a,b,c;cin>>a>>b>>c;
    G[a][b]=G[b][a]=c;
  }
  dijk(s,g);
  return 0;
}
0