結果

問題 No.160 最短経路のうち辞書順最小
ユーザー ttkkggwwttkkggww
提出日時 2022-08-17 23:20:24
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 16 ms / 5,000 ms
コード長 1,176 bytes
コンパイル時間 4,671 ms
コンパイル使用メモリ 259,264 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-15 10:53:08
合計ジャッジ時間 6,267 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 13 ms
5,376 KB
testcase_05 AC 14 ms
5,376 KB
testcase_06 AC 15 ms
5,376 KB
testcase_07 AC 11 ms
5,376 KB
testcase_08 AC 11 ms
5,376 KB
testcase_09 AC 11 ms
5,376 KB
testcase_10 AC 11 ms
5,376 KB
testcase_11 AC 12 ms
5,376 KB
testcase_12 AC 11 ms
5,376 KB
testcase_13 AC 12 ms
5,376 KB
testcase_14 AC 12 ms
5,376 KB
testcase_15 AC 11 ms
5,376 KB
testcase_16 AC 11 ms
5,376 KB
testcase_17 AC 12 ms
5,376 KB
testcase_18 AC 11 ms
5,376 KB
testcase_19 AC 11 ms
5,376 KB
testcase_20 AC 11 ms
5,376 KB
testcase_21 AC 11 ms
5,376 KB
testcase_22 AC 11 ms
5,376 KB
testcase_23 AC 12 ms
5,376 KB
testcase_24 AC 11 ms
5,376 KB
testcase_25 AC 11 ms
5,376 KB
testcase_26 AC 11 ms
5,376 KB
testcase_27 AC 10 ms
5,376 KB
testcase_28 AC 16 ms
5,376 KB
testcase_29 AC 11 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
#include<atcoder/all>
using namespace atcoder;
using ll = long long;
int n,m,s,g;
using P = pair<int,int>;
vector<vector<P>> G;

void solve(){
	vector<vector<int>> dist(n,vector<int>(n,INT_MAX/2));
	for(int i =0;i<n;i++)dist[i][i] = 0;
	for(int i = 0;i<n;i++){
		for(auto &[to,cost]:G[i]){
			dist[i][to] = cost;
			dist[to][i] = cost;
		}
	}
	for(int k = 0;k<n;k++){
		for(int i = 0;i<n;i++){
			for(int j = 0;j<n;j++){
				dist[i][j] = min(dist[i][j],dist[i][k]+dist[k][j]);
			}
		}
	}
	int sumcost = dist[s][g];
	cout<<s<<' ';
	int cur = s;
	auto dist2 = vector<vector<int>>(n,vector<int>(n,INT_MAX/2));
	for(int i = 0;i<n;i++){
		for(auto &[to,cost]:G[i]){
			dist2[i][to] = cost;
			dist2[to][i] = cost;
		}
	}
	while(cur!=g){
		for(int i = 0;i<n;i++){
			if(dist2[cur][i]+dist[i][g]==sumcost){
				sumcost -= dist2[cur][i];
				cout<<i<<' ';
				cur = i;
				break;
			}
		}
	}
	//cout<<g;
	cout<<endl;
}

signed main(){
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	cin >> n >> m >> s >> g;
	G = vector<vector<P>>(n);
	for(int i = 0;i<m;i++){
		int a,b,c;
		cin >> a >> b >> c;
		G[a].push_back(P(b,c));
	}
	solve();
}
0