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就好. 挑战 ...
随机推荐
- LUA对象
Rectangle = {width = , height = , area = }; function Rectangle:new(o, width, height) o = o or {}; se ...
- 对posintion属性的简单认识,对于还在纠结的同学们,有一定的帮助
position的四个属性值: relative ,absolute ,fixed,static 下面分别讲述这四个属性,以简单代码表示 <div id="parent" ...
- DDD实战成绩管理---需求分析
需求的分析我们采用四色模型.从用户故事中找出MI,然后围绕MI找出其中的role,ppt,des.本次先对两个优先级最高的用户故事进行四色模型建模. 1.用户故事一建模:作为教务处老师,我要建立教学班 ...
- Oracle数据库备份与还原命令
http://www.2cto.com/database/201305/210262.html
- moment.js使用方法总结
Moment.js是一个轻量级的JavaScript时间库,它方便了日常开发中对时间的操作,提高了开发效率.日常开发中,通常会对时间进行下面这几个操作:比如获取时间,设置时间,格式化时间,比较时间等等 ...
- spring 属性文件加载接口---PropertySourceLoader
org.springframework.boot.config Interface PropertySourceLoader 实现类: PropertiesPropertySourceLoader, ...
- Ubuntu常用shell命令
目录 ls cd mkdir mv cp scp rm df du chmod chown chgrp head tail screen apt-get Ubuntu常用shell命令 Ubuntu作 ...
- MYSQL主从复制配置(整理)
MYSQL主从原理及过程 原理 Mysql的 Replication 是一个异步的复制过程(mysql5.1.7以上版本分为异步复制和半同步两种模式),从一个 Mysql instace(我们称之为 ...
- DeepLearning - Overview of Sequence model
I have had a hard time trying to understand recurrent model. Compared to Ng's deep learning course, ...
- 如何使用openstack OCL
本节首先讨论 image 删除操作,然后介绍 OpenStack CLI 的使用方法,最后讨如何 Troubleshoot. Web UI 删除 image admin 登录后,Project -&g ...