poj3041 Asteroids(二分图最小顶点覆盖、二分图匹配)
Description
Fortunately, Bessie has a powerful weapon that can vaporize all the asteroids in any given row or column of the grid with a single shot.This weapon is quite expensive, so she wishes to use it sparingly.Given the location of all the asteroids in the field, find the minimum number of shots Bessie needs to fire to eliminate all of the asteroids.
Input
* Lines 2..K+1: Each line contains two space-separated integers R and C (1 <= R, C <= N) denoting the row and column coordinates of an asteroid, respectively.
Output
Sample Input
3 4
1 1
1 3
2 2
3 2
Sample Output
2
Hint
The following diagram represents the data, where "X" is an asteroid and "." is empty space:
X.X
.X.
.X.
OUTPUT DETAILS:
Bessie may fire across row 1 to destroy the asteroids at (1,1) and (1,3), and then she may fire down column 2 to destroy the asteroids at (2,2) and (3,2).
Source
program rrr(input,output);
const
inf=;
type
etype=record
t,c,rev,next:longint;
end;
var
e:array[..]of etype;
a,cur,d:array[-..]of longint;
q:array[..]of longint;
n,m,i,x,y,h,t,cnt,ans:longint;
procedure ins(x,y,c:longint);
begin
inc(cnt);e[cnt].t:=y;e[cnt].c:=c;e[cnt].next:=a[x];a[x]:=cnt;
end;
procedure add(x,y:longint);
begin
ins(x,y,);ins(y,x,);
e[cnt].rev:=cnt-;e[cnt-].rev:=cnt;
end;
function min(a,b:longint):longint;
begin
if a<b then exit(a) else exit(b);
end;
procedure bfs;
begin
for i:=-n to n+ do d[i]:=-;
h:=;t:=;q[]:=;d[]:=;
while h<t do
begin
inc(h);
i:=a[q[h]];
while i<>inf do
begin
if (d[e[i].t]=-) and (e[i].c>) then
begin
d[e[i].t]:=d[q[h]]+;
inc(t);q[t]:=e[i].t;
end;
i:=e[i].next;
end;
end;
end;
function dfs(k,f:longint):longint;
var
ans,r,i:longint;
begin
if (k=n+) or (f=) then exit(f);
ans:=;i:=cur[k];
while i<>inf do
begin
if (e[i].c>) and (d[e[i].t]=d[k]+) then
begin
r:=dfs(e[i].t,min(f,e[i].c));
dec(e[i].c,r);inc(e[e[i].rev].c,r);
ans:=ans+r;f:=f-r;
if f= then break;
end;
i:=e[i].next;
cur[k]:=i;
end;
if f> then d[k]:=-;
exit(ans);
end;
begin
assign(input,'r.in');assign(output,'r.out');reset(input);rewrite(output);
readln(n,m);
cnt:=;for i:=-n to n+ do a[i]:=inf;
for i:= to n do begin add(,-i);add(i,n+); end;
for i:= to m do begin readln(x,y);add(-x,y); end;
ans:=;
while true do
begin
bfs;
if d[n+]=- then break;
for i:=-n to n+ do cur[i]:=a[i];
ans:=ans+dfs(,inf);
end;
write(ans);
close(input);close(output);
end.
poj3041 Asteroids(二分图最小顶点覆盖、二分图匹配)的更多相关文章
- UVa 1349 (二分图最小权完美匹配) Optimal Bus Route Design
题意: 给出一个有向带权图,找到若干个圈,使得每个点恰好属于一个圈.而且这些圈所有边的权值之和最小. 分析: 每个点恰好属于一个有向圈 就等价于 每个点都有唯一后继. 所以把每个点i拆成两个点,Xi ...
- UVA 1349 Optimal Bus Route Design (二分图最小权完美匹配)
恰好属于一个圈,那等价与每个点有唯一的前驱和后继,这让人想到了二分图, 把一个点拆开,点的前驱作为S集和点的后继作为T集,然后连边,跑二分图最小权完美匹配. 写的费用流..最大权完美匹配KM算法没看懂 ...
- 紫书 例题11-10 UVa 1349 (二分图最小权完美匹配)
二分图网络流做法 (1)最大基数匹配.源点到每一个X节点连一条容量为1的弧, 每一个Y节点连一条容量为1的弧, 然后每条有向 边连一条弧, 容量为1, 然后跑一遍最大流即可, 最大流即是最大匹配对数 ...
- HDU ACM 1054 Strategic Game 二分图最小顶点覆盖?树形DP
分析:这里使用树形DP做. 1.最小顶点覆盖做法:最小顶点覆盖 == 最大匹配(双向图)/2. 2.树形DP: dp[i][0]表示i为根节点,而且该节点不放,所需的最少的点数. dp[i][1]表示 ...
- poj3041 二分图最小顶点覆盖
Asteroids Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 17237 td>Accepted: 9375 ...
- POJ1325 Machine Schedule 【二分图最小顶点覆盖】
Machine Schedule Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 11958 Accepted: 5094 ...
- POJ 2195 Going Home 【二分图最小权值匹配】
传送门:http://poj.org/problem?id=2195 Going Home Time Limit: 1000MS Memory Limit: 65536K Total Submis ...
- [POJ3041] Asteroids(最小点覆盖-匈牙利算法)
传送门 题意: 给一个N*N的矩阵,有些格子有障碍,要求我们消除这些障碍,问每次消除一行或一列的障碍,最少要几次. 解析: 把每一行与每一列当做二分图两边的点. 某格子有障碍,则对应行与列连边. ...
- POJ 3041 Asteroids (对偶性,二分图匹配)
题目:POJ 3041 Asteroids http://poj.org/problem?id=3041 分析: 把位置下标看出一条边,这显然是一个二分图最小顶点覆盖的问题,Hungary就好. 挑战 ...
随机推荐
- 20155320 2016-2017-2 《Java程序设计》第的四周学习总结
20155320 2016-2017-2 <Java程序设计>第的四周学习总结 教材学习内容总结 继承与多态 继承就是子承父类,避免重复定义共同行为,会使用extends关键词,表示会扩展 ...
- LNMP的安装--详细版
一.软件概述 [root@webserver ~]# cat /etc/redhat-release CentOS Linux release (Core) [root@webserver ~]# u ...
- python3工作环境部署+spyder3+jupyter notebook
1.python3安装 1)官网去下载python3.7版本,双击安装,只要注意勾选写到PATH就行,其它直接NEXT. 2)安装完成,CMD键入 python 回车,跳出python界面就是成功. ...
- spring源码-aop增强-5.2
一.aop增强就是针对于不同的切面进行的相关增强,目的当然是更好的支持相关应用和解耦. 二.默认的aop增强类有AspectJMethodBeforeAdvice.AspectJMethodBefor ...
- Django视图层详细介绍
1 视图函数 一个视图函数,简称视图,是一个简单的Python 函数,它接受Web请求并且返回Web响应.响应可以是一张网页的HTML内容,一个重定向,一个404错误,一个XML文档,或者一张图片. ...
- 四、利用EnterpriseFrameWork快速开发基于WCF为中间件的三层结构系统
回<[开源]EnterpriseFrameWork框架系列文章索引> EnterpriseFrameWork框架实例源代码下载: 实例下载 本章内容与上一张<利用Enterprise ...
- 使用闭包的方式实现一个累加函数 addNum
使用闭包的方式实现一个累加函数 addNum,参数为 number 类型,每次返回的结果 = 上一次计算的值 + 传入的值,如: addNum(10); //10 addNum(12); //22 a ...
- Appium 运行脚本报错InvalidSelectorException: Message: Locator Strategy 'css selector' is not supported for (转)
现象:Appium运行脚本报错InvalidSelectorException: Message: Locator Strategy 'css selector' is not supported f ...
- AtCoder Regular Contest 101 D - Median of Medians
二分答案 然后前缀和+树状数组来判断这个答案是否大于等于数 如果我们对于一个查询,如果小于这个数令为1,大于这个数领为-1 将所有前缀和放在树状数组中,就可以查询所有sum_{l} < sum_ ...
- 420. Count and Say【LintCode java】
Description The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, ...