BZOJ1196[HNOI2006] Highway construction problems
Test instructions
Repair N-1 Road will be n points to connect, each point can be built a road can also be built a second-class highway, requires the first road must have K, requires the most cost of the road to spend the least.
Exercises
The first two minutes of the largest cost, and then decided: first in the premise of not producing the ring (with and check set maintenance) to make every road as far as possible to repair a road, if the last can not constitute a tree to consider the repair of the two-level highway.
Code:
1#include <cstdio>2#include <cstring>3#include <algorithm>4 #defineInc (I,J,K) for (int i=j;i<=k;i++)5 #defineMAXN 105006 using namespacestd;7 8 intn,k,m,u[maxn*2],v[maxn*2],c1[maxn*2],c2[maxn*2],mx,fa[maxn],cnt;9 intFindintx) {returnx==fa[x]?x:fa[x]=find (Fa[x]);}Ten BOOLCheckintLim) { OneInc (I,1, n) fa[i]=i; Cnt=0; AInc (I,1, m)if(c1[i]<=Lim) { - intX=find (U[i]), Y=find (V[i]);if(x==y)Continue; Fa[y]=x; cnt++; - } the if(cnt<k)return 0; -Inc (I,1, m)if(c2[i]<=Lim) { - intX=find (U[i]), Y=find (V[i]);if(x==y)Continue; Fa[y]=x; cnt++; - } + if(cnt<n-1)return 0;return 1; - } + intMain () { Ascanf"%d%d%d", &n,&k,&m); mx=0; atInc (I,1, M-1) scanf ("%d%d%d%d", &u[i],&v[i],&c1[i],&c2[i]), Mx=max (Mx,c1[i]), mx=Max (mx,c2[i]); - intL=0, r=Mx,ans; - while(l<=R) { - intMid= (l+r) >>1; - if(Check (mid)) ans=mid,r=mid-1;ElseL=mid+1; - } inprintf"%d", ans);return 0; -}
20160531
BZOJ1196[HNOI2006] Highway construction problems