QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#278114 | #7883. Takeout Delivering | Vegetable_Dog# | WA | 467ms | 90012kb | C++14 | 1.9kb | 2023-12-07 12:24:02 | 2023-12-07 12:24:03 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int N = 600005;
#define mp make_pair
#define pb push_back
int n,m;
vector< int >G[N];
struct edge{
int u,v;
long long w;
bool operator <(const edge &o)const{
return w<o.w;
}
}e[2000005];
long long p[N];
int f[N];
int g[N][20];
int find(int x){
return (x==f[x]?x:f[x]=find(f[x]));
}
int dep[N];
void dfs(int u){
for(auto v:G[u]){
dep[v]=dep[u]+1;
dfs(v);
}
}
int LCA(int x,int y){
if(x==y)return x;
if(dep[x]<dep[y])swap(x,y);
for(int i=19;i>=0;i--)if(dep[g[x][i]]>=dep[y])x=g[x][i];
if(x==y)return x;
for(int i=19;i>=0;i--)if(g[x][i]!=g[y][i])x=g[x][i]; // fa[x][20]=fa[y][20]=0
return g[x][0];
}
int main(){
cin>>n>>m;
for(int i=1;i<=m;i++){
int u,v;
long long w;
cin>>u>>v>>w;
e[i]=(edge){u,v,w};
}
sort(e+1, e+1+m);
for(int i=1;i<=n+n-1;i++){
f[i]=i;
}
int tot=n;
for(int i=1;i<=m;i++){
if(find(e[i].u)!=find(e[i].v)){
++tot;
g[find(e[i].u)][0]=tot;
G[tot].push_back(find(e[i].u));
G[tot].push_back(find(e[i].v));
// printf("%d -> %d\n",find(e[i].u), tot);
// printf("%d -> %d\n",find(e[i].v), tot);
g[find(e[i].v)][0]=tot;
f[find(e[i].u)]=tot;
f[find(e[i].v)]=tot;
p[tot]=e[i].w;
}
}
for(int j=1;j<20;j++){
for(int i=1;i<=tot;i++){
g[i][j]=g[g[i][j-1]][j-1];
}
}
dep[tot]=1;
dfs(tot);
long long ans = 2000000005;
for(int i=1;i<=m;i++){
// printf("(%d, %d) w = %lld, LCA(1,%d)=%d, LCA(n,%d)=%d\n",e[i].u,e[i].v,e[i].w,e[i].u,LCA(1,e[i].u),e[i].v,LCA(n,e[i].v));
// printf(" 1: %lld, 2: %lld\n",max(p[LCA(1,e[i].v)], p[LCA(n,e[i].u)]) , max(p[LCA(1,e[i].u)], p[LCA(n,e[i].v)]));
long long t1 = max(p[LCA(1,e[i].v)], p[LCA(n,e[i].u)]);
long long t2 = max(p[LCA(1,e[i].u)], p[LCA(n,e[i].v)]);
if(e[i].w>=min(t1,t2))
ans = min(ans,e[i].w+ min( t1,t2 ));
// printf("ans = %lld\n",ans);
}
cout<<ans;
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 5ms
memory: 26588kb
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: 467ms
memory: 90012kb
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:
1925
result:
wrong answer 1st numbers differ - expected: '1999991697', found: '1925'