首页 > 代码库 > HDU 5093 二分最大匹配
HDU 5093 二分最大匹配
Battle ships
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 394 Accepted Submission(s): 178
Problem Description
Dear contestant, now you are an excellent navy commander, who is responsible of a tough mission currently.
Your fleet unfortunately encountered an enemy fleet near the South Pole where the geographical conditions are negative for both sides. The floating ice and iceberg blocks battleships move which leads to this unexpected engagement highly dangerous, unpredictable and incontrollable.
But, fortunately, as an experienced navy commander, you are able to take opportunity to embattle the ships to maximize the utility of cannons on the battleships before the engagement.
The target is, arrange as many battleships as you can in the map. However, there are three rules so that you cannot do that arbitrary:
A battleship cannot lay on floating ice
A battleship cannot be placed on an iceberg
Two battleships cannot be arranged in the same row or column, unless one or more icebergs are in the middle of them.
Your fleet unfortunately encountered an enemy fleet near the South Pole where the geographical conditions are negative for both sides. The floating ice and iceberg blocks battleships move which leads to this unexpected engagement highly dangerous, unpredictable and incontrollable.
But, fortunately, as an experienced navy commander, you are able to take opportunity to embattle the ships to maximize the utility of cannons on the battleships before the engagement.
The target is, arrange as many battleships as you can in the map. However, there are three rules so that you cannot do that arbitrary:
A battleship cannot lay on floating ice
A battleship cannot be placed on an iceberg
Two battleships cannot be arranged in the same row or column, unless one or more icebergs are in the middle of them.
Input
There is only one integer T (0<T<12) at the beginning line, which means following T test cases.
For each test case, two integers m and n (1 <= m, n <= 50) are at the first line, represents the number of rows and columns of the battlefield map respectively. Following m lines contains n characters iteratively, each character belongs to one of ‘#’, ‘*’, ‘o’, that symbolize iceberg, ordinary sea and floating ice.
For each test case, two integers m and n (1 <= m, n <= 50) are at the first line, represents the number of rows and columns of the battlefield map respectively. Following m lines contains n characters iteratively, each character belongs to one of ‘#’, ‘*’, ‘o’, that symbolize iceberg, ordinary sea and floating ice.
Output
For each case, output just one line, contains a single integer which represents the maximal possible number of battleships can be arranged.
Sample Input
2
4 4
*ooo
o###
**#*
ooo*
4 4
#***
*#**
**#*
ooo#
Sample Output
3
5
题目意思:
给一个n*m的图,‘*’表示海洋,‘O’表示浮冰,‘#’表示冰山。一艘军舰只能停在海洋上,两艘军舰不能在同一行或同一列,或者在同一行或同一列当且仅当它们之间有冰山使它们隔开。
思路:
白书上有建图模型,挺好的一道二分匹配的题目。想想,设一艘军舰在点map[i][j]处,点i和点j连一条边,若两艘军舰不在同一行,那么不就是和行结点只连一条边么,放置最多的军舰不就相当于最大匹配么。行列缩点建边,求最大匹配即为答案。
代码:
1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #include <iostream> 5 #include <vector> 6 #include <queue> 7 using namespace std; 8 9 #define N 5510 11 int max(int x,int y){return x>y?x:y;}12 int min(int x,int y){return x<y?x:y;}13 int abs(int x,int y){return x<0?-x:x;}14 15 char map[N][N];16 int n, m;17 bool visited[2505];18 int from[2505];19 int a[N][N]; //行缩点20 int b[N][N]; //列缩点21 int cnt_a, cnt_b;22 vector<int>ve[N*N];23 24 int march(int u){25 int i, v;26 for(i=0;i<ve[u].size();i++){27 v=ve[u][i];28 if(!visited[v]){29 visited[v]=true;30 if(from[v]==-1||march(from[v])){31 from[v]=u;32 return 1;33 }34 }35 }36 return 0;37 }38 39 main()40 {41 int t, i, j, k;42 cin>>t;43 while(t--){44 scanf("%d %d",&n,&m);45 memset(map,‘\0‘,sizeof(map));46 memset(a,0,sizeof(a));47 memset(b,0,sizeof(b));48 for(i=1;i<=n;i++) scanf("%s",map[i]+1);49 cnt_a=1;50 for(i=1;i<=n;i++){ //对行缩点51 for(j=1;j<=m;j++){52 if(map[i][j]!=‘#‘){53 if(map[i][j-1]==‘#‘){54 cnt_a++;55 a[i][j]=cnt_a;56 }57 else a[i][j]=cnt_a;58 }59 }60 cnt_a++;61 }62 for(i=0;i<=cnt_a;i++) ve[i].clear();63 cnt_b=1;64 for(j=1;j<=m;j++){ //对列缩点65 for(i=1;i<=n;i++){66 if(map[i][j]!=‘#‘){67 if(map[i-1][j]==‘#‘){68 cnt_b++;69 b[i][j]=cnt_b;70 }71 else b[i][j]=cnt_b;72 }73 }74 cnt_b++;75 }76 for(i=1;i<=n;i++){ //缩点后的行列建边77 for(j=1;j<=m;j++){78 if(map[i][j]==‘*‘){79 ve[a[i][j]].push_back(b[i][j]);80 }81 }82 }83 84 memset(from,-1,sizeof(from));85 int ans=0;86 for(i=1;i<=cnt_a;i++){ //匈牙利求最大匹配87 memset(visited,false,sizeof(visited));88 if(march(i)) ans++;89 }90 printf("%d\n",ans);91 }92 }
HDU 5093 二分最大匹配
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。