题目要求求出图中的一颗生成树,使得最大的边权最小,且满足一级公路的个数>=k。
考虑二分最大边,问题就变为给出的图的生成树中,是否满足所有的边<=val,且一级公路的个数>=k。
所以我们把边按一级公路权值排序,优先选择能构成生成树的一级公路。这样贪心的构造。
# include <cstdio>
# include <cstring>
# include <cstdlib>
# include <iostream>
# include <vector>
# include <queue>
# include <stack>
# include <map>
# include <set>
# include <cmath>
# include <algorithm>
using namespace std;
# define lowbit(x) ((x)&(-x))
# define pi acos(-1.0)
# define eps 1e-
# define MOD
# define INF
# define mem(a,b) memset(a,b,sizeof(a))
# define FOR(i,a,n) for(int i=a; i<=n; ++i)
# define FO(i,a,n) for(int i=a; i<n; ++i)
# define bug puts("H");
# define lch p<<,l,mid
# define rch p<<|,mid+,r
# define mp make_pair
# define pb push_back
typedef pair<int,int> PII;
typedef vector<int> VI;
# pragma comment(linker, "/STACK:1024000000,1024000000")
typedef long long LL;
int Scan() {
int res=, flag=;
char ch;
if((ch=getchar())=='-') flag=;
else if(ch>=''&&ch<='') res=ch-'';
while((ch=getchar())>=''&&ch<='') res=res*+(ch-'');
return flag?-res:res;
}
void Out(int a) {
if(a<) {putchar('-'); a=-a;}
if(a>=) Out(a/);
putchar(a%+'');
}
const int N=;
//Code begin... struct Edge{int u, v, w1, w2;}edge[N<<];
int n, m, k, fa[N];
int find(int x)
{
int s, temp;
for (s=x; fa[s]>=; s=fa[s]) ;
while (s!=x) temp=fa[x], fa[x]=s, x=temp;
return s;
}
void union_set(int x, int y)
{
int temp=fa[x]+fa[y];
if (fa[x]>fa[y]) fa[x]=y, fa[y]=temp;
else fa[y]=x, fa[x]=temp;
}
bool check(int x)
{
int num=;
mem(fa,-);
FO(i,,m) {
int u=find(edge[i].u), v=find(edge[i].v);
if (edge[i].w2>x||u==v) continue;
if (edge[i].w1<=x) ++num;
union_set(u,v);
}
return num>=k&&fa[find()]==-n;
}
bool comp(Edge a, Edge b){return a.w1<b.w1;}
int main ()
{
int u, v, w1, w2;
n=Scan(); k=Scan(); m=Scan();
FO(i,,m) edge[i].u=Scan(), edge[i].v=Scan(), edge[i].w1=Scan(), edge[i].w2=Scan();
sort(edge+,edge+m,comp);
int l=, r=, mid;
while (l<r) {
mid=(l+r)>>;
if (check(mid)) r=mid;
else l=mid+;
}
printf("%d\n",r);
return ;
}