题面
https://www.luogu.org/problem/P2619
题解
注意可行解的区间性质
#include<cstdio> #include<iostream> #include<algorithm> #include<cstring> #define ri register int #define N 200050 #define M 400050 using namespace std; int n,m,k,cnt; int f[N],L[M]; struct edge { int u,v,l; int w; } e[M]; bool cmp(int x,int y){return L[x]<L[y] || (L[x]==L[y] && e[x].w>e[y].w);} int id[N]; bool used[N]; int findroot(int x){ if (f[x]==x) return x; return f[x]=findroot(f[x]); } int check(int mid) { for (ri i=1;i<=m;i++) if (e[i].w) L[i]=e[i].l-mid; else L[i]=e[i].l; for (ri i=1;i<=n;i++) f[i]=i; for (ri i=1;i<=m;i++) used[i]=0; sort(id+1,id+m+1,cmp); int cc=0; cnt=0; for (ri i=1;i<=m;i++) { int r1=findroot(e[id[i]].u),r2=findroot(e[id[i]].v); if (r1==r2) continue; f[r1]=r2; cnt++; used[id[i]]=1; if (e[id[i]].w) cc++; if (cnt==n-1) return cc; } } int main(){ scanf("%d %d %d",&n,&m,&k); for (ri i=1;i<=m;i++) { scanf("%d %d %d %d",&e[i].u,&e[i].v,&e[i].l,&e[i].w); e[i].u++; e[i].v++; e[i].w^=1; } int lb=-101,rb=101,ans; for (ri i=1;i<=m;i++) id[i]=i; while (lb<=rb) { int mid=(lb+rb)/2; if (check(mid)>=k) ans=mid,rb=mid-1; else lb=mid+1; } check(ans); int ret=0; cnt=0; for (ri i=1;i<=n;i++) f[i]=i; for (ri i=1;i<=m;i++) { int r1=findroot(e[id[i]].u),r2=findroot(e[id[i]].v); if (r1==r2) continue; f[r1]=r2; cnt++; ret+=L[id[i]]; if (cnt==n-1) break; } cout<<ret+k*ans<<endl; return 0; }