結果
| 問題 |
No.1473 おでぶなおばけさん
|
| コンテスト | |
| ユーザー |
carrot46
|
| 提出日時 | 2021-04-09 22:33:55 |
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 79 ms / 2,000 ms |
| コード長 | 2,580 bytes |
| コンパイル時間 | 2,362 ms |
| コンパイル使用メモリ | 213,280 KB |
| 最終ジャッジ日時 | 2025-01-20 14:50:27 |
|
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 47 |
ソースコード
#include <bits/stdc++.h>
#include <atcoder/modint>
using namespace atcoder;
//#pragma GCC optimize("O3")
using namespace std;
#define reps(i,s,n) for(int i = s; i < n; i++)
#define rep(i,n) reps(i,0,n)
#define Rreps(i,n,e) for(int i = n - 1; i >= e; --i)
#define Rrep(i,n) Rreps(i,n,0)
#define ALL(a) a.begin(), a.end()
using ll = long long;
using vec = vector<ll>;
using mat = vector<vec>;
ll N,M,H,W,Q,K,A,B;
string S;
using P = pair<ll, ll>;
using tp = tuple<ll, ll, ll>;
const ll INF = (1LL<<61);
template<class T> bool chmin(T &a, const T b){
if(a > b) {a = b; return true;}
else return false;
}
template<class T> bool chmax(T &a, const T b){
if(a < b) {a = b; return true;}
else return false;
}
template<class T> void my_printv(std::vector<T> v,bool endline = true){
if(!v.empty()){
for(std::size_t i{}; i<v.size()-1; ++i) std::cout<<v[i]<<" ";
std::cout<<v.back();
}
if(endline) std::cout<<std::endl;
}
class union_find{
vector<long> parent, tree_size;
public:
union_find(unsigned long _n) : parent(_n), tree_size(_n, 1){
for(unsigned long i = 0; i < _n; ++i)parent[i] = i;
}
ll find(ll x){
while(parent[x] != x)x = parent[x] = parent[parent[x]];
return x;
}
void merge(ll a, ll b){
a = find(a);
b = find(b);
if(a==b) return;
if(tree_size[a] < tree_size[b])swap(a, b);
tree_size[a] += tree_size[b];
parent[b] = a;
return;
}
bool same(ll a, ll b){
a = find(a);
b = find(b);
return a == b;
}
};
//uf.mergeのように使う
//ufはMAX_N+1でとるか、代入時に0-indexedにする
int solve(mat &G){
vec dist(N, INF);
queue<P> que;
que.emplace(0, 0);
while(!que.empty()){
auto [d, v] = que.front();
que.pop();
for(int to : G[v]) if(chmin(dist[to], d + 1)) que.emplace(d + 1, to);
}
return dist[N - 1];
}
int main() {
cin.tie(nullptr);
ios::sync_with_stdio(false);
cin>>N>>M;
mat G(N);
vector<tp> es;
rep(i, M){
int s, t, d;
cin>>s>>t>>d;
--s; --t;
es.emplace_back(s, t, d);
}
sort(ALL(es), [](auto a, auto b){
return get<2>(a) > get<2>(b);
});
union_find uf(N);
int w = -1;
for(auto [s, t, d] : es){
if(d < w) break;
G[s].push_back(t);
G[t].push_back(s);
uf.merge(s, t);
if(w == -1 && uf.same(0, N - 1)) w = d;
}
cout<<w<<' '<<solve(G)<<endl;
}
carrot46