1741: [Usaco2005 nov]Asteroids 穿越小行星群
1741: [Usaco2005 nov]Asteroids 穿越小行星群
Time Limit: 5 Sec Memory Limit: 64 MB
Submit: 231 Solved: 166
[Submit][Status][Discuss]
Description
Bessie wants to navigate her spaceship through a dangerous asteroid field in the shape of an N x N grid (1 <= N <= 500). The grid contains K asteroids (1 <= K <= 10,000), which are conveniently located at the lattice points of the grid. 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
* Line 1: Two integers N and K, separated by a single space.
* 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
* Line 1: The integer representing the minimum number of times Bessie must shoot.
一个整数表示贝茜需要的最少射击次数,可以消除所有的小行星
Sample Input
1 1
1 3
2 2
3 2
INPUT DETAILS:
The following diagram represents the data, where "X" is an
asteroid and "." is empty space:
X.X
.X.
.X.
Sample Output
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).
HINT
Source
题解:看了半天,感觉还是网络流= =,而且貌似还是二分图
分别以横坐标与纵坐标建立点集,然后根据各点来连边建立二分图,然后二分图匹配或者网络流秒之
网络流:(这个程序其实还可以优化——因为我在A掉此题之后才想到不需要根据每一个点再在中间建立一个节点,直接二分图就可以搞定)
/**************************************************************
Problem:
User: HansBug
Language: Pascal
Result: Accepted
Time: ms
Memory: kb
****************************************************************/ type
point=^node;
node=record
g,w:longint;
next,anti:point;
end;
var
i,j,k,l,m,n,s,t,ans:longint;
a:array[..] of point;
d,dv:array[..] of longint;
function min(x,y:longint):longint;
begin
if x<y then min:=x else min:=y;
end;
procedure add(x,y,z:longint);
var p:point;
begin
new(p);p^.g:=y;p^.w:=z;p^.next:=a[x];a[x]:=p;
new(p);p^.g:=x;p^.w:=;p^.next:=a[y];a[y]:=p;
a[x]^.anti:=a[y];a[y]^.anti:=a[x];
end;
function dfs(x,flow:longint):longint;
var p:point;k:longint;
begin
if x=t then exit(flow);
p:=a[x];dfs:=;
while p<>nil do
begin
if (p^.w<>) and (d[x]=(d[p^.g]+)) then
begin
k:=dfs(p^.g,min(flow-dfs,p^.w));
if p^.w<>maxlongint then dec(p^.w,k);
if p^.anti^.w<>maxlongint then inc(p^.anti^.w,k);
inc(dfs,k);
if dfs=flow then exit;
end;
p:=p^.next;
end;
if d[s]=n then exit;
dec(dv[d[x]]);
if dv[d[x]]= then d[s]:=n;
inc(d[x]);inc(dv[d[x]]);
end;
begin
readln(n,m);
for i:= to n*+m+ do a[i]:=nil;
for i:= to n do
begin
add(,i,);add(n+m+i,n*+m+,);
end;
for i:= to m do
begin
readln(j,k);
add(j,n+i,);add(n+i,n+m+k,);
end;
s:=;t:=n*+m+;
n:=n*+m+;
fillchar(d,sizeof(d),);
fillchar(dv,sizeof(dv),);
dv[]:=n;ans:=;
while d[s]<n do inc(ans,dfs(s,maxlongint));
writeln(ans);
readln;
end.
二分图:(话说二分图居然比优化的不到位的网络流满是什么鬼QAQ)
/**************************************************************
Problem:
User: HansBug
Language: Pascal
Result: Accepted
Time: ms
Memory: kb
****************************************************************/ type
point=^node;
node=record
g:longint;
next:point;
end;
var
i,j,k,l,m,n,ans:longint;
a:array[..] of point;
c,f:array[..] of longint;
procedure add(x,y:longint);
var p:point;
begin
new(p);p^.g:=y;
p^.next:=a[x];a[x]:=p;
end;
function check(x:longint):boolean;
var p:point;
begin
p:=a[x];
while p<>nil do
begin
if f[p^.g]<>i then
begin
f[p^.g]:=i;
if c[p^.g]= then
begin
c[p^.g]:=x;
exit(true);
end
else if check(c[p^.g]) then
begin
c[p^.g]:=x;
exit(true);
end;
end;
p:=p^.next;
end;
exit(false);
end;
begin
readln(n,m);
for i:= to n do a[i]:=nil;
for i:= to m do
begin
readln(j,k);
add(j,k);
end;
ans:=;
fillchar(c,sizeof(c),);
fillchar(f,sizeof(f),);
for i:= to n do if check(i) then inc(ans);
writeln(ans);;
readln;
end.
1741: [Usaco2005 nov]Asteroids 穿越小行星群的更多相关文章
- BZOJ 1741: [Usaco2005 nov]Asteroids 穿越小行星群
Description 贝茜想驾驶她的飞船穿过危险的小行星群.小行星群是一个NxN的网格(1≤N≤500),在网格内有K个小行星(1≤K≤10000). 幸运地是贝茜有一个很强大的武器,一次可以消除所 ...
- 【BZOJ】1741: [Usaco2005 nov]Asteroids 穿越小行星群
[题意]给定n*n网格,有k个物品,每次可以消灭一行或一列,求消灭掉所有物品的最少操作次数. [算法]二分图最小覆盖 [题解]此题是最小覆盖模型的出处. 将物品的x-y连边建立二分图. 最小覆盖:选择 ...
- bzoj1741 [Usaco2005 nov]Asteroids 穿越小行星群
网络流,对于每一个行星,将行星所在行到行星连一条流量为1的边,将行星到其所在列连一条流量为1的边,从源点到所有行连一条流量为1的边,将所有列到汇点都连一条流量为1的边,最大流即为答案. 代码 #inc ...
- bzoj 1741: [Usaco2005 nov]Asteroids 穿越小行星群【最大点覆盖】
二分图最大点覆盖模型,因为对于一个点(x,y)显然只要选x或者y就好了,于是连边,跑最大匹配=最大点覆盖(不会证) #include<iostream> #include<cstdi ...
- 【JZOJ1922】【Usaco 2005 NOV Gold】小行星群
题目描述 Bessie想驾驶她的飞船穿过危险的小行星群,小行星群是一个N×N的网格(1 <= N <= 500),在网格内有K个小行星(1 <= K <= 10,000). 幸 ...
- bzoj1741 [Usaco2005 nov]Asteroids 穿越小行星群 最小点覆盖
链接 https://www.lydsy.com/JudgeOnline/problem.php?id=1741 思路 消除所有的小行星 每个点(x,y)只有选择x或者y才能被覆盖 二分图最小点覆盖= ...
- [bzoj1741]穿越小行星群
将每一行/每一列作为一个点,对于一个障碍(x,y),要么第x行和第y列的状态(是否攻击)只需要有一个就可以了,将第x行和第y列连边,就是二分图的最小点覆盖=最大匹配数. 1 #include<b ...
- [Usaco2005 Nov]Asteroids
Description Bessie wants to navigate her spaceship through a dangerous asteroid field in the shape o ...
- BZOJ2023: [Usaco2005 Nov]Ant Counting 数蚂蚁
2023: [Usaco2005 Nov]Ant Counting 数蚂蚁 Time Limit: 4 Sec Memory Limit: 64 MBSubmit: 56 Solved: 16[S ...
随机推荐
- Redhat6.4下配置本地yum
一.准备工作1. Linux安装盘插入光驱 2. 挂载光驱 [root@localhost ~]# mount /dev/cdrom /mnt/ mount: block device /dev/s ...
- Ubuntu14.04安装Go语言开发环境
1.使用apt-get命令来安装Go环境 apt-get install software-properties-common apt-get install python-software-prop ...
- iOS页面间传值的五种方式总结(Delegate/NSNotification/Block/NSUserDefault/单例)
iOS页面间传值的方式(Delegate/NSNotification/Block/NSUserDefault/单例) iOS页面间传值的方式(NSUserDefault/Delegate/NSNot ...
- linux驱动的多种init函数及其调用顺序
在驱动设计时可以选用多种驱动初始化函数达到控制驱动初始化顺序控制,其中level(__define_initcall的第一个参数即优先级)越小优先级越高, #define pure_initcall( ...
- 移动HTML5前端性能优化总结
概述 1. PC优化手段在Mobile侧同样适用 2. 在Mobile侧我们提出三秒种渲染完成首屏指标 3. 基于第二点,首屏加载3秒完成或使用Loading 4. 基于联通3G网络平均338KB/s ...
- 模块化写法-IIFE解析
一.IIFE解释 全拼Imdiately Invoked Function Expression,立即执行的函数表达式. 像如下的代码所示,就是一个匿名立即执行函数: (function(windo ...
- 【Java基础】线程和并发机制
前言 在Java中,线程是一个很关键的名词,也是很高频使用的一种资源.那么它的概念是什么呢,是如何定义的,用法又有哪些呢?为何说Android里只有一个主线程呢,什么是工作线程呢.线程又存在并发,并发 ...
- 如何解决在chrome中自动完成表单后input出现黄色背景
可以对input:-webkit-autofill使用足够大的纯色内阴影来覆盖input输入框的黄色背景:如: 代码如下: input:-webkit-autofill { -webkit-box-s ...
- BZOJ 4085:[Sdoi2015]bigyration(SDOI 2015 round 2 Day 1)
别人家的神选系列.Day2根本不能做QAQ 题目描述:给定两个字符串集合,一个长度为n,另一个为m,求有多少个数字对i,j,满足xi+yj能由一个(n+m)/2的字符串旋转拼接而成 我们枚举长度较长的 ...
- 纪中集训 Day 3
这几天一直坚持写blog= =加油吧!! 早上醒来,说了"我要AK"(其实只是蒟蒻的妄想罢了QAQ) 然后为了不立flag,改成了我要rank 1 然后依旧是有一题不会做QAQ 好 ...