求二分图的最小点覆盖集,并输出

对于每一个a[i][j]=1,我们从行i-->列j建立一条边

显然,这张图是一张二分图。左边的节点代表删除哪一行,右边的节点代表删除哪一列。中间的边代表所有a[i][j]为1的点。

现在,我们需要做的事情就是找出最少的点,使这些点覆盖住所有的边(即删去哪几行哪几列,没有士兵)

最少的点,使这些点覆盖住所有的边   这个东西是最小点覆盖集。

对于一张二分图来说,在数量上,最小点覆盖集=最大匹配

如果 最大匹配>坦克数量,那么输出无解

剩下的情况就是有解了,如何寻找解?这问题困扰了我很久......最后还是看了别人的博客。

此外,这题目点最多有2000个,为什么匈牙利算法可以AC......不是o(n^3)效率的吗......

详见北京大学神犇Matrix67的讲解http://blog.csdn.net/niushuai666/article/details/7036897
#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<algorithm>
using namespace std; const int MAXN = + ;
int nx, ny;
int g[MAXN][MAXN];
int cx[MAXN], cy[MAXN];
int mk[MAXN];
int ROW, COLUMN, N;
char s[MAXN][MAXN];
vector<int>Gl[MAXN];
vector<int>Gr[MAXN];
vector<int> ansr;
vector<int> ansc;
int flagl[MAXN], flagr[MAXN];
int flag[MAXN];
int mat[MAXN][MAXN]; int path(int u)
{
for (int v = ; v<ny; v++)
{
if (g[u][v] && !mk[v])
{
mk[v] = ;
if (cy[v] == - || path(cy[v]))
{
cx[u] = v;
cy[v] = u;
return ;
}
}
}
return ;
} int MaxMatch()
{
int res = ;
memset(cx, -, sizeof(cx));
memset(cy, -, sizeof(cy));
for (int i = ; i<nx; i++)
{
if (cx[i] == -)
{
memset(mk, , sizeof(mk));
res = res + path(i);
}
}
return res;
} void dfs(int now, int x)
{
if (x==)
{
flagl[now] = ;
for (int i = ; i<Gl[now].size(); i++)
if (flagr[Gl[now][i]] == && cx[now] == Gl[now][i])
dfs(Gl[now][i], ); }
else
{
flagr[now] = ;
for (int i = ; i<Gr[now].size(); i++)
if (flagl[Gr[now][i]] == && cy[now] != Gr[now][i])
dfs(Gr[now][i], );
}
} int main()
{
while (~scanf("%d%d%d", &ROW, &COLUMN, &N))
{
for (int i = ; i < ROW; i++) scanf("%s", s[i]);
memset(g, , sizeof g);
for (int i = ; i<=ROW; i++) Gl[i].clear();
for (int i = ; i<=COLUMN; i++) Gr[i].clear();
for (int i = ; i < ROW; i++)
for (int j = ; j < COLUMN; j++)
if (s[i][j] == '')
{
g[i][j] = ;
Gl[i].push_back(j);
Gr[j].push_back(i);
}
nx = ROW;
ny = COLUMN;
int ans = MaxMatch();
if (ans>N) printf("NOT ENOUGH TANK\n");
else
{
printf("%d\n", ans); memset(flagl, , sizeof flagl);
memset(flagr, , sizeof flagr);
memset(flag, , sizeof flag);
memset(mat, , sizeof mat);
ansr.clear();
ansc.clear(); for (int i = ; i<ROW; i++) if (cx[i] != -) flag[cx[i]] = ;
for (int j = ; j<COLUMN; j++) if (!flag[j]) dfs(j, ); for (int i = ; i<ROW; i++) if (flagl[i]) ansr.push_back(i);
for (int i = ; i<COLUMN; i++) if (!flagr[i]) ansc.push_back(i); printf("ROW:");
for (int i = ; i < ansr.size(); i++) printf(" %d", ansr[i]+);
printf("\n"); printf("COLUMN:");
for (int i = ; i < ansc.size(); i++) printf(" %d", ansc[i]+);
printf("\n"); }
}
return ;
}

HUST 1027 Enemy Target!的更多相关文章

  1. Unity BehaviorDesigner行为树基础总结

    BehaviorDesigner——行为树,用于控制和实现AI逻辑,类似于这样: 上面这个行为树实现了这样的逻辑: 当Player有Input时按照Input值来移动,无Input时查找最近的可攻击目 ...

  2. UVALive 7146 Defeat the Enemy(贪心+STL)(2014 Asia Shanghai Regional Contest)

    Long long ago there is a strong tribe living on the earth. They always have wars and eonquer others. ...

  3. UVa 7146 Defeat the Enemy(贪心)

    题目链接: 传送门 Defeat the Enemy Time Limit: 3000MS     Memory Limit: 32768 KB Description Long long ago t ...

  4. Defeat the Enemy UVALive - 7146

      Long long ago there is a strong tribe living on the earth. They always have wars and eonquer other ...

  5. UVA LIVE 7146 Defeat the Enemy

    这个题跟codeforces 556 D Case of Fugitive思路一样 关于codeforces 556 D Case of Fugitive的做法的链接http://blog.csdn. ...

  6. UVALive 7146 Defeat The Enemy

    Defeat The Enemy Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu Long long ...

  7. Neither BindingResult nor plain target object for bean name 'command' available as request attribute

    最近用JSR303在表单提交时使用Java Bean Validation验证数据.报错堆栈如下: java.lang.IllegalStateException: Neither BindingRe ...

  8. MySQL中You can't specify target table for update in FROM clause一场

    mysql中You can't specify target table <tbl> for update in FROM clause错误的意思是说,不能先select出同一表中的某些值 ...

  9. jQuery之常用且重要方法梳理(target,arguments,slice,substring,data,trigger,Attr)-(一)

    1.jquery  data(name) data() 方法向被选元素附加数据,或者从被选元素获取数据. $("#btn1").click(function(){ $(" ...

随机推荐

  1. NDK编译应用程序

    Android源码目录下的build/envsetup.sh文件,描述编译的命令 - m:       Makes from the top of the tree. - mm:      Build ...

  2. Html的第一次小结

    一 Html的文档结构  (1) <html> 标记html文件的头标记,没有什么实质性的作用,但是却是必不可少的  (2) <head> 放置html文件信息.如css的一些 ...

  3. 关于C++ const

    1.Const用途 No. 用途 使用范围 参考代码 1 类型检查 参数传递 void func(const int i){ ... } 2 节省空间,避免不必要的内存分配 代替#define #de ...

  4. Android OpenGL ES(五)GLSurfaceView .

    Android OpenGL ES 相关的包主要定义在 javax.microedition.khronos.opengles    GL 绘图指令 javax.microedition.khrono ...

  5. OC 消息机制本质

    转载自:http://m.blog.csdn.net/blog/util_c/10287909 在Objective-C中,message与方法的真正实现是在执行阶段绑定的,而非编译阶段.编译器会将消 ...

  6. 快学Scala-第三章 数组相关操作

    知识点: 1.定长数组 Array val nums = new Array[Int](10) //10个整数的数组,所有元素初始化为0 val a = new Array[String](10) / ...

  7. 给input元素添加float. 去除IE6 下input的空隙

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  8. linux下的vim使用笔记

    环境:window下可以使用gvim编辑软件 学习主要是在ubuntu15敲击命令学习的视频来自于智普教育vim使用视频1. sudo apt show vi 查看安装的vi版本,当然了我的ubunt ...

  9. MYSQL数据库的套接字文件,pid文件,表结构文件

    socket文件:当用Unix域套接字方式进行连接时需要的文件. pid文件:MySQL实例的进程ID文件. MySQL表结构文件:用来存放MySQL表结构定义文件. 套接字文件 Unix系统下本地连 ...

  10. 随机法解决TSP问题

    TSP问题一直是个头疼的问题,但是解决的方法数不胜数,很多的算法也都能解决.百度资料一大堆,但是我找到了代码比较简练的一种.随机法.下面只是个人的看法而已,如果有任何问题虚心接受. 顾名思义,随机法就 ...