博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU 3360 National Treasures(二分匹配,最小点覆盖)
阅读量:5924 次
发布时间:2019-06-19

本文共 5512 字,大约阅读时间需要 18 分钟。

National Treasures

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 871    Accepted Submission(s): 291

Problem Description
The great hall of the national museum has been robbed few times recently. Everyone is now worried about the security of the treasures on display. To help secure the hall, the museum contracted with a private security company to provide additional guards to stay in the great hall and keep an eye on the ancient artifacts. The museum would like to hire the minimum number of additional guards so that the great hall is secured.
The great hall is represented as a two dimensional grid of R × C cells. Some cells are already occupied with the museum’s guards. All remaining cells are occupied by artifacts of different types (statues, sculptures, . . . etc.) which can be replaced by new hired guards. For each artifact, few other cells in the hall are identified as critical points of the artifact depending on the artifact value, type of vault it is kept inside, and few other factors. In other words, if this artifact is going to stay in the hall then all of its critical points must have guards standing on them. A guard standing in a critical position of multiple artifacts can keep an eye on them all. A guard, however,
can not stand in a cell which contains an artifact (instead, you may remove the artifact to allow the guard to stay there). Also you can not remove an artifact and leave the space free (you can only replace an artifact with a new hired guard).
Surveying all the artifacts in the great hall you figured out that the critical points of any artifact (marked by a 
 ) are always a subset of the 12 neighboring cells as shown in the grid below.
Accordingly, the type of an artifact can be specified as a non-negative integer where the i-th bit is 1 only if critical point number i from the picture above is a critical point of that artifact. For example an artifact of type 595 (in binary 1001010011) can be pictured as shown in the figure below. Note that bits are numbered from right to left (the right-most bit is bit number 1.) If a critical point of an artifact lies outside the hall grid then it is considered secure.
You are given the layout of the great hall and are asked to find the minimum number of additional guards to hire such that all remaining artifacts are secured.
 

 

Input
Your program will be tested on one or more test cases. Each test case is specified using R+1 lines.
The first line specifies two integers (1<= R,C <= 50) which are the dimensions of the museum hall. The next R lines contain C integers separated by one or more spaces. The j-th integer of the i-th row is -1 if cell (i, j) already contains one of the museum’s guards, otherwise it contains an integer (0 <= T <= 2
12) representing the type of the artifact in that cell.
The last line of the input file has two zeros.
 

 

Output
For each test case, print the following line:
k. G
Where k is the test case number (starting at one,) and G is the minimum number of additional guards to hire such that all remaining artifacts are secured.
 

 

Sample Input
1 3 512 -1 2048 2 3 512 2560 2048 512 2560 2048 0 0
 

 

Sample Output
1. 0 2. 2
Hint
The picture below shows the solution of the second test case where the two artifacts in the middle are replaced by guards.
 

 

Source
 

 

Recommend
lcy
 

 

 

题目意思很难懂。

但是看懂了就不难了。

将格子按照奇偶分成两个部分。

 

#include 
#include
#include
#include
using namespace std;const int MAXN = 3000;const int MAXM = 50000;struct Edge{ int to,next;}edge[MAXM];int head[MAXN],tot;void init(){ tot = 0; memset(head,-1,sizeof(head));}void addedge(int u,int v){ edge[tot].to = v;edge[tot].next = head[u]; head[u] = tot++;}int linker[MAXN];bool used[MAXN];int uN;bool dfs(int u){ for(int i = head[u]; i != -1;i = edge[i].next) { int v = edge[i].to; if(!used[v]) { used[v] = true; if(linker[v] == -1 || dfs(linker[v])) { linker[v] = u; return true; } } } return false;}int hungary(){ int res = 0; memset(linker,-1,sizeof(linker)); for(int u = 0;u < uN;u++) { memset(used,false,sizeof(used)); if(dfs(u))res++; } return res;}int dir[][2] = {
{-1,-2},{-2,-1},{-2,1},{-1,2},{
1,2},{
2,1},{
2,-1},{
1,-2},{-1,0},{
0,1},{
1,0},{
0,-1}};int b[60][60];int a[60][60];int main(){ int n,m; int iCase = 0; while(scanf("%d%d",&n,&m) == 2) { iCase ++; if(n == 0 && m == 0)break; uN = 0; int vN = 0; for(int i = 0;i < n;i++) for(int j = 0;j < m;j++) { if((i+j)%2 == 0)b[i][j] = uN++; else b[i][j] = vN++; } init(); for(int i = 0;i < n;i++) for(int j = 0;j < m;j++) scanf("%d",&a[i][j]); for(int i = 0;i < n;i++) for(int j = 0;j < m;j++) { if(a[i][j] == -1)continue; for(int k = 0;k < 12;k++) if(a[i][j] & (1<
= n || ty >= m) continue; if(a[tx][ty]==-1)continue; if((i+j)%2 == 0) addedge(b[i][j],b[tx][ty]); else addedge(b[tx][ty],b[i][j]); } } printf("%d. %d\n",iCase,hungary()); } return 0;}

 

 

 

转载地址:http://vrnvx.baihongyu.com/

你可能感兴趣的文章
CentOS LInux启动关闭和服务管理
查看>>
Eclipse中10个最有用的快捷键组合
查看>>
java与xml
查看>>
Redis Sentinel机制与用法(二)
查看>>
ls命令实际使用
查看>>
磁盘及磁盘阵列系统选择
查看>>
Javascript异步数据的同步处理方法
查看>>
9. Palindrome Number(回文数)(leetcode)
查看>>
MySQL之自定义函数实例讲解
查看>>
用.htaccess获取文件夹和文件名
查看>>
自我提升mysql
查看>>
步步为营之——建造者模式(Builder)
查看>>
快速排序——Java
查看>>
unity游戏与我
查看>>
187. Repeated DNA Sequences
查看>>
PHP数组的定义
查看>>
避免头文件重复包含
查看>>
Oracle:Authid Current_User的使用
查看>>
陈天桥:欣赏360保护隐私 用户安全永远第一
查看>>
JMeter使用技巧
查看>>