結果
| 問題 |
No.1473 おでぶなおばけさん
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2021-04-15 12:04:21 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 100 ms / 2,000 ms |
| コード長 | 3,400 bytes |
| コンパイル時間 | 4,149 ms |
| コンパイル使用メモリ | 179,296 KB |
| 実行使用メモリ | 12,032 KB |
| 最終ジャッジ日時 | 2024-07-01 11:15:13 |
| 合計ジャッジ時間 | 8,884 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 47 |
ソースコード
#include <atcoder/all>
#include <bitset>
#include <fstream>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
using namespace atcoder;
//* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ *//
void input() {
}
template <class Cost> struct graph_l {
public :
graph_l(int n) : _n(n), _graph(n), _dist(n, 0) {}
void add (int from, int to, Cost cost){
assert(0 <= from && from < _n);
assert(0 <= to && to < _n);
_graph[from].push_back(_edge{(unsigned int)from, (unsigned int)to, cost});
}
void add_bi (int from, int to, Cost cost){
add(from, to, cost);
add(to, from, cost);
}
Cost get_dist (int v) {
assert(0 <= v && v < _n);
return _dist[v];
}
void dijkstra (int s) {
assert(0 <= s && s < _n);
for (int i = 0 ; i < _n ; ++i) { _dist[i] = DIST_INF; }
auto edge_compare = [] (_edge e1, _edge e2) {
if (e1.cost != e2.cost) return e1.cost > e2.cost;
else if (e1.to != e2.to) return e1.to > e2.to;
else return e1.from > e2.from;
};
priority_queue<_edge, vector<_edge>, function<bool(_edge, _edge)>> edge_que(edge_compare);
for (auto e : _graph[s]) {
edge_que.push(_edge{e.from, e.to, e.cost});
if(_dist[e.to] > e.cost) _dist[e.to] = e.cost;
}
while (!edge_que.empty()) {
_edge p = edge_que.top(); edge_que.pop();
unsigned int v = p.to;
if (_dist[v] < p.cost) continue;
for (auto e : _graph[v]) {
auto next_cost = e.cost + _dist[v];
if (_dist[e.to] <= next_cost) continue;
_dist[e.to] = next_cost;
edge_que.push(_edge{e.from, e.to, _dist[e.to]});
}
}
}
private:
int _n;
const Cost DIST_INF = numeric_limits<Cost>::max();
struct _edge {
unsigned int from;
unsigned int to;
Cost cost;
};
std::vector<std::vector<_edge>> _graph;
std::vector<Cost> _dist;
};
void solve() {
using pii = pair<int, int>;
int N, M; cin >> N >> M;
vector<int> S(M), T(M);
vector<pii> D(M);
for(int i = 0; i < M; i++) {
int s, t, d; cin >> s >> t >> d;
s--; t--;
S[i] = s;
T[i] = t;
D[i] = {d, i};
}
auto compare = [](pii a, pii b) -> bool {
if(a.first != b.first) return a.first > b.first;
else return a.second < b.second;
};
sort(D.begin(), D.end(), compare);
int w = 0;
dsu UF(N);
graph_l<int> G(N);
for(int i = 0; i < M; i++) {
int wi = D[i].first;
int ni = D[i].second;
UF.merge(S[ni], T[ni]);
if(w > 0 && w > wi) break;
if(w == 0 && UF.same(0, N-1)) {
w = wi;
}
G.add_bi(S[ni], T[ni], 1);
}
G.dijkstra(0);
cout << w << " " << G.get_dist(N-1) << endl;
}
//* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ *//
int main() {
std::ifstream in("input.txt");
std::cin.rdbuf(in.rdbuf());
std::cin.tie(0);
ios::sync_with_stdio(false);
input();
solve();
return 0;
}