记录编号 366000 评测结果 AAAAAAAAAAAAAAAAAAAA
题目名称 [国家集训队 2012] tree(陈立杰) 最终得分 100
用户昵称 GravatarFoolMike 是否通过 通过
代码语言 C++ 运行时间 8.180 s
提交时间 2017-01-22 12:12:39 内存使用 7.16 MiB
显示代码纯文本
#include<cstdio>
#include<algorithm>
using namespace std;
const int N=2e5+10;
struct edge{int f,t,l;}White[N],Black[N];
int n,m,need,mb,mw,ans[N],cnt[N];
bool cmp(const edge &x,const edge &y){
	return x.l<y.l;
}
int fa[N];
int find(int x){return x==fa[x]?x:fa[x]=find(fa[x]);}
//每条白边加上权值x,白边数和权值和分别为cnt,ans[x+101] 
void solve(int x){
	for (int i=0;i<n;i++) fa[i]=i;
	int pl=1,pr=1;
	for (int i=-101;i<=201;i++){
		for (;White[pl].l+x==i;pl++){
			int a=find(White[pl].f),b=find(White[pl].t);
			if (a!=b) fa[a]=b,ans[x+101]+=i,cnt[x+101]++;
		}
		for (;Black[pr].l==i;pr++){
			int a=find(Black[pr].f),b=find(Black[pr].t);
			if (a!=b) fa[a]=b,ans[x+101]+=i;
		}
	}
}
int main()
{
	freopen("nt2012_tree.in","r",stdin);
	freopen("nt2012_tree.out","w",stdout);
	scanf("%d%d%d",&n,&m,&need);
	for (int i=1;i<=m;i++){
		int f,t,l,tp;
		scanf("%d%d%d%d",&f,&t,&l,&tp);
		if (tp) Black[++mb]=(edge){f,t,l};
			else White[++mw]=(edge){f,t,l};
	}
	sort(Black+1,Black+mb+1,cmp);
	sort(White+1,White+mw+1,cmp);
	Black[mb+1].l=White[mw+1].l=1e9;
	for (int x=-101;x<=101;x++) solve(x);
	int Ans=1e9;
	for (int x=-101;x<101;x++)
		if (cnt[x+101]>=need&&need>=cnt[x+102])
			Ans=min(Ans,ans[x+101]-x*need);
	printf("%d\n",Ans);
	return 0;
}