QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#648274 | #7883. Takeout Delivering | ChiTang | WA | 374ms | 82544kb | C++17 | 3.6kb | 2024-10-17 18:03:53 | 2024-10-17 18:03:53 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define PII pair<int, int>
#define fi first
#define se second
const int N = 3e5 + 10, M = 1e6 + 10;
int fa[N]; // 定义父亲
int n, m;
int d1[N], d2[N];
int vis1[N], vis2[N];
struct edge
{
int u, v, w;
};
vector<PII> gg[N];
int l, ls, tot = 0;
edge g[M], gs[M];
void add(int u, int v, int w)
{
l++;
g[l].u = u;
g[l].v = v;
g[l].w = w;
}
void adds(int u, int v, int w)
{
ls++;
gs[ls].u = u;
gs[ls].v = v;
gs[ls].w = w;
}
void addg(int u, int v, int w)
{
gg[u].push_back({v, w});
gg[v].push_back({u, w});
}
// 标准并查集
int findroot(int x) { return fa[x] == x ? x : fa[x] = findroot(fa[x]); }
void Merge(int x, int y)
{
x = findroot(x);
y = findroot(y);
fa[x] = y;
}
bool cmp(edge A, edge B) { return A.w < B.w; }
// Kruskal 算法
void kruskal()
{
// int tot = 0; // 存已选了的边数
int cnt = 0;
for (int i = 1; i <= m; i++)
{
int xr = findroot(g[i].u), yr = findroot(g[i].v);
if (xr != yr)
{ // 如果父亲不一样
Merge(xr, yr); // 合并
tot ++ ; // 边数增加
adds(g[i].u, g[i].v, g[i].w);
tot ++ ;
adds(g[i].v, g[i].u, g[i].w);
addg(g[i].u, g[i].v, g[i].w);
}
}
}
void dfs1(int p, int u, int w)
{
if(vis1[u]) return ;
for(int i = 0; i < gg[u].size(); i ++ )
{
int v = gg[u][i].first, vw = gg[u][i].second;
if(p == v) continue;
if(vis1[v] == 1) continue;
vw = max(vw, w);
d1[v] = vw;
dfs1(u, v, vw);
vis1[v] = 1;
}
}
void dfs2(int p, int u, int w)
{
if(vis2[u]) return ;
for(int i = 0; i < gg[u].size(); i ++ )
{
int v = gg[u][i].first, vw = gg[u][i].second;
if(p == v) continue;
if(vis2[v] == 1) continue;
vw = max(vw, w);
d2[v] = vw;
dfs2(u, v, vw);
vis2[v] = 1;
}
}
void solve()
{
cin >> n >> m;
for (int i = 1; i <= n; i++)
{
fa[i] = i;
}
for (int i = 1; i <= m; i++)
{
int u, v, w;
cin >> u >> v >> w;
add(u, v, w);
}
sort(g + 1, g + m + 1, cmp);
kruskal();
// for (int i = 1; i <= ls; i++)
// {
// cout << gs[i].u << ' ' << gs[i].v << ' ' << gs[i].w << endl;
// }
// for(int i = 1; i <= n; i ++ )
// {
// cout << i << ": ";
// for(int j = 0; j < gg[i].size(); j ++ )
// {
// cout << gg[i][j].first << ' ';
// }
// cout << endl;
// }
dfs1(-1, 1, 0);
// for(int i = 1; i <= n; i ++ )
// {
// cout << d1[i] << ' ';
// }
// cout << endl;
dfs2(-1, n, 0);
// for(int i = 1; i <= n; i ++ )
// {
// cout << d2[i] << ' ';
// }
// cout << endl;
int ans = 0x3f3f3f3f3f3f3f3f;
// cout << ans << endl;
for(int i = 1; i <= ls; i ++ )
{
// cout << gs[i].u << ' ' << gs[i].v << ' ' << gs[i].w << endl;
int u = gs[i].u, v = gs[i].v, w = gs[i].w;
int du1 = d1[u], dun = d2[u];
int dv1 = d1[v], dvn = d2[v];
int t = max(du1, dvn);
if(t <= w)
{
ans = min(du1 + dvn + w, ans);
}
t = max(dun, dv1);
if(t <= w)
{
ans = min(dun + dv1 + w, ans);
}
}
cout << ans << endl;
}
signed main()
{
solve();
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 4ms
memory: 12120kb
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: 374ms
memory: 82544kb
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:
2999971848
result:
wrong answer 1st numbers differ - expected: '1999991697', found: '2999971848'