标签:rod sed resources contain script follow name php cert
题目链接:https://vjudge.net/problem/HDU-4185
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3016 Accepted Submission(s): 1262
题解:
1.首先为每个油田编号。然后对于当前的油田, 如果它的上面有油田,则在这两个油田之间连一条边,同理其他三个方向。
2.利用匈牙利算法求出最大匹配数,即为答案。
代码如下:
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cstdlib> 5 #include <string> 6 #include <vector> 7 #include <map> 8 #include <set> 9 #include <queue> 10 #include <sstream> 11 #include <algorithm> 12 using namespace std; 13 const int INF = 2e9; 14 const int MOD = 1e9+7; 15 const int MAXN = 600+10; 16 17 int n, N; 18 char a[MAXN][MAXN]; 19 int M[MAXN][MAXN], id[MAXN][MAXN], link[MAXN]; 20 bool vis[MAXN]; 21 22 bool dfs(int u) 23 { 24 for(int i = 1; i<=N; i++) 25 if(M[u][i] && !vis[i]) 26 { 27 vis[i] = true; 28 if(link[i]==-1 || dfs(link[i])) 29 { 30 link[i] = u; 31 return true; 32 } 33 } 34 return false; 35 } 36 37 int hungary() 38 { 39 int ret = 0; 40 memset(link, -1, sizeof(link)); 41 for(int i = 1; i<=N; i++) 42 { 43 memset(vis, 0, sizeof(vis)); 44 if(dfs(i)) ret++; 45 } 46 return ret; 47 } 48 49 int main() 50 { 51 int T; 52 scanf("%d", &T); 53 for(int kase = 1; kase<=T; kase++) 54 { 55 scanf("%d", &n); 56 N = 0; 57 memset(id, -1, sizeof(id)); 58 for(int i = 1; i<=n; i++) 59 { 60 scanf("%s", a[i]+1); 61 for(int j = 1; j<=n; j++) //为每个油田编号 62 if(a[i][j]==‘#‘) 63 id[i][j] = ++N; 64 } 65 66 memset(M, false, sizeof(M)); 67 for(int i = 1; i<=n; i++) 68 for(int j = 1; j<=n; j++) 69 { 70 if(id[i][j]==-1) continue; 71 if(j!=1 && id[i][j-1]!=-1) M[id[i][j]][id[i][j-1]] = true; 72 if(j!=n && id[i][j+1]!=-1) M[id[i][j]][id[i][j+1]] = true; 73 if(i!=1 && id[i-1][j]!=-1) M[id[i][j]][id[i-1][j]] = true; 74 if(i!=n && id[i+1][j]!=-1) M[id[i][j]][id[i+1][j]] = true; 75 } 76 77 int ans = hungary()/2; 78 printf("Case %d: %d\n", kase, ans); 79 } 80 }
标签:rod sed resources contain script follow name php cert
原文地址:http://www.cnblogs.com/DOLFAMINGO/p/7818324.html