結果
| 問題 |
No.160 最短経路のうち辞書順最小
|
| ユーザー |
chakku
|
| 提出日時 | 2020-03-23 18:43:40 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 21 ms / 5,000 ms |
| コード長 | 1,843 bytes |
| コンパイル時間 | 2,083 ms |
| コンパイル使用メモリ | 181,848 KB |
| 実行使用メモリ | 5,248 KB |
| 最終ジャッジ日時 | 2024-12-26 16:24:13 |
| 合計ジャッジ時間 | 3,582 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 26 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
template<typename T>
ostream& operator<<(ostream& os, const vector<T>& v){
os << "{";
for(auto e : v){
os << e << ",";
}
os << "}";
return os;
}
using ll = long long;
constexpr long long linf = numeric_limits<long long>::max()/3;
struct edge{
int from,to;
ll cost;
edge(int f,int t,ll c) : from(f), to(t), cost(c){}
edge(){}
};
int main(){
int n,m,s,g;
cin >> n >> m >> s >> g;
vector<vector<edge>> G(n);
vector<vector<ll>> dist(n,vector<ll>(n,linf));
for(int i=0;i<n;i++) dist[i][i] = 0;
for(int i=0;i<m;i++){
int a,b,c; cin >> a >> b >> c;
G[a].emplace_back(a,b,c);
G[b].emplace_back(b,a,c);
dist[a][b] = c;
dist[b][a] = c;
}
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]);
}
}
}
//cout << dist[s][g] << endl;
//for(int i=0;i<n;i++){
// for(int j=0;j<n;j++){
// if(dist[i][j] == linf) cout << "X" << " ";
// else cout << dist[i][j] << " ";
// }
// cout << endl;
//}
vector<int> ans;
ans.push_back(s);
int now = s;
for(int i=0;i<n;i++){
vector<int> nexts;
for(auto e : G[now]){
if(e.cost + dist[e.to][g] == dist[now][g]){
nexts.emplace_back(e.to);
}
}
sort(nexts.begin(), nexts.end());
//cout << now << " " << nexts << endl;
//assert(nexts.size() > 0);
ans.push_back(nexts[0]);
now = nexts[0];
if(now == g){
break;
}
}
for(int i=0;i<ans.size();i++){
if(i) cout << " ";
cout << ans[i];
}
cout << endl;
}
chakku