結果
| 問題 |
No.1565 Union
|
| コンテスト | |
| ユーザー |
Flkanjin
|
| 提出日時 | 2022-01-05 16:51:22 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 292 ms / 2,000 ms |
| コード長 | 2,697 bytes |
| コンパイル時間 | 1,858 ms |
| コンパイル使用メモリ | 151,040 KB |
| 最終ジャッジ日時 | 2025-01-27 08:49:02 |
|
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 27 |
ソースコード
#define _USE_MATH_DEFIMES
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <climits>
#include <clocale>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <regex>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
const int MOD = 1'000'000'007;
const int MOD2 = 998'244'353;
const int INF = 1'000'000'000; //1e9
const int NIL = -1;
const long long LINF = 1'000'000'000'000'000'000; // 1e18
const long double EPS = 1E-10L;
template<class T, class S> inline bool chmax(T &a, const S &b){
if(a < b){a = b; return true;}
return false;
}
template<class T, class S> inline bool chmin(T &a, const S &b){
if(b < a){a = b; return true;}
return false;
}
template<class T, class Container> inline bool exist(Container &s, const T &e){
return (s.find(e) != std::end(s));
}
template<class T> inline bool inside(T x, T lx, T rx){
return (std::clamp(x, lx, rx) == x);
}
template<class T> inline bool inside(T x, T y, T lx, T rx, T ly, T ry){
return inside(x, lx, rx) && inside(y, ly, ry);
}
void dijkstraWithoutCost(int s, std::vector<std::vector<int>> &G, std::vector<long long> &d, std::vector<int> &prv){
//pair<int, int> first: 距離 second: 頂点
std::priority_queue<std::pair<long long, int>,
std::vector<std::pair<long long, int>>,
std::greater<std::pair<long long, int>>> que;
int V{int(G.size())};
d.resize(V, LINF+3);
prv.resize(V, NIL);
d[s] = 0;
que.emplace(0, s);
while(!que.empty()){
std::pair<long long, int> p{que.top()}; que.pop();
int v{p.second};
if(d[v] < p.first) continue;
for(int &u: G[v]){
if(d[u] > d[v] + 1){
d[u] = d[v] + 1;
prv[u] = v;
que.emplace(d[u], u);
}
}
}
}
int main(){
int N, M; std::cin >> N >> M;
std::vector<std::vector<int>> G(N);
{
int a, b;
for(int i{0}; i < M; ++i){
std::cin >> a >> b; --a; --b;
G[a].push_back(b);
G[b].push_back(a);
}
}
std::vector<long long> d;
std::vector<int> prv;
dijkstraWithoutCost(0, G, d, prv);
std::cout << (d[N-1] <= N ? d[N-1] : -1) << std::endl;
return 0;
}
Flkanjin