QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#273142 | #7883. Takeout Delivering | ucup-team484# | WA | 142ms | 51824kb | C++17 | 1.7kb | 2023-12-02 21:33:32 | 2023-12-02 21:33:33 |
Judging History
answer
#include <bits/stdc++.h>
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define st first
#define nd second
using namespace std;
typedef long long ll;
const int mod = 1e9 + 7;
const int N = 1e6 + 5;
int pre[N], suf[N], par[N], s[N];
vector<int> adj[N];
int qry(int x) { return x == par[x] ? x : qry(par[x]); }
void join(int x, int y) {
x = qry(x);
y = qry(y);
if (s[x] > s[y])
swap(x, y);
if (x != y) {
adj[x].push_back(y), adj[y].push_back(x);
s[y] += s[x];
par[x] = y;
}
}
void dfs(int u, int f, int w) {
(f == 1 ? pre : suf)[u] = w;
for (int v: adj[u]) {
if (f == 1 && pre[v] != 0 || f == 2 && suf[v] != 0)
continue;
dfs(v, f, w);
}
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(0);
int n, m; cin >> n >> m;
vector<array<int, 3>> arr;
for (int i = 0; i < m; i++) {
int u, v, w; cin >> u >> v >> w;
arr.push_back({w, u, v});
}
iota(par, par + n + 1, 0);
fill(s, s + n + 1, 1);
sort(all(arr));
for (auto [w, u, v]: arr) {
if (qry(u) == qry(1) || qry(v) == qry(1))
dfs(qry(u) == qry(1) ? v : u, 1, w);
if (qry(u) == qry(n) || qry(v) == qry(n))
dfs(qry(u) == qry(n) ? v : u, 2, w);
join(u, v);
}
int ans = 2'000'000'000;
for (auto [w, u, v]: arr) {
// cout << u << " " << v << " " << pre[u] << " " << suf[v] << endl;
// cout << v << " " << u << " " << pre[v] << " " << suf[u] << endl;
vector<int> tmp = {pre[u], suf[v], w};
sort(all(tmp));
ans = min(ans, tmp[1] + tmp[2]);
tmp = {pre[v], suf[u], w};
sort(all(tmp));
ans = min(ans, tmp[1] + tmp[2]);
}
cout << ans << "\n";
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 35236kb
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
Wrong Answer
time: 142ms
memory: 51824kb
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 ...
output:
1356916107
result:
wrong answer 1st numbers differ - expected: '1999991697', found: '1356916107'