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就好. 挑战 ...
随机推荐
- 在Linux中安装JDK和IDEA
前言 寒假安装虚拟机的时候我就没有安装好,到学校之后,因为时间紧加上更习惯Windows的操作习惯,我只在Windows上安装了JDK和IDEA,但是随着学习的深入,我发现用虚拟机写命令行.新建jav ...
- 20155332 2016-2017-2《Java程序设计》课程总结
20155332 2016-2017-2<Java程序设计>课程总结 1.每周作业链接汇总 2.博客之最 3.实验链接汇总 博客链接汇总 预备作业1:那些年陪伴我的老师+我期待的师生关系 ...
- 验证码示例代码演示——以php为例
运行 · 修改index.php(图片验证码的生成示例) [html] view plain copy initNECaptcha({ captchaId: 'YOUR_CAPTCHA_ID', // ...
- Windows网络通信(一):socket同步编程
网络通信常用API 1. WSAStartup用于初始化WinSock环境 int WSAStartup( WORD wVersionRequested, LPWSADATA lpWSAData ); ...
- _INTSIZEOF
在_INTSIZEOF中该有的都有了 1.这其中最小非负剩余和最大正余数例子如下: 设n为4,当r为1时,最小非负剩余就是1,最大非正剩余就是1 - 4 = -3,最大正余数为4 - 1 = 3 2. ...
- mongo复杂操作
相比关系型数据库, Array [1,2,3,4,5] 和 Object { 'name':'DragonFire' } 是MongoDB 比较特殊的类型了 特殊在哪里呢?在他们的操作上又有什么需要注 ...
- python数据可视化——matplotlib 用户手册入门:pyplot 画图
参考matplotlib官方指南: https://matplotlib.org/tutorials/introductory/pyplot.html#sphx-glr-tutorials-intro ...
- 算法笔记(c++)--桶排序题目
算法笔记(c++)--桶排序 记得题目是排序,输入n个1-1000的数字然后去重然后排序. 桶排序没毛病 #include<iostream> using namespace std; i ...
- Java跨平台的实现原理
不同操作系统支持的指令集有所差异,只要在不同操作系统上安装对应的jvm,jvm负责把Java字节码翻译成对应机器的二进制码,从而实现java语言的跨平台.
- Github三次学习
作者声明:本文参照aicoder.com马伦老师的文档,根据自己的学习情况而做的笔记,仅供交流学习. Git入门到高级教程 为什么要进行项目文件的版本管理 代码备份和恢复 团队开发和协作流程 项目分支 ...