HDU 3360 National Treasures 奇偶匹配最小点覆盖

题目来源:HDU 3360 National Treasures

题意:如果a[i][j] != -1 把他转成二进制 最多有12位 代表题目那张图的12个位置 如果对应位是1 说明在那里放一个守卫可以看住a[i][j]位置上的这个东西

思路:明显死最小点覆盖 奇偶匹配建图

#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
const int maxn = 55;
int vis[maxn*maxn];
int y[maxn*maxn];
vector <int> G[maxn*maxn];
int n, m;
int a[maxn][maxn];
int dir[12][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};
bool dfs(int u)
{
    for(int i = 0; i < G[u].size(); i++)
    {
        int v = G[u][i];
        if(vis[v])
            continue;
        vis[v] = true;
        if(y[v] == -1 || dfs(y[v]))
        {
            y[v] = u;
            return true;
        }
    }
    return false;
}
int match()
{
    int ans = 0;
    memset(y, -1, sizeof(y));
    for(int i = 0; i < n; i++)
    {
    	for(int j = 0; j < m; j++)
        {
        	if((i+j)%2)
			{
				memset(vis, 0, sizeof(vis));
        		if(dfs(i*m+j))
        		    ans++;
			}
        }
    }
    return ans;
}

int main()
{
	int cas = 1;
	while(scanf("%d %d", &n, &m) && (n||m))
	{
		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*m; i++)
			G[i].clear();
		for(int i = 0; i < n; i++)
		{
			for(int j = 0; j < m; j++)
			{
				int x = a[i][j];
				if(x == -1)
					continue;
				for(int k = 0; k < 12; k++, x >>= 1)
				{
					if(!x)
						break;
					if(!(x&1))
						continue;

					int xx = i + dir[k][0];
					int yy = j + dir[k][1];
					if(xx < 0 || xx >= n || yy < 0 || yy >= m)
						continue;
					if(a[xx][yy] == -1)
						continue;
					if((i+j)%2)
						G[i*m+j].push_back(xx*m+yy);
					else
						G[xx*m+yy].push_back(i*m+j);
				}
			}
		}
		printf("%d. %d\n", cas++, match());
	}
    return 0;
}

HDU 3360 National Treasures 奇偶匹配最小点覆盖

时间: 2024-08-05 04:20:30

HDU 3360 National Treasures 奇偶匹配最小点覆盖的相关文章

HDU 3360 National Treasures 奇偶匹配的最低点覆盖

标题来源:pid=3360">HDU 3360 National Treasures 意甲冠军:假设a[i][j] != -1 把他转成二进制 最多有12位 代表题目那张图的12个位置 假设相应位是1 说明在那里放一个守卫能够看住a[i][j]位置上的这个东西 思路:明显死最小点覆盖 奇偶匹配建图 #include <cstdio> #include <cstring> #include <vector> using namespace std; con

HDU 3360 National Treasures

National Treasures Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Submission(s) : 3   Accepted Submission(s) : 1 Font: Times New Roman | Verdana | Georgia Font Size: ← → Problem Description The great hall of th

hdu - 1150 Machine Schedule (二分图匹配最小点覆盖)

http://acm.hdu.edu.cn/showproblem.php?pid=1150 有两种机器,A机器有n种模式,B机器有m种模式,现在有k个任务需要执行,没切换一个任务机器就需要重启一次, 如果任务i在机器A上执行,A机器需要一个对应的模式A,如果在机器B上执行,机器A需要一个模式B. 一直就是机器A在切换模式,现在让你安排一种合理的任务执行顺序,让机器重启次数最少. 每个任务之间连一条边.二分图的最小顶点覆盖就是求最少的点可以连接所有的边,然后转化成最大匹配即可. 这题就是初始状态

HDU 1498 50 years, 50 colors(最小点覆盖,坑题)

50 years, 50 colors Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1635    Accepted Submission(s): 892 Problem Description On Octorber 21st, HDU 50-year-celebration, 50-color balloons floating

hdu 1151 或 poj 1422 二分图 最小点覆盖集

最小点覆盖集的裸题,只要“拆点建边”然后求出最大匹配,则:最小点覆盖集的大小 = 点数 - 最大匹配 1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 using namespace std; 5 6 const int N = 121; 7 const int M = 5000; 8 bool visit[N]; 9 int mark[N]; 10 int head[N]; 11 int

UVA 11419 - SAM I AM(二分图匹配+最小点覆盖)

UVA 11419 - SAM I AM 题目链接 题意:给定一个棋盘,上面有一些目标,现在要放炮,一个炮能打一行或一列,问最少放几个炮及放炮位置 思路:首先是二分图匹配,每个目标行列建边,做二分图匹配就是最少的放炮位置,至于输出方案,利用最小点覆盖的Konig原理去做,详细证明 代码: #include <cstdio> #include <cstring> #include <vector> using namespace std; const int N = 10

二分图匹配 + 最小点覆盖 - Vertex Cover

Vertex Cover Problem's Link Mean: 给你一个无向图,让你给图中的结点染色,使得:每条边的两个顶点至少有一个顶点被染色.求最少的染色顶点数. analyse: 裸的最小点覆盖问题,二分图的最大匹配,直接套模版即可. Time complexity: O(N^2) view code

HDU149850 years, 50 colors(行列匹配+最小点覆盖)

题意:给出一个n*n的矩阵,里面的数字代表气球的颜色,你每次可以一行或者一列里的相同的某一颜色气球,并把它们全部打破,你一共有k次机会,问最后不能被某一位学生在k次操作里打破的气球,按字典序升序输出,没有的话输出-1 思路:我们反过来想,能被学生在K次里打破的话,那么这些气球的分布行列数必然不大于K,我们就以某一色气球的 X,Y建立二分图 ,X,Y对应二分图的左右两边,我们肯定是要选择最少点来覆盖所有的边,最小点覆盖就是最大匹配,然后枚举所有颜色的点即可 #include<iostream>

hdu 1498 50 years, 50 colors 最小点覆盖

50 years, 50 colors Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Problem Description On Octorber 21st, HDU 50-year-celebration, 50-color balloons floating around the campus, it's so nice, isn't it? To celebrate