結果
| 問題 |
No.2565 はじめてのおつかい
|
| コンテスト | |
| ユーザー |
*yudai25n
|
| 提出日時 | 2023-12-02 16:31:44 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 98 ms / 2,000 ms |
| コード長 | 3,365 bytes |
| コンパイル時間 | 4,721 ms |
| コンパイル使用メモリ | 253,740 KB |
| 最終ジャッジ日時 | 2025-02-18 05:29:13 |
|
ジャッジサーバーID (参考情報) |
judge4 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 50 |
ソースコード
#include <bits/stdc++.h>
#include <atcoder/all>
#define OVERLOAD_REP(_1, _2, _3, name, ...) name
#define REP1(i,n) for (auto i = std::decay_t<decltype(n)>{}; (i) != (n); ++(i))
#define REP2(i,l,r) for (auto i = (l); (i) != (r); ++(i))
#define rep(...) OVERLOAD_REP(__VA_ARGS__, REP2, REP1)(__VA_ARGS__)
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend()
using namespace std;
using namespace atcoder;
using ll = long long;
using ull = unsigned long long;
using P = pair<int,int>;
using LP = pair<long long,long long>;
using SP = pair<string,string>;
using vi = vector<int>;
using vl = vector<long long>;
using vs = vector<string>;
using vc = vector<char>;
using mint1 = modint1000000007;
using mint9 = modint998244353;
const int INF = 1e9;
const long long LINF = 1e18;
const int di[] = {0,1,0,-1,-1,1,-1,1};
const int dj[] = {1,0,-1,0,1,1,-1,-1};
template<class T> inline bool chmin(T& a, T b){if(a>b){a=b;return true;}return false;}
template<class T> inline bool chmax(T& a, T b){if(a<b){a=b;return true;}return false;}
void YesNo (bool ok , const string &s , const string &t) {if(ok) cout << s << endl; else cout << t << endl;}
//-----def end-----//
class UGraph {
private:
vector<long long> dist_bfs (int s) {
vector<long long> res(n , LINF);
queue<int> qe;
qe.push(s);
res[s] = 0;
while(!qe.empty()) {
int pos = qe.front(); qe.pop();
for(int u : g[pos]) {
if(res[u] != LINF) continue;
res[u] = res[pos]+1;
qe.push(u);
}
}
return res;
}
bool bipartite_bfs () {
vector<int> color(n , -1);
bool isBipartite = true;
for(int s = 0; s < n; s++) {
if(color[s] != -1) continue;
queue<int> qe;
qe.push(s);
color[s] = 0;
while(!qe.empty()) {
int pos = qe.front(); qe.pop();
for(int u : g[pos]) {
if(color[u] != -1) {
if(color[u] == color[pos]) isBipartite = false;
continue;
}
color[u] = 1-color[pos];
qe.push(u);
}
}
}
return isBipartite;
}
public:
int n , m;
vector<vector<int>> g;
UGraph (int nn) {
g = vector<vector<int>>(nn);
n = nn;
}
void merge (int u , int v) {
g[u].push_back(v);
//g[v].push_back(u);
}
vector<long long> dist(int s) {
return dist_bfs(s);
}
bool bipartite () {
return bipartite_bfs();
}
};
int main () {
int n , m;
cin >> n >> m;
UGraph g(n);
rep(i , m) {
int u , v;
cin >> u >> v;
u--; v--;
g.merge(u , v);
}
auto d0 = g.dist(0) , dn2 = g.dist(n-2) , dn1 = g.dist(n-1);
long long res = LINF;
chmin(res , d0[n-2]+dn2[n-1]+dn1[0]);
chmin(res , d0[n-1]+dn1[n-2]+dn2[0]);
if(res == LINF) res = -1;
cout << res << endl;
return 0;
}
*yudai25n