HDU4185 Oil Skimming —— 最大匹配
阿新 • • 發佈:2017-11-11
otto collect ica splay map get spl c program 代碼
Input
The input starts with an integer K (1 <= K <= 100) indicating the number of cases. Each case starts with an integer N (1 <= N <= 600) indicating the size of the square grid. Each of the following N lines contains N characters that represent the cells of a row in the grid. A character of ‘#‘ represents an oily cell, and a character of ‘.‘ represents a pure water cell.
Output
For each case, one line should be produced, formatted exactly as follows: "Case X: M" where X is the case number (starting from 1) and M is the maximum number of scoops of oil that may be extracted.
Source
The 2011 South Pacific Programming Contest
題目鏈接:https://vjudge.net/problem/HDU-4185
Oil Skimming
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3016 Accepted Submission(s): 1262
Sample Input 1 6 ...... .##... .##... ....#. ....## ......
Sample Output Case 1: 3
Recommend lcy
題解:
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 }View Code
HDU4185 Oil Skimming —— 最大匹配