天天写算法之National Treasures

这个题目的问题,我一开始百思不得其解,后来反过来想了想,就是求最少的点,把所有的线都占有了,那么就是最小覆盖问题,最小覆盖=最大匹配,emmmmm:
这个题,我有一个地方很不理解,我们原本就有警卫,那么原本的警卫是怎么处理的?感觉这样做,是直接忽略掉原本有的警卫,然后给出了最大覆盖,那么万一原本的警卫占据了一个有用的位置,那么岂不是没有考虑到?
哦~,还是最大覆盖问题,你再给出的线里面根本不包含这个线,那么也就不用覆盖,因此,这样是说得通的,我们要从最小覆盖的角度去思考,而不应该是从匹配的角度去考虑问题。

代码如下:
#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;
}
/*
    题意:在一个n*m的格子中,每个格子有一个数值,-1表示空,其余表示财宝。每个财宝的数值转换成二进制数,
12个二进制位上数值,从右到左,第i个位是1表示图上相应第i序号位置需要有警卫。所有的要求位置有警卫财宝才安全。
财宝可以被警卫替换。问至少需要替换多少财宝才能保证所有财宝的安全。
    方法:需要警戒位置是财宝的讯号对财宝位置讯号建边。由于警戒位置与财宝位置的横纵坐标奇偶相反,可以建得二分图。
对于所建图,根据题意就是找出最少的顶点使得剩余顶点覆盖所有的边,即最小顶点覆盖数为答案。

    最大匹配:二分图G中,找出边数最大的子图M,使得M中各条边均无公共顶点,则M为最大匹配。可用匈牙利算法求得。
    最小顶点覆盖:二分图G中,找出顶点数最少的子图M,使得M中所有的点可以覆盖G中所有的边(一个顶点可以覆盖与它相连的边)。
    最小顶点覆盖=最大匹配
*/

National Treasures

01-01

DescriptionnnThe 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. nThe 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). nSurveying 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. n 2 3 n1 9 4n 12 ⊙ 10 n8 11 5n 7 6 nnAccordingly, 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. n 2 n1 n ⊙ 10 n 5n 7 nnYou 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.nInputnnYour 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. nThe last line of the input file has two zeros. nOutputnnFor each test case, print the following line: nk. G nWhere 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.nSample Inputnn1 3n512 -1 2048n2 3n512 2560 2048n512 2560 2048n0 0nSample Outputnn1. 0n2. 2

没有更多推荐了,返回首页

私密
私密原因:
请选择设置私密原因
  • 广告
  • 抄袭
  • 版权
  • 政治
  • 色情
  • 无意义
  • 其他
其他原因:
120
出错啦
系统繁忙,请稍后再试

关闭