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

对于每一个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. Parade

    Parade time limit per test 1 second memory limit per test 256 megabytes input standard input output ...

  2. POJ 3368/RMQ/线段数

    题目链接 /* 给出一段序列,询问[L,R]区间内最大相同数的个数. 用一个很巧妙地方法,转化成求区间内的最大值的问题. RMQ维护区间最大值. MAX处理: */ for(int i=1;i< ...

  3. JavaScript(1)——变量、函数声明及作用域

    这是我的第一篇博客文章,本人不才,文笔也不好,所以可能写的有点凌乱.有什么不对的地方还望见谅.不过每天进步一小步,总有一天会迈出那一大步.以下内容是我对变量.函数声明及函数表达式.作用域的理解. [变 ...

  4. java 数据结构 栈的实现

    java数据结构之栈的实现,可是入栈,出栈操作: /** * java数据结构之栈的实现 * 2016/4/26 **/ package cn.Link; public class Stack{ No ...

  5. UVA - 12563 Jin Ge Jin Qu hao (01背包变形)

    此题应该注意两个点,首先背包容量应该缩减为t-1,因为最长的歌不超过三分钟,而劲歌金曲有678s,所以肯定要留出这个时间来.其次注意优先级,保证唱的歌曲数目最多,在此前提下尽可能的延长时间. 处理方法 ...

  6. Python基础学习3---数据结构

    数据结构 数据结构基本上就是---他们是可以处理数据的结构或者说他们是用来存储一组相关数据的. 在python中有三种内建的数据结构-----列表.元组和字典 列表(list) 列表就像是我们要去超市 ...

  7. PAT (Advanced Level) 1108. Finding Average (20)

    简单模拟. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #i ...

  8. RedHat Enterprise Linux AS4&5 安装gcc过程

    三.Gcc安装方法(redhat 4): 一.安装步骤 1.使用which gcc命令查看gcc是否安装安装 2.如若没有安装则下载如下安装包,所需安装包如下 一共需要拷贝以下五个安装包: binut ...

  9. JS页面延迟执行一些方法(整理)

    一般在JS页面延迟执行一些方法.可以使用以下的方法 jQuery.delay()方法简介 http://shawphy.com/2010/11/jquery-delay.html jQuery中que ...

  10. 关于表单提交submit的兼容性问题。

    这里的form 表单 点击下载执行的函数名字是submit,这样不规范,submit是提交表单,函数名字不能取名叫submit,如果取名叫submit会在低版本的浏览器上无法识别,导致直接提交表单,无 ...