結果
問題 | No.160 最短経路のうち辞書順最小 |
ユーザー |
![]() |
提出日時 | 2017-05-25 20:52:14 |
言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 1,179 bytes |
コンパイル時間 | 1,140 ms |
コンパイル使用メモリ | 161,256 KB |
実行使用メモリ | 5,376 KB |
最終ジャッジ日時 | 2024-09-19 19:16:01 |
合計ジャッジ時間 | 1,947 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | WA * 4 |
other | WA * 26 |
ソースコード
#include"bits/stdc++.h"//#include<bits/stdc++.h>using namespace std;#define print(x) cout<<x<<endl;#define rep(i,a,b) for(int i=a;i<b;i++)#define REP(i,a) for(int i=0;i<a;i++)typedef long long ll;typedef pair<int, int> PI;typedef pair<int, PI> V;const ll mod = 100000000;int n, m, s, g;int a[20000], b[20000], c[20000];int d[202];bool used[202];int cost[202][202];void dijkstra(int s) {REP(i, n)d[i] = mod;REP(i, n)used[i] = 0;d[s] = 0;while (true) {int v = -1;REP(u, n) {if (!used[u] && (v == -1 || d[u] < d[v]))v = u;}if (v == -1)break;used[v] = true;REP(u, n) {d[u] = min(d[u], d[v] + cost[v][u]);}}}int main() {cin >> n >> m >> s >> g;REP(i, 202)REP(j, 202)cost[i][j] = mod;REP(i, m) {cin >> a[i] >> b[i] >> c[i];cost[a[i]][b[i]] = c[i];cost[b[i]][a[i]] = c[i];}dijkstra(s);vector<int> v;v.push_back(g);int now = g;while (now != s) {int next = mod;rep(i, 0, n) {if (d[i] == d[now] - cost[i][now]) {next= min(next, i);}}now = next;v.push_back(now);}reverse(v.begin(), v.end());print(d[g]);REP(i,v.size())cout<<v[i] << " ";cout << endl;return 0;}