結果
| 問題 |
No.160 最短経路のうち辞書順最小
|
| コンテスト | |
| ユーザー |
ふーらくたる
|
| 提出日時 | 2016-07-01 15:59:01 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 22 ms / 5,000 ms |
| コード長 | 2,394 bytes |
| コンパイル時間 | 520 ms |
| コンパイル使用メモリ | 56,924 KB |
| 実行使用メモリ | 6,824 KB |
| 最終ジャッジ日時 | 2024-10-12 00:01:36 |
| 合計ジャッジ時間 | 1,696 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 26 |
ソースコード
#include <iostream>
using namespace std;
const int kINF = 1 << 28;
const int kMAX_N = 210;
const int kMAX_M = kMAX_N * (kMAX_N - 1) / 2;
int N, M, S, G;
int cost[kMAX_N][kMAX_N];
int next_node[kMAX_N][kMAX_N];
int a[kMAX_M], b[kMAX_M], c[kMAX_M];
void WarshallFloyd() {
for (int k = 0; k < N; k++) {
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if (cost[i][j] > cost[i][k] + cost[k][j]) {
cost[i][j] = cost[i][k] + cost[k][j];
next_node[i][j] = next_node[i][k];
} else if (cost[i][j] == cost[i][k] + cost[k][j] && next_node[i][j] > next_node[i][k]) {
next_node[i][j] = next_node[i][k];
}
}
}
}
}
/*
void RestoreRoute(int node, int start, int goal) {
cout << node << " " << start << " " << goal << endl;
if (node == start) {
cout << node;
return;
}
RestoreRoute(next_node[start][node], start, goal);
cout << " " << node;
if (node == goal) {
cout << endl;
}
}
*/
/*
#include <vector>
vector<int> RestoreRoute(int node, int start, int goal) {
if (node == start) {
cout << node;
return;
}
RestoreRoute(next_node[start][node], start, goal);
cout << " " << node;
if (node == goal) {
cout << endl;
}
}
*/
void Solve() {
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if (i == j) {
cost[i][j] = 0;
next_node[i][j] = kINF;
} else {
cost[i][j] = kINF;
next_node[i][j] = j;
}
}
}
for (int i = 0; i < M; i++) {
cost[a[i]][b[i]] = cost[b[i]][a[i]] = c[i];
}
WarshallFloyd();
// RestoreRoute(G, S, G);
/*
int u, v;
while (cin >> u >> v, u | v) {
cout << "next " << next_node[u][v] << endl;
}
*/
int node = S;
cout << S;
while (node != G) {
node = next_node[node][G];
cout << " " << node;
}
cout << endl;
/*
cout << cost[S][G] << endl;
int s, g;
while (cin >> s >> g, s | g) {
cout << next_node[s][g] << endl;
}
*/
}
int main() {
cin >> N >> M >> S >> G;
for (int i = 0; i < M; i++) {
cin >> a[i] >> b[i] >> c[i];
}
Solve();
return 0;
}
ふーらくたる