QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#281855 | #7883. Takeout Delivering | xianggui | WA | 134ms | 15448kb | C++20 | 1.9kb | 2023-12-10 23:32:20 | 2023-12-10 23:32:21 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
struct Edge{
int x,y,val;
}ed[1000010];
int n,m;
int ans;
int fa[300010];
void init()
{
for(int i=1;i<=n;i++) fa[i]=i;
}
int Find(int x)
{
if(fa[x]==x) return x;
return fa[x]=Find(fa[x]);
}
void Merge(int x,int y)
{
x=Find(x);
y=Find(y);
if(x!=y) fa[x]=y;
}
bool cmp(Edge x,Edge y)
{
return x.val<y.val;
}
int check(int mid)
{
init();
Merge(ed[mid].x, ed[mid].y);
if(Find(1)==Find(n)) return 0;
for(int i=1;i<mid;i++)
{
Merge(ed[i].x, ed[i].y);
if(Find(1)==Find(n)) return i;
}
return -1;
}
int si[300010];
vector<pair<int,int> > vec;
int find(int x)
{
if(fa[x]==x) return x;
return find(fa[x]);
}
void merge(int x,int y)
{
x=find(x);
y=find(y);
if(si[x]>si[y]) swap(x, y);
vec.push_back(make_pair(x, fa[x]));
if(x==y) return ;
fa[x]=y;
si[y]+=si[x];
}
void del()
{
auto tem=vec.back();
int x=tem.first;
int y=tem.second;
vec.pop_back();
if(y==fa[x]) return ;
fa[x]=x;
si[y]-=si[x];
}
bool doit(int x,int y,int mid)
{
int xx=find(ed[mid].x);
int yy=find(ed[mid].y);
if((xx==x&&yy==y)||(xx==y&&yy==x)) return 1;
return 0;
}
int main(){
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(false);
cin>>n>>m;
for(int i=1;i<=m;i++) cin>>ed[i].x>>ed[i].y>>ed[i].val;
sort(ed+1,ed+1+m,cmp);
int l=1,r=m,nr,nl;
while(l<=r)
{
int mid=(l+r)>>1;
int tem=check(mid);
if(tem!=-1)
{
nl=tem;
nr=mid;
r=mid-1;
}
else l=mid+1;
}
init();
for(int i=1;i<=nl;i++)
{
merge(ed[i].x, ed[i].y);
}
ans=ed[nr].val+ed[nl].val;
while(nl)
{
del();
nl--;
int x=find(1);
int y=find(n);
while(nr<m&&!doit(1, n, nr))
{
if(ed[nl-1].val+ed[nr].val>ans) break;
nr++;
}
if(nr>m) break;
ans=min(ans,ed[nl].val+ed[nr].val);
}
cout<<ans;
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 7716kb
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: 134ms
memory: 15448kb
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:
999996532
result:
wrong answer 1st numbers differ - expected: '1999991697', found: '999996532'