HDU - 5093(二分图经典建图)

网友投稿 197 2022-09-16

HDU - 5093(二分图经典建图)

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.

Input

There is only one integer T (0

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),船可以放到海里,但不能放到冰和山上。船会互相攻击,所以同一列的船必须有山相间隔。问最多放多少船。

思路:

二分图建图之行列建图法----一行变多行,一列变多列。

代码:

#include #include #include #include #include #include #include #include using namespace std;#define ll long longconst int mod = 1e9 + 7;const int maxn = 550;const int maxe = 90050;bool inpath[maxe];int match[maxe];int head[maxe];int edgeNum = 0;struct Edge { int to, next;}edge[maxe];int cntx, cnty;void addEdge(int a, int b) { edge[edgeNum].to = b; edge[edgeNum].next = head[a]; head[a] = edgeNum++;}bool findpath(int k) { for (int i = head[k];i != -1;i = edge[i].next) { int j = edge[i].to; if (!inpath[j]) { inpath[j] = true; if (match[j] == -1 || findpath(match[j])) { match[j] = k;return true; } } } return false;}void hungary() { int cnt = 0; for (int i = 1;i <= cntx;i++) { memset(inpath, 0, sizeof(inpath)); if (findpath(i)) { cnt++; } } cout << cnt << endl;}void init() { memset(inpath, false, sizeof(inpath)); memset(match, -1, sizeof(match)); memset(head, -1, sizeof(head)); edgeNum = 0;}int n, m;char Map[maxn][maxn];int x[maxn][maxn], y[maxn][maxn];void getx(){ cntx = 1; for (int i = 1;i <= n;i++) { for (int j = 1;j <= m;j++) { if (Map[i][j] == '#')cntx++; x[i][j] = cntx; } cntx++; }}void gety(){ cnty = 1; for (int i = 1;i <= m;i++) { for (int j = 1;j <= n;j++) { if (Map[j][i] == '#')cnty++; y[j][i] = cnty; } cnty++; }}void getMap(){ for (int i = 1;i <= n;i++) { for (int j = 1;j <= m;j++) { if (Map[i][j] == '*')addEdge(x[i][j], y[i][j]); } }}int main(){ int t; scanf("%d", &t); while (t--) { init(); scanf("%d%d", &n, &m); for (int i = 1;i <= n;i++) { scanf("%s", Map[i] + 1); } getx(); gety(); getMap(); hungary(); } return 0;}

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:小a与黄金街道(欧拉函数求和)
下一篇:HDU - 4826(dp)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~