hdu3360National Treasures (最大匹配,拆點法),treasures

來源:互聯網
上載者:User

hdu3360National Treasures (最大匹配,拆點法),treasures
National TreasuresTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1038 Accepted Submission(s): 364


Problem DescriptionThe 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.

InputYour 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.

OutputFor 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 3512 -1 20482 3512 2560 2048512 2560 20480 0

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

Source2009 ANARC 這題看似與方向有關,當前位置只與它保護的位置是單向的,但並非是這樣的,我們來分析一下。從當前題意可知,保護位置與被保護位置是奇偶對立的,所以我們可以先把點分成奇偶兩部分,每一部分內部是不會有關連的,且每個點是獨一無二的,假設是有向的,保護點指向被保護點,建圖,左偶右奇,那麼現在是要求最小頂點覆蓋,先從左指向右求起,求完之後,假設左邊沒有匹配的點是被保護點,之後再求右指向左的匹配,但右邊部分點己經有匹配的點就不用匹配了,那麼也就是求右邊未匹配的點和左邊未匹配的點匹配,這樣匹配是一對一的,所以這樣的匹配也可認為是求左邊未匹配的點與右邊未匹配的點相匹配。----------綜上得知,這是一個無向區配。是一個無向的話就有兩種求法:一種是:把圖分成奇偶兩部分,求最小頂點覆蓋。        另一種是:求最大匹配。
#include<stdio.h>#include<vector>#include<iostream>using namespace std;int match[2505],vist[2505];vector<int>map[2505];int find(int i){    for(int j=0;j<map[i].size();j++)    if(!vist[map[i][j]])    {        vist[map[i][j]]=1;        if(match[map[i][j]]==-1||find(match[map[i][j]]))        {            match[map[i][j]]=i; return 1;        }    }    return 0;}int main(){    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 n,m,mp[55][55],b_w[55][55],bn,wn,k=0;    while(scanf("%d%d",&n,&m)>0&&n+m!=0)    {        for(int i=0;i<n*m;i++)        {            map[i].clear(),match[i]=-1;        }        for(int i=0;i<n;i++)        for(int j=0;j<m;j++)        scanf("%d",&mp[i][j]);        for(int i=0;i<n;i++)        for(int j=0;j<m;j++)        if(mp[i][j]!=-1)        {            int ti,tj;            for(int e=0;e<12;e++)            if(mp[i][j]&(1<<e))            {                ti=i+dir[e][0]; tj=j+dir[e][1];                if(ti>=0&&ti<n&&tj>=0&&tj<m&&mp[ti][tj]!=-1)                {                    map[ti*m+tj].push_back(i*m+j);                    map[i*m+j].push_back(ti*m+tj);                }            }        }        int ans=0;        for(int i=0;i<n*m;i++)        {            for(int j=0;j<n*m;j++)            vist[j]=0;            ans+=find(i);        }        printf("%d. %d\n",++k,ans/2);    }}





聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.