QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#284003 | #7883. Takeout Delivering | ContrisElikemouthing# | WA | 0ms | 38332kb | C++20 | 4.0kb | 2023-12-15 20:43:18 | 2023-12-15 20:43:18 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
bool Mbe;
const int maxm=1e6+10,maxn=3e5+10;
int fa[maxn];
inline int find(int x){return x==fa[x]?x:fa[x]=find(fa[x]);}
struct edge{int u,v,w;}E[maxm];
int n,m;
int lk1[maxm],lkn[maxm];
int nxt[maxm<<1],tot,id[maxm<<1];
int head[maxn],tail[maxn];
priority_queue< pair<int,int> > q;
vector<int> vect[maxm];
int tmin[maxm];
bool Med;
signed main()
{
//freopen("data.in","r",stdin);
//freopen("2.out","w",stdout);
//cout<<(&Med-&Mbe)/(1024*1024)<<'\n';
cin>>n>>m;
for(int i=1;i<=n;i++)fa[i]=i;
for(int i=1;i<=m;i++)
{
cin>>E[i].u>>E[i].v>>E[i].w;
if(E[i].u>E[i].v)swap(E[i].u,E[i].v);
}
sort(E+1,E+1+m,[&](edge a,edge b){return a.w<b.w;});
// for(int i=1;i<=m;i++)
// cout<<E[i].u<<' '<<E[i].v<<' '<<E[i].w<<'\n';
for(int i=1;i<=m;i++)lk1[i]=lkn[i]=-1;
for(int i=1;i<=m;i++)
{
int u=E[i].u,v=E[i].v;
++tot;id[tot]=i;nxt[tot]=head[v];
if(!head[v])tail[v]=tot;
head[v]=tot;
++tot;id[tot]=i;nxt[tot]=head[u];
if(!head[u])tail[u]=tot;
head[u]=tot;
}
for(int t=head[1];t;t=nxt[t])
if(lk1[id[t]]==-1)lk1[id[t]]=1;
for(int i=1;i<=m;i++)
{
int u=E[i].u,v=E[i].v,w=E[i].w;
// cout<<"#"<<u<<' '<<v<<' '<<i<<'\n';
if(find(u)==find(v))continue;
//cout<<find(2)<<' '<<find(3)<<'\n';
//cout<<"?"<<u<<' '<<v<<' '<<E[i].w<<'\n';
u=find(u),v=find(v);
if(u>v)swap(u,v);
//cout<<find(u)<<find(v)<<'\n';
fa[v]=u;
if(u==1)
{
for(int t=head[v];t;t=nxt[t])
if(lk1[id[t]]==-1)lk1[id[t]]=i;
}
else
{
if(!head[u])head[u]=head[v],tail[u]=tail[v];
else
{
nxt[tail[u]]=head[v];
tail[u]=tail[v];
}
}
}
for(int i=1;i<=n;i++)fa[i]=i,head[i]=tail[i]=0;
for(int i=1;i<=m;i++)swap(E[i].u,E[i].v);
tot=0;
for(int i=1;i<=m;i++)
{
int u=E[i].u,v=E[i].v;
++tot;id[tot]=i;nxt[tot]=head[v];
if(!head[v])tail[v]=tot;
head[v]=tot;
++tot;id[tot]=i;nxt[tot]=head[u];
if(!head[u])tail[u]=tot;
head[u]=tot;
}
for(int t=head[n];t;t=nxt[t])
if(lkn[id[t]]==-1)lkn[id[t]]=1;
for(int i=1;i<=m;i++)
{
int u=E[i].u,v=E[i].v,w=E[i].w;
//cout<<"?"<<u<<' '<<v<<'\n';
if(find(u)==find(v))continue;
u=find(u),v=find(v);
if(v>u)swap(u,v);
//cout<<"?"<<u<<' '<<v<<'\n';
fa[v]=u;
if(u==n)
{
for(int t=head[v];t;t=nxt[t])
if(lkn[id[t]]==-1)lkn[id[t]]=i;
}
else
{
if(!head[u])head[u]=head[v],tail[u]=tail[v];
else
{
nxt[tail[u]]=head[v];
tail[u]=tail[v];
}
cout<<head[u]<<' '<<tail[u]<<'\n';
}
}
// for(int i=1;i<=m;i++)
// cout<<lk1[i]<<' '<<lkn[i]<<'\n';
for(int i=1;i<=m;i++)
if(lk1[i]!=-1&&lkn[i]!=-1)
tmin[i]=max(lk1[i],lkn[i]),vect[tmin[i]].push_back(i);
//for(int i=1;i<=m;i++)
// cout<<tmin[i]<<'\n';
int Ans=2e9;
for(int i=1;i<=m;i++)
if(E[i].u==n&&E[i].v==1)
Ans=min(Ans,E[i].w);
for(int i=1;i<=n;i++)fa[i]=i;
for(int i=1;i<=m;i++)
{
int u=E[i].u,v=E[i].v;
if(find(u)==find(v))continue;
fa[find(v)]=find(u);
if(find(1)==find(n))Ans=min(Ans,2*E[i].w);
for(int id:vect[i])q.push({-E[id].w,id});
while(q.size())
{
int id=q.top().second;
int u=E[id].u,v=E[id].v;
if(find(u)==find(v))q.pop();
else
{
Ans=min(Ans,E[i].w+E[id].w);
break;
}
}
}
cout<<Ans<<'\n';
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 38332kb
input:
4 6 1 2 2 1 3 4 1 4 7 2 3 1 2 4 3 3 4 9
output:
11 1 11 3 5
result:
wrong answer 1st numbers differ - expected: '5', found: '11'