QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#710559 | #7883. Takeout Delivering | SDNUyuqi | RE | 0ms | 0kb | C++23 | 1.8kb | 2024-11-04 20:26:36 | 2024-11-04 20:26:37 |
answer
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e6+100;
struct EDGE
{
int u,v,w;
bool operator<(const EDGE & e)
{
return w<e.w;
}
};
int n,m;
vector<EDGE>e;
int fa[N],fa1[N],fa2[N];
int find(int x,int faa[])
{
if(faa[x]!=x)
{
faa[x]=find(faa[x],faa);
}
return faa[x];
}
void hb(int x,int y,int faa[])
{
x=find(x,faa);
y=find(y,faa);
if(x==y)
{
return ;
}
faa[x]=y;
}
ll K(int faa[],int id,int x,int y)
{
vector<EDGE>E;
for(int i=1;i<=n;i++)
{
faa[i]=i;
}
for(auto [u,v,w]:e)
{
int sign=find(u,fa);
int sign2=find(v,fa);
if(find(u,fa)==id&&find(v,fa)==id)
{
E.push_back({u,v,w});
}
}
for(auto [u,v,w]:E)
{
hb(u,v,faa);
if(find(x,faa)==find(y,faa))
{
return w;
}
}
return 0;
}
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n>>m;
for(int i=1;i<=n;i++)
{
fa[i]=i;
}
for(int i=1;i<=m;i++)
{
ll u,v,w;
cin>>u>>v>>w;
e.push_back({u,v,w});
}
sort(e.begin(),e.end());
ll an=0;
for(auto [u,v,w]:e)
{
int fu=find(u,fa),fv=find(v,fa);
int f1=find(1,fa),fn=find(n,fa);
int sign1=(f1==fu?u:v);
int signn=(fn==fu?u:v);
if(f1==find(sign1,fa)&&fn==find(signn,fa))
{
int sign=(f1==fu?u:v);
ll a=K(fa1,f1,1,sign);
sign=(fn==fu?u:v);
ll b=K(fa2,fn,n,sign);
an=w+max(a,b);
break;
}
fa[fu]=fv;
}
cout<<an<<endl;
system("pause");
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Dangerous Syscalls
input:
4 6 1 2 2 1 3 4 1 4 7 2 3 1 2 4 3 3 4 9
output:
5