NOIP 模拟赛 染色

MZOJ

先跑一遍 K r u s a l Krusal Krusal,得出最小值,在考虑将矩形染色

而由于矩形的个数很小,我们可以考虑用状压的方式 0 / 1 0/1 0/1枚举当前这个矩形染还是不染色,然后再来跑 K r u s a l Krusal Krusal或者 P r i m e Prime Prime

直接上代码:

#include<bits/stdc++.h>
#define rep(i,a,b) for(int (i)=(a);(i)<=(b);(i)++)
using namespace std;
const int maxn=6e6+10;
const int maxm=3e3+10;
int m,n,ans=INT_MAX;
int x[maxn],y[maxn];
vector<int> v[maxm];

template <class t> inline void read(t &x) {
	x=0;char ch=getchar();int f=1;
	while(!isdigit(ch)){if(ch=='-') f=-1;ch=getchar();}
	while(isdigit(ch)){x=10*x+ch-'0';ch=getchar();}
	x*=f;
}

int sum=0,cnt=0,fa[maxn],c[maxn],k[maxn];

struct node{
	int v,u,w;
	bool friend operator < (const node &A,const node &B) {
		return A.w<B.w;
	}
}e[maxn];
	
int get_dis(int i,int j) {
	return (x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]);
}
	
inline int find(int x) {
	if(fa[x]==x) return x;
	else return fa[x]=find(fa[x]);
}
	
int Krusal(int flag,int sum,int k) {
	rep(i,1,cnt) {
		int fx=find(e[i].u),fy=find(e[i].v),w=e[i].w;
		if(k==n-1) break;
		if(fx==fy) continue;
		fa[fx]=fy;
		k++;
		if(flag) e[k].u=e[i].u,e[k].v=e[i].v,e[k].w=e[i].w;
		sum+=w;
	}
	return sum;
}

void readdata() {
	read(n);read(m);
	rep(i,1,n) {
		read(x[i]);
		read(y[i]);
	}
	rep(i,1,m) {
		read(k[i]),read(c[i]);
		rep(j,1,k[i]) {int x;read(x);v[i].push_back(x);}
	}
}
	
void work() {
	rep(i,1,n) fa[i]=i;
	rep(i,1,n)
	 rep(j,1,n) {
	 	if(i==j) continue;
	 	e[++cnt].u=i;
	 	e[cnt].v=j;
	 	e[cnt].w=get_dis(i,j);
	}
	sort(e+1,e+1+cnt);
	ans=min(ans,Krusal(true,0,0));cnt=n-1;
	/*printf("%d\n",sum);
	system("pause\n");*/
	rep(s,0,(1<<m)) {
		rep(i,1,n) fa[i]=i;
		int cost=0,tot=0;
		rep(i,1,m)
		 if(s&(1<<(i-1))) {
		 	cost+=c[i];
		 	rep(j,0,k[i]-2) {
		 		int a1=v[i][j],a2=v[i][j+1];
		 		int x=find(v[i][j]),y=find(v[i][j+1]);
		 		if(x!=y) fa[x]=y,++tot;
			}
		 }
		ans=min(ans,Krusal(false,cost,tot));
	}
	printf("%d\n",ans);
}

int main() {
	//freopen("input.txt","r",stdin);
	//freopen("graph.out","w",stdout);
	readdata();
	work();
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值