标签:mes div com scanf 二进制 题解 set 包含 进制
思路:
明显的dp,虽然我想到了二进制模拟,想到了转移,但还是先看了题解,原来真是这样,,,,不是第三题吗?
用f[i]表示,对于前i个罪犯最少需要分几组。
对于每个状态用二进制表示,第i位上1,0表示该集合中是否包含i罪犯。
转移时,枚举中间节点,把f[i]看成所有f[j]+f[i^j]的最小值。(即两个最小的互补子集的和)
#include<iostream> #include<queue> #include<cstdio> #include<algorithm> #include<cstring> using namespace std; const int N= 1<<17; int n,m,k,f[N]; bool map[20][20]; int tot; int main() { scanf("%d%d%d",&n,&m,&k); for(int i=1,x,y;i<=m;i++) { scanf("%d%d",&x,&y); --x,--y; map[x][y]=map[y][x]=1; } memset(f,127,sizeof(f)); for(int i=0;i<(1<<n);i++) { tot=0; for(int j=0;j<n;j++) for(int kk=j;kk<n;kk++) if(map[j][kk]&&((1<<j)&i)&&((1<<kk)&i)) tot++; if(tot<=k) f[i]=1; for(int j=i;j;j=(j-1)&i) f[i]=min(f[i],f[j]+f[i^j]); } cout<<f[(1<<n)-1]; return 0; }
标签:mes div com scanf 二进制 题解 set 包含 进制
原文地址:http://www.cnblogs.com/CLGYPYJ/p/7619830.html