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

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 <= 212) 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

Source

2009 ANARC
 
 
 
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>

using namespace std;

const int N=2520;

vector<int> vt[N];
int map[60][60],linker[N],vis[N];
int n,m;

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}};

int DFS(int u){
    for(int i=0;i<int(vt[u].size());i++){
        int v=vt[u][i];
        if(!vis[v]){
            vis[v]=1;
            if(linker[v]==-1 || DFS(linker[v])){
                linker[v]=u;
                return 1;
            }
        }
    }
    return 0;
}

int Hungary(){
    int u,ans=0;
    memset(linker,-1,sizeof(linker));
    for(u=1;u<=n*m;u++){
        memset(vis,0,sizeof(vis));
        if(DFS(u))
            ans++;
    }
    return ans;
}

int main(){

    //freopen("input.txt","r",stdin);

    int cases=0;
    while(~scanf("%d%d",&n,&m)){
        if(n==0 && m==0)
            break;
        for(int i=0;i<=n*m;i++)
            vt[i].clear();
        memset(map,-1,sizeof(map));
        for(int i=1;i<=n;i++)
            for(int j=1;j<=m;j++)
                scanf("%d",&map[i][j]);
        for(int i=1;i<=n;i++)
            for(int j=1;j<=m;j++)
                if(map[i][j]!=-1){
                    for(int k=0;k<12;k++)
                        if((map[i][j]>>k) & 1){
                            int x=i+dir[k][0];
                            int y=j+dir[k][1];
                            if(x>=1 && x<=n && y>=1 && y<=m && map[x][y]!=-1){
                                vt[(i-1)*m+j].push_back((x-1)*m+y);
                                vt[(x-1)*m+y].push_back((i-1)*m+j);
                            }
                        }
                }
        printf("%d. %d\n",++cases,Hungary()/2);
    }
    return 0;
}

 

 

 

相关文章: