提出 #70442586
ソースコード 拡げる
#include <bits/stdc++.h>
#ifdef LOCAL
#include <debug.hpp>
#else
#define debug(...) void(0)
#endif
template <class T, class U = T> bool chmin(T& x, U&& y) { return y < x and (x = std::forward<U>(y), true); }
template <class T, class U = T> bool chmax(T& x, U&& y) { return x < y and (x = std::forward<U>(y), true); }
template <class T> void mkuni(std::vector<T>& v) {
std::ranges::sort(v);
auto result = std::ranges::unique(v);
v.erase(result.begin(), result.end());
}
template <class T> int lwb(const std::vector<T>& v, const T& x) {
return std::distance(v.begin(), std::ranges::lower_bound(v, x));
}
using namespace std;
using ll = long long;
constexpr int INF = 1e9;
void solve() {
int N, M;
cin >> N >> M;
vector<vector<int>> G(N);
for (; M--;) {
int U, V;
cin >> U >> V;
U--, V--;
G[U].emplace_back(V);
G[V].emplace_back(U);
}
string S;
cin >> S;
vector dp(N, vector<pair<int, int>>(2, {INF, -1}));
priority_queue<tuple<int, int, int, int>> pq;
for (int i = 0; i < N; i++) {
if (S[i] == 'S') {
dp[i][0] = {0, i};
pq.emplace(-dp[i][0].first, dp[i][0].second, i, 0);
}
}
while (not pq.empty()) {
auto [val, pre, v, p] = pq.top();
pq.pop();
val *= -1;
if (dp[v][p].first != val or dp[v][p].second != pre) {
continue;
}
int nval = val + 1;
for (int u : G[v]) {
if (nval <= dp[u][0].first) {
if (dp[u][0].second == pre) {
if (dp[u][0].first == nval) {
continue;
}
dp[u][0] = {nval, pre};
pq.emplace(-nval, pre, u, 0);
} else {
dp[u][1] = dp[u][0];
dp[u][0] = {nval, pre};
pq.emplace(-dp[u][1].first, dp[u][1].second, u, 1);
pq.emplace(-nval, pre, u, 0);
}
} else if (dp[u][0].second != pre and nval < dp[u][1].first) {
dp[u][1] = {nval, pre};
pq.emplace(-nval, pre, u, 1);
}
}
}
for (int i = 0; i < N; i++) {
if (S[i] == 'D') {
int ans = dp[i][0].first + dp[i][1].first;
cout << ans << "\n";
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout << fixed << setprecision(15);
solve();
return 0;
}
提出情報
| 提出日時 | |
|---|---|
| 問題 | E - Hit and Away |
| ユーザ | rniya |
| 言語 | C++ 23 (gcc 12.2) |
| 得点 | 450 |
| コード長 | 2612 Byte |
| 結果 | AC |
| 実行時間 | 216 ms |
| メモリ | 34344 KiB |
ジャッジ結果
| セット名 | Sample | All | ||||
|---|---|---|---|---|---|---|
| 得点 / 配点 | 0 / 0 | 450 / 450 | ||||
| 結果 |
|
|
| セット名 | テストケース |
|---|---|
| Sample | example_00.txt, example_01.txt |
| All | example_00.txt, example_01.txt, hand_00.txt, hand_01.txt, hand_02.txt, hand_03.txt, hand_04.txt, hand_05.txt, hand_06.txt, hand_07.txt, hand_08.txt, hand_09.txt, hand_10.txt, hand_11.txt, hand_12.txt, hand_13.txt, hand_14.txt, hand_15.txt, random_00.txt, random_01.txt, random_02.txt, random_03.txt, random_04.txt, random_05.txt, random_06.txt, random_07.txt, random_08.txt, random_09.txt, random_10.txt, random_11.txt, random_12.txt, random_13.txt, random_14.txt, random_15.txt, random_16.txt, random_17.txt, random_18.txt, random_19.txt, random_20.txt, random_21.txt, random_22.txt, random_23.txt, random_24.txt, random_25.txt, random_26.txt, random_27.txt, random_28.txt, random_29.txt, random_30.txt, random_31.txt, random_32.txt, random_33.txt, random_34.txt, random_35.txt |
| ケース名 | 結果 | 実行時間 | メモリ |
|---|---|---|---|
| example_00.txt | AC | 1 ms | 3388 KiB |
| example_01.txt | AC | 1 ms | 3456 KiB |
| hand_00.txt | AC | 187 ms | 29320 KiB |
| hand_01.txt | AC | 141 ms | 27400 KiB |
| hand_02.txt | AC | 128 ms | 29552 KiB |
| hand_03.txt | AC | 189 ms | 34300 KiB |
| hand_04.txt | AC | 177 ms | 34332 KiB |
| hand_05.txt | AC | 18 ms | 6248 KiB |
| hand_06.txt | AC | 46 ms | 10208 KiB |
| hand_07.txt | AC | 156 ms | 29392 KiB |
| hand_08.txt | AC | 119 ms | 23212 KiB |
| hand_09.txt | AC | 71 ms | 13452 KiB |
| hand_10.txt | AC | 137 ms | 29324 KiB |
| hand_11.txt | AC | 104 ms | 27376 KiB |
| hand_12.txt | AC | 103 ms | 27300 KiB |
| hand_13.txt | AC | 91 ms | 29488 KiB |
| hand_14.txt | AC | 185 ms | 34344 KiB |
| hand_15.txt | AC | 189 ms | 34180 KiB |
| random_00.txt | AC | 211 ms | 29520 KiB |
| random_01.txt | AC | 208 ms | 29664 KiB |
| random_02.txt | AC | 201 ms | 29596 KiB |
| random_03.txt | AC | 216 ms | 29612 KiB |
| random_04.txt | AC | 202 ms | 29368 KiB |
| random_05.txt | AC | 204 ms | 29460 KiB |
| random_06.txt | AC | 158 ms | 29252 KiB |
| random_07.txt | AC | 153 ms | 29252 KiB |
| random_08.txt | AC | 151 ms | 29260 KiB |
| random_09.txt | AC | 172 ms | 29272 KiB |
| random_10.txt | AC | 171 ms | 29200 KiB |
| random_11.txt | AC | 164 ms | 29516 KiB |
| random_12.txt | AC | 156 ms | 29280 KiB |
| random_13.txt | AC | 155 ms | 29428 KiB |
| random_14.txt | AC | 150 ms | 29480 KiB |
| random_15.txt | AC | 172 ms | 29304 KiB |
| random_16.txt | AC | 165 ms | 29372 KiB |
| random_17.txt | AC | 171 ms | 29284 KiB |
| random_18.txt | AC | 11 ms | 4784 KiB |
| random_19.txt | AC | 12 ms | 4980 KiB |
| random_20.txt | AC | 22 ms | 6316 KiB |
| random_21.txt | AC | 22 ms | 6420 KiB |
| random_22.txt | AC | 22 ms | 6080 KiB |
| random_23.txt | AC | 27 ms | 6632 KiB |
| random_24.txt | AC | 48 ms | 8812 KiB |
| random_25.txt | AC | 34 ms | 7244 KiB |
| random_26.txt | AC | 56 ms | 8732 KiB |
| random_27.txt | AC | 40 ms | 8668 KiB |
| random_28.txt | AC | 68 ms | 9384 KiB |
| random_29.txt | AC | 68 ms | 10832 KiB |
| random_30.txt | AC | 209 ms | 28000 KiB |
| random_31.txt | AC | 86 ms | 20224 KiB |
| random_32.txt | AC | 163 ms | 21516 KiB |
| random_33.txt | AC | 141 ms | 28476 KiB |
| random_34.txt | AC | 155 ms | 19792 KiB |
| random_35.txt | AC | 138 ms | 20804 KiB |