QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#283983 | #7883. Takeout Delivering | ContrisElikemouthing# | WA | 253ms | 47308kb | C++20 | 3.3kb | 2023-12-15 20:06:53 | 2023-12-15 20:06:53 |
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()
{
//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++)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;
if(find(u)==find(v))continue;
u=find(u),v=find(v);
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;
if(find(u)==find(v))continue;
u=find(u),v=find(v);
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];
}
}
}
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);
int Ans=2e9+10;
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: 100
Accepted
time: 4ms
memory: 26912kb
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: 0
Accepted
time: 253ms
memory: 47308kb
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:
1999991697
result:
ok 1 number(s): "1999991697"
Test #3:
score: -100
Wrong Answer
time: 211ms
memory: 41140kb
input:
300000 299999 207226 231742 414945003 84591 210444 175968953 46327 51582 612565723 18773 141119 82562646 76139 286963 762958587 131867 224820 928900783 215240 216181 405340417 144725 290878 195350550 267216 268752 846015171 31413 255927 389339642 45219 147512 489502910 113391 215402 555706684 53359 ...
output:
1993277688
result:
wrong answer 1st numbers differ - expected: '1989898633', found: '1993277688'