QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#276139 | #7883. Takeout Delivering | 01shijian# | RE | 0ms | 3536kb | C++20 | 3.8kb | 2023-12-05 16:55:39 | 2023-12-05 16:55:39 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#ifdef LOCAL
#include <debugger>
#else
#define debug(...) 42
#endif
template <typename T> void chkmax(T &x, T y) { x = max(x, y); }
template <typename T> void chkmin(T &x, T y) { x = min(x, y); }
struct DSU {
int n;
vector<int> p;
vector<vector<int> > a;
DSU (int _n): n(_n) {
p.resize(n); iota(p.begin(), p.end(), 0);
a.resize(n);
for (int i = 0; i < n; i ++ ) a[i].emplace_back(i);
}
int find (int x) {
if (p[x] != x) p[x] = find(p[x]);
return p[x];
}
// int operator [] (int x) {
// return find(x);
// }
// int merge(int x, int y) {
// int fx = find(x), fy = find(y);
// if (fx == fy) return 0;
// if (fx > fy) swap(fx, fy);
// if (fx == 1 and fy == n) return 2; // over
// if (fx == 1) {
// if (a[fx].size() >= a[fy].size()) {
// for (auto &num: a[fy]) a[fx].emplace_back(num);
// a[fy].clear();
// } else {
// a[fx].swap(a[fy]);
// for (auto &num: a[fy]) a[fx].emplace_back(num);
// a[fy].clear();
// }
// } else if (fy == n) {
// if (a[fy].size() >= a[fx].size()) {
// for (auto &num: a[fx]) a[fy].emplace_back(num);
// a[fx].clear();
// } else {
// a[fy].swap(a[fx]);
// for (auto &num: a[fx]) a[fy].emplace_back(num);
// a[fx].clear();
// }
// } else {
// if (a[fx].size() < a[fy].size()) swap(fx, fy);
// for (auto &num: a[fy]) a[fx].emplace_back(num);
// a[fy].clear();
// p[fy] = fx;
// }
// return true;
// }
// bool same (int x, int y) {
// return find(x) == find(y);
// }
};
void solve() {
int n, m; cin >> n >> m;
struct edge {
int u, v, w;
bool operator < (const edge &T) const {
return w < T.w;
}
};
vector<vector<pair<int, int> > > son(n + 1);
vector<edge> e(m);
for (auto &[u, v, w]: e) {
cin >> u >> v >> w;
son[u].push_back(make_pair(v, w));
son[v].push_back(make_pair(u, w));
}
DSU d(n + 1);
sort(e.begin(), e.end());
int ret = INT_MAX;
set<int> s;
for (int i = 0; i < m; i ++ ) {
auto [x, y, W] = e[i];
int fx = d.find(x), fy = d.find(y);
if (fx == fy) continue; //不需要合并 因此不需要更新答案
if (fx > fy) swap(fx, fy); //fx fy
if (fx == 1 and fy == n) break ; // over
if (fx == 1) {
//fy 的 点 都 加进去
for (auto &u: d.a[fy]) {
for (auto &[v, w]: son[u]) {
int fv = d.find(v);
if (fv != n) { continue; }
s.insert(w);
}
}
if (d.a[fx].size() >= d.a[fy].size()) {
for (auto &num: d.a[fy]) d.a[fx].emplace_back(num);
d.a[fy].clear();
} else {
d.a[fx].swap(d.a[fy]);
for (auto &num: d.a[fy]) d.a[fx].emplace_back(num);
d.a[fy].clear();
}
} else if (fy == n) {
for (auto &u: d.a[fx]) {
for (auto &[v, w]: son[u]) {
int fv = d.find(v);
if (fv != 1) { continue; }
s.insert(w);
}
}
if (d.a[fy].size() >= d.a[fx].size()) {
for (auto &num: d.a[fx]) d.a[fy].emplace_back(num);
d.a[fx].clear();
} else {
d.a[fy].swap(d.a[fx]);
for (auto &num: d.a[fx]) d.a[fy].emplace_back(num);
d.a[fx].clear();
}
} else {
if (d.a[fx].size() < d.a[fy].size()) swap(fx, fy);
for (auto &num: d.a[fy]) d.a[fx].emplace_back(num);
d.a[fy].clear();
d.p[fy] = fx;
}
if (not s.empty()) {
chkmin(ret, W + *s.begin());
}
}
assert(ret != INT_MAX);
cout << ret << "\n";
}
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3536kb
input:
4 6 1 2 2 1 3 4 1 4 7 2 3 1 2 4 3 3 4 9
output:
5
result:
ok 1 number(s): "5"
Test #2:
score: -100
Runtime Error
input:
300000 299999 80516 80517 597830404 110190 110191 82173886 218008 218009 954561262 250110 250111 942489774 66540 66541 156425292 34947 34948 239499776 273789 273790 453201232 84428 84429 439418398 98599 98600 326095035 55636 55637 355015760 158611 158612 684292473 43331 43332 43265001 171621 171622 ...