結果
問題 | No.2565 はじめてのおつかい |
ユーザー |
![]() |
提出日時 | 2023-12-12 23:10:35 |
言語 | C++23 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 123 ms / 2,000 ms |
コード長 | 2,098 bytes |
コンパイル時間 | 3,511 ms |
コンパイル使用メモリ | 277,604 KB |
実行使用メモリ | 26,496 KB |
最終ジャッジ日時 | 2024-09-27 05:04:46 |
合計ジャッジ時間 | 6,880 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 50 |
ソースコード
#include<bits/stdc++.h>namespace {#pragma GCC diagnostic ignored "-Wunused-function"#include<atcoder/all>#pragma GCC diagnostic warning "-Wunused-function"using namespace std;using namespace atcoder;#define rep(i,n) for(int i = 0; i < (int)(n); i++)#define rrep(i,n) for(int i = (int)(n) - 1; i >= 0; i--)#define all(x) begin(x), end(x)#define rall(x) rbegin(x), rend(x)template<class T> bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; } else return false; }template<class T> bool chmin(T& a, const T& b) { if (b < a) { a = b; return true; } else return false; }using ll = long long;using P = pair<int,int>;using VI = vector<int>;using VVI = vector<VI>;using VL = vector<ll>;using VVL = vector<VL>;constexpr int INF = 1001001001;template <int k = 2, class T> // cost in [0, k)vector<int> kBFS(const vector<vector<pair<int, T>>>& g, int s = 0) {static_assert(k >= 1);const int n = g.size();vector<int> dist(n, INF);vector<int> q[2 * k];dist[s] = 0;q[0].emplace_back(s);for (int dist_cur = 0, ptr = 0;;) {const auto qb = q + ptr;while (!qb[0].empty()) {int u = qb[0].back();qb[0].pop_back();if (dist[u] != dist_cur) continue;for (auto [v, c] : g[u]) {int nd = dist_cur + c;if (nd < dist[v]) {dist[v] = nd;qb[c].emplace_back(v);}}}do {dist_cur++;ptr++;if (ptr == 2 * k) return dist;} while (q[ptr].empty());if (ptr > k) {for (int i = ptr; i < 2 * k; i++) {swap(q[i], q[i - ptr]);}ptr = 0;}}}} int main() {ios::sync_with_stdio(false);cin.tie(0);int n, m;cin >> n >> m;vector<vector<P>> to(4 * n);rep(_, m) {int u, v;cin >> u >> v;u--, v--;rep(s, 4) to[n * s + u].emplace_back(n * s + v, 1);}rep(s0, 2) to[n * s0 + n - 2].emplace_back(n * (s0 + 2) + n - 2, 0);rep(s1, 2) to[n * (2 * s1) + n - 1].emplace_back(n * (2 * s1 + 1) + n - 1, 0);int ans = kBFS(to)[3 * n];if (ans == INF) ans = -1;cout << ans << '\n';}