結果
| 問題 |
No.2565 はじめてのおつかい
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2023-12-02 15:49:54 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 88 ms / 2,000 ms |
| コード長 | 1,471 bytes |
| コンパイル時間 | 1,967 ms |
| コンパイル使用メモリ | 175,772 KB |
| 実行使用メモリ | 9,984 KB |
| 最終ジャッジ日時 | 2024-09-26 19:22:15 |
| 合計ジャッジ時間 | 6,251 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 50 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
const int INF = 100000000;
int main() {
int N, M;
cin >> N >> M;
vector<vector<int>> G(N);
for (int i = 0; i < M; i++) {
int u, v;
cin >> u >> v;
u--;
v--;
G[u].push_back(v);
}
queue<int> q;
q.push(0);
vector<int> dist1(N, INF);
dist1[0] = 0;
while (!q.empty()) {
int now = q.front();
q.pop();
for (auto next : G[now]) {
if (dist1[next] == INF) {
q.push(next);
dist1[next] = dist1[now] + 1;
}
}
}
q.push(N - 2);
vector<int> dist2(N, INF);
dist2[N - 2] = 0;
while (!q.empty()) {
int now = q.front();
q.pop();
for (auto next : G[now]) {
if (dist2[next] == INF) {
q.push(next);
dist2[next] = dist2[now] + 1;
}
}
}
q.push(N - 1);
vector<int> dist3(N, INF);
dist3[N - 1] = 0;
while (!q.empty()) {
int now = q.front();
q.pop();
for (auto next : G[now]) {
if (dist3[next] == INF) {
q.push(next);
dist3[next] = dist3[now] + 1;
}
}
}
int ans = dist1[N - 2] + dist2[N - 1] + dist3[0];
ans = min(ans, dist1[N - 1] + dist3[N - 2] + dist2[0]);
if (ans < INF) {
cout << ans << endl;
} else {
cout << -1 << endl;
}
}