UVA 11825 dp、狀態壓縮、二進位制法表示集合
http://vjudge.net/vjudge/contest/view.action?cid=53516#problem/D
Miracle Corporations has a number of system services running in a distributed computer system
which is a prime target for hackers. The system is basically a set of N computer nodes with each of
them running a set of N services. Note that, the set of services running on every node is same
everywhere in the network. A hacker can destroy a service by running a specialized exploit for that
service in all the nodes.
One day, a smart hacker collects necessary exploits for all these N services and launches an attack
on the system. He finds a security hole that gives him just enough time to run a single exploit in
each computer. These exploits have the characteristic that, its successfully infects the computer
where it was originally run and all the neighbor computers of that node.
Given a network description, find the maximum number of services that the hacker can damage.
Input
There will be multiple test cases in the input file. A test case begins with an integer N
(1<=N<=16), the number of nodes in the network. The nodes are denoted by 0 to N - 1. Each of the
following N lines describes the neighbors of a node. Line i (0<=i<N) represents the description of
node i. The description for node i starts with an integer m (Number of neighbors for node i),
followed by m integers in the range of 0 to N - 1, each denoting a neighboring node of node i.
The end of input will be denoted by a case with N = 0. This case should not be processed.
Output
For each test case, print a line in the format, “Case X: Y”, where X is the case number & Y
is the maximum possible number of services that can be damaged.
Sample Input
3
2 1 2
2 0 2
2 0 1
4
1 1
1 0
1 3
1 2
0
Output for Sample Input
Case 1: 3
Case 2: 2
題目大意:(大白書p69)
假設你是一個黑客,侵入了一個有著n臺計算機(編號為0,1,2......n-1)的網路。一共有n種服務,每臺計算機都執行著所有服務。對於每臺計算就,你都可以選擇一項服務,終止這臺計算機的該項服務(如果其中一些服務已經終止,則這些服務繼續處於停止的狀態)。你的目標是讓儘量多的服務完全癱瘓(即沒有任何計算機執行該項服務)
解題思路:
本題的數學模型是:把n個集合p1,p2,......pn分成儘量多組,使得每組中所有集合的並集等於全集。這裡的集合pi就是計算機i及其相鄰計算機的集合,每組對應於題目中的一項服務,注意到n很小,可以用二進位制法表示這些集合,在程式碼中,每個集合pi實際上是一個非負整數。
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn=1<<16+2;
int f[maxn],cover[maxn],p[maxn];
int n;
int main()
{
int tt=0;
while(~scanf("%d",&n))
{
if(n==0)
break;
//二進位制法表示集合p[i];
for(int i=0;i<n;i++)
{
int x,m;
scanf("%d",&m);
p[i]=1<<i;
while(m--)
{
scanf("%d",&x);
p[i]|=(1<<x);
}
}
//cover(s)表示若干pi的集合s中所有pi的並集(二進位制表示),即:這些pi在數值上“按位或”
for(int s=0;s<(1<<n);s++)
{
cover[s]=0;
for(int i=0;i<n;i++)
{
if(s&(1<<i))
cover[s]|=p[i];
}
}
//dp:用F[s]表示子集s最多可以分成多少組,則狀態轉移方程為:f(s)=max(f(s-s0)+1,f(s)).s0是s的子集,cover[s0]等於全集
f[0]=0;
int all=(1<<n)-1;
for(int s=1;s<(1<<n);s++)
{
f[s]=0;
for(int s0=s;s0;s0=(s0-1)&s)//列舉s的子集s0.
{
if(cover[s0]==all)
f[s]=max(f[s],f[s^s0]+1);//f[s^0]即為:f(s-s0).
}
}
printf("Case %d: %d\n",++tt,f[all]);
}
return 0;
}
相關文章
- 動態規劃——用二進位制表示集合的狀態壓縮DP動態規劃
- 簡易狀態壓縮DP
- HDU 5816 Hearthstone(狀態壓縮DP+概率)
- hdu--5418Victor and World+狀態壓縮DP
- bzoj4145: [AMPPZ2014]The Prices(狀態壓縮+Dp)
- 狀態壓縮
- MySQL 壓縮二進位制日誌MySql
- 狀壓 dp
- 狀壓DP
- [狀壓dp] 最短Hamilton路徑(模板題+狀壓dp)
- HDU 3006 The Number of set (狀態壓縮)
- 論文閱讀 狀態壓縮
- POJ3279 Fliptile【狀態壓縮+DFS】
- ACM-ICPC 2018 南京賽區網路預賽__E AC Challenge【狀態壓縮+DP】ACM
- 動態規劃中初識狀態壓縮(入門)動態規劃
- POJ 2777 Count Color (線段樹+狀態壓縮)
- JavaScript 八進位制與二進位制表示法JavaScript
- JavaScript八進位制與二進位制表示法JavaScript
- HDU 1074 Doing Homework(狀壓DP)
- 狀壓DP基礎入門
- 合理安排(狀壓dp,包括技巧)
- 負數的二進位制表示方法
- NOI2001 炮兵陣地(狀壓dp)
- E - Remove Pairs(狀壓dp+博弈論)REMAI
- BZOJ3329: Xorequ(二進位制數位dp 矩陣快速冪)矩陣
- oracle壓縮表(二)Oracle
- Luogu P1777 幫助 題解 [ 紫 ] [ 線性 dp ] [ 狀壓 dp ]
- 分組(狀壓dp+技巧:快速列舉子集)
- 新型冠狀病毒轉二進位制(首發)
- 驗證二進位制數字正規表示式
- UVA 1220 Party at Hali-Bula (樹形DP)
- [DP] 數位DP
- Nginx網路壓縮 CSS壓縮 圖片壓縮 JSON壓縮NginxCSSJSON
- Java使用位域進行多標記(狀態)管理Java
- 05@多用列舉表示狀態、選項、狀態碼
- Redis 的基礎資料結構(二) 整數集合、跳躍表、壓縮列表Redis資料結構
- Codeforces 11D A Simple Task 題解 [ 藍 ] [ 狀壓 dp ]
- 【LeetCode動態規劃#07】01揹包問題一維寫法(狀態壓縮)實戰,其二(目標和、零一和)LeetCode動態規劃
- 字串壓縮(二)之LZ4字串