zoukankan      html  css  js  c++  java
  • HDU-3360 National Treasures

    给定各个宝物的多个守卫点,求最少移走宝物个数。

    假如某个宝物的守卫点位于另一个宝物的位置上,则两者只能选其一,于是连边,求二分图的最小顶点覆盖。(可证明图内无奇数环)

    //#include <cmath>
    #include <cstdlib>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <fstream>
    #include <iostream>
    
    #define rep(i, l, r) for(int i=l; i<=r; i++)
    #define clr(x, c) memset(x, c, sizeof(x))
    #define N 53
    #define MAX 1<<30
    #define ll long long
    
    using namespace std;
    int read()
    {
    	int x=0, f=1; char ch=getchar();
    	while (ch<'0' || ch>'9') { if (ch=='-') f=-1; ch=getchar(); }
    	while (ch>='0' && ch<='9') { x=x*10+ch-'0'; ch=getchar(); }
    	return x*f;
    }
    
    const 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}};
    
    struct edge{int y1, y2, n;} e[N*N*N*N]; int fir[N][N], en;
    int n, m, k1[N][N], k2[N][N], t, ans, map[N][N];
    bool b[N][N];
    
    void Add(int x1, int x2, int y1, int y2) 
    { 
    	en++, e[en].y1=y1, e[en].y2=y2, e[en].n=fir[x1][x2], fir[x1][x2]=en; 
    	en++, e[en].y1=x1, e[en].y2=x2, e[en].n=fir[y1][y2], fir[y1][y2]=en; 
    }
    
    bool Can(int x, int y) { return x>0 && x<=n && y>0 && y<=m; }
    
    bool Find(int x1, int x2)
    {
    	int o=fir[x1][x2], y1=e[o].y1, y2=e[o].y2;
    	while (o) 
    	{
    		if (!b[y1][y2])
    		{
    			b[y1][y2]=1; 
    			if (!k1[y1][y2] || Find(k1[y1][y2], k2[y1][y2])) { k1[y1][y2]=x1; k2[y1][y2]=x2; return true; }
    		}
    		o=e[o].n, y1=e[o].y1, y2=e[o].y2;
    	}
    	return false;
    }
    
    int main()
    {
    	while(scanf("%d %d", &n, &m) && (n || m))
    	{
    		t++; clr(fir, 0); en=ans=0;
    		rep(i, 1, n) rep(j, 1, m) map[i][j]=read();
    		rep(i, 1, n) rep(j, 1, m)
    		{
    			int a=map[i][j]; if (a<0) continue;
    			rep(o, 0, 11)
    			{
    				if (a%2 && Can(i+dir[o][0], j+dir[o][1]) && map[i+dir[o][0]][j+dir[o][1]]>=0) 
    					Add(i, j, i+dir[o][0], j+dir[o][1]); 
    				a/=2;
    			}
    		}
    		clr(k1, 0); clr(k2, 0);
    		rep(i, 1, n) rep(j, 1, m) if ((i+j)%2)
    		{
    			clr(b, 0); if (Find(i, j)) ans++;
    		}
    		printf("%d. %d
    ", t, ans);
    	}
    	return 0;
    }
  • 相关阅读:
    每天一个linux命令(20):linux chmod命令
    每天一个linux命令(19):Linux 目录结构
    每天一个linux命令(18):find 命令概览
    每天一个linux命令(17):locate 命令
    shiro实战系列(一)之入门实战
    nginx反向代理和tomcat集群(适用于ubutnu16.04及其centos7)
    Ubuntu16.04之开发环境构建
    Drools实战系列(三)之eclipse创建工程
    Drool实战系列(二)之eclipse安装drools插件
    谈谈maven多模块
  • 原文地址:https://www.cnblogs.com/NanoApe/p/4358066.html
Copyright © 2011-2022 走看看