QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#660945 | #7883. Takeout Delivering | Calculatelove# | WA | 111ms | 48344kb | C++14 | 1.7kb | 2024-10-20 14:00:50 | 2024-10-20 14:00:51 |
Judging History
answer
#include <bits/stdc++.h>
const int N = 300100, M = 1000100;
int n, m;
int trClock;
struct edg {
int x, y, z;
bool operator < (const edg &rhs) const {
return z < rhs.z;
}
} e[M], tr[N];
int fa[N];
int get(int x) {
return fa[x] == x ? x : fa[x] = get(fa[x]);
}
int tot, head[N], edge[N * 2], ver[N * 2], Next[N * 2];
void add_edge(int u, int v, int w) {
ver[++ tot] = v; edge[tot] = w; Next[tot] = head[u]; head[u] = tot;
}
int max_w[2][N];
int dep[2][N];
void dfs(int u, int fu, int type) {
dep[type][u] = dep[type][fu] + 1;
for (int i = head[u]; i; i = Next[i]) {
int v = ver[i], w = edge[i];
if (v == fu) continue;
max_w[type][v] = std::max(max_w[type][u], w);
dfs(v, u, type);
}
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= m; i ++)
scanf("%d%d%d", &e[i].x, &e[i].y, &e[i].z);
std::sort(e + 1, e + 1 + m);
for (int i = 1; i <= n; i ++) fa[i] = i;
for (int i = 1; i <= m; i ++) {
int x = e[i].x, y = e[i].y;
int p = get(x), q = get(y);
if (p == q) continue;
tr[++ trClock] = e[i];
fa[p] = q;
}
for (int i = 1; i < n; i ++) {
int x = tr[i].x, y = tr[i].y, z = tr[i].z;
add_edge(x, y, z), add_edge(y, x, z);
}
dfs(1, 0, 0);
dfs(n, 0, 1);
int ans = 0x7fffffff;
for (int i = 1; i <= m; i ++) {
int x = e[i].x, y = e[i].y, fir = e[i].z;
if (dep[0][x] > dep[1][x]) std::swap(x, y);
int sec = std::max(max_w[0][x], max_w[1][y]);
if (sec > fir) continue;
ans = std::min(ans, fir + sec);
}
printf("%d\n", ans);
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 16076kb
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: 111ms
memory: 48344kb
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:
1999993064
result:
wrong answer 1st numbers differ - expected: '1999991697', found: '1999993064'