結果
| 問題 |
No.160 最短経路のうち辞書順最小
|
| コンテスト | |
| ユーザー |
siman
|
| 提出日時 | 2021-12-23 19:30:39 |
| 言語 | C++17(clang) (17.0.6 + boost 1.87.0) |
| 結果 |
MLE
|
| 実行時間 | - |
| コード長 | 1,812 bytes |
| コンパイル時間 | 4,678 ms |
| コンパイル使用メモリ | 143,372 KB |
| 実行使用メモリ | 814,740 KB |
| 最終ジャッジ日時 | 2024-09-17 18:04:42 |
| 合計ジャッジ時間 | 6,814 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 3 MLE * 1 -- * 22 |
ソースコード
#include <cassert>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <climits>
#include <map>
#include <queue>
#include <set>
#include <cstring>
#include <vector>
using namespace std;
typedef long long ll;
struct Node {
int v;
int parent;
ll cost;
Node(int v = -1, int parent = -1, ll cost = -1) {
this->v = v;
this->parent = parent;
this->cost = cost;
}
bool operator>(const Node &n) const {
return cost > n.cost;
}
};
ll g_cost[210][210];
vector<int> E[210];
int main() {
memset(g_cost, -1, sizeof(g_cost));
int N, M, S, G;
cin >> N >> M >> S >> G;
for (int i = 0; i < M; ++i) {
int a, b, c;
cin >> a >> b >> c;
E[a].push_back(b);
E[b].push_back(a);
g_cost[a][b] = c;
g_cost[b][a] = c;
}
priority_queue <Node, vector<Node>, greater<Node>> pque;
pque.push(Node(S, -1, 0));
ll visited[N + 1];
memset(visited, -1, sizeof(visited));
int step_history[N + 1];
memset(step_history, -1, sizeof(step_history));
while (not pque.empty()) {
Node node = pque.top();
pque.pop();
if (visited[node.v] == -1) {
visited[node.v] = node.cost;
step_history[node.v] = node.parent;
} else if (visited[node.v] < node.cost) {
continue;
} else {
step_history[node.v] = min(step_history[node.v], node.parent);
}
for (int u : E[node.v]) {
ll ncost = node.cost + g_cost[node.v][u];
pque.push(Node(u, node.v, ncost));
}
}
vector<int> path;
int cur = G;
path.push_back(G);
while (cur != S) {
cur = step_history[cur];
path.push_back(cur);
}
reverse(path.begin(), path.end());
string ans = "";
for (int p : path) {
ans += to_string(p);
if (p != path.back()) ans += " ";
}
cout << ans << endl;
return 0;
}
siman