国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

HDU3360-二分圖最小點覆蓋

2019-11-08 19:25:31
字體:
來源:轉載
供稿:網友

National Treasures

Time Limit: 2000/1000 MS (java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1295    Accepted Submission(s): 469PRoblem 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. GWhere 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.

題目大意:

                 給你一個n*m的矩陣,在該矩陣中放置一些警衛,每個警衛所能守衛的格子為他的二進制中1的位置如上圖,編號1到12分別表示第i位為1所能守衛的格子,矩陣中除了警衛和值為-1外都是文物然后問你最少放多少個警衛才能守住所有的文物

題目思路:

               首先我們考慮怎么建圖,我知道每個警衛所能守衛的格子,我們不妨先將二維坐標轉換成一維坐標,然后每個守衛與他所能守衛的格子連邊,這樣我們就轉換成了求最少的點使得所有的邊的至少一個端點被選中,即最小點覆蓋模型,所以我們只需進行最大匹配就行

AC代碼:

#include<cstring>#include<cstdio>#include<vector>const int maxn = 55;using std::vector;int mp[maxn][maxn],link[maxn*maxn];bool vis[maxn*maxn];vector<int>v[maxn*maxn];int n,m;int fx[14][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};  //記錄12個方向bool dfs(int u){    for(int i=0;i<v[u].size();i++){        int vv = v[u][i];        if(!vis[vv]){            vis[vv]=true;            if(link[vv]==-1||dfs(link[vv])){                link[vv]=u;                return true;            }        }    }    return false;}int main(){    int T=1;    while(scanf("%d%d",&n,&m),n+m){        memset(v,0,sizeof(v));        memset(link,-1,sizeof(link));        for(int i=1;i<=n;i++)for(int j=1;j<=m;j++)scanf("%d",&mp[i][j]);        for(int i=1;i<=n;i++)            for(int j=1;j<=m;j++)                if(mp[i][j]!=-1)                    for(int k=0;k<12;k++)    //枚舉12個方向                        if((mp[i][j]>>k)&1){                    //狀態位為1                            int h = i+fx[k][0],l=j+fx[k][1];                            if(h>=1&&h<=n&&l>=1&&l<=m&&mp[h][l]!=-1){                                int x = (i-1)*m+j,y=(h-1)*m+l;                                v[x].push_back(y);                                v[y].push_back(x);                            }                        }        int ans = 0;        for(int i=1;i<=n*m;i++){            memset(vis,false,sizeof(vis));            if(dfs(i))ans++;        }        printf("%d. %d/n",T++,ans/2);    }    return 0;}


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 禹州市| 海丰县| 湘阴县| 贵州省| 象山县| 罗源县| 林口县| 乌兰县| 德州市| 贵南县| 都兰县| 股票| 习水县| 旌德县| 会同县| 隆德县| 平江县| 抚远县| 江华| 嘉黎县| 潞西市| 凤阳县| 乐东| 栾川县| 郯城县| 璧山县| 海兴县| 永善县| 耿马| 高碑店市| 临桂县| 嘉禾县| 万盛区| 特克斯县| 涟水县| 临夏县| 新野县| 夏河县| 伊金霍洛旗| 广德县| 醴陵市|