1741: [Usaco2005 nov]Asteroids 穿越小行星群
1741: [Usaco2005 nov]Asteroids 穿越小行星群
Time Limit: 5 Sec Memory Limit: 64 MB
Submit: 231 Solved: 166
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.
贝茜想驾驶她的飞船穿过危险的小行星群.小行星群是一个NxN的网格(1≤N≤500),在网格内有K个小行星(1≤K≤10000). 幸运地是贝茜有一个很强大的武器,一次可以消除所有在一行或一列中的小行星,这种武器很贵,所以她希望尽量地少用.给出所有的小行星的位置,算出贝茜最少需要多少次射击就能消除所有的小行星.
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.
第1行:两个整数N和K,用一个空格隔开.
第2行至K+1行:每一行有两个空格隔开的整数R,C(1≤R,C≤N),分别表示小行星所在的行和列.
Output
* Line 1: The integer representing the minimum number of times Bessie must shoot.
一个整数表示贝茜需要的最少射击次数,可以消除所有的小行星
Sample Input
3 4 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
2 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掉此题之后才想到不需要根据每一个点再在中间建立一个节点,直接二分图就可以搞定)
1 /**************************************************************
2 Problem: 1741
3 User: HansBug
4 Language: Pascal
5 Result: Accepted
6 Time:20 ms
7 Memory:1036 kb
8 ****************************************************************/
9
10 type
11 point=^node;
12 node=record
13 g,w:longint;
14 next,anti:point;
15 end;
16 var
17 i,j,k,l,m,n,s,t,ans:longint;
18 a:array[0..20000] of point;
19 d,dv:array[0..20000] of longint;
20 function min(x,y:longint):longint;
21 begin
22 if x<y then min:=x else min:=y;
23 end;
24 procedure add(x,y,z:longint);
25 var p:point;
26 begin
27 new(p);p^.g:=y;p^.w:=z;p^.next:=a[x];a[x]:=p;
28 new(p);p^.g:=x;p^.w:=0;p^.next:=a[y];a[y]:=p;
29 a[x]^.anti:=a[y];a[y]^.anti:=a[x];
30 end;
31 function dfs(x,flow:longint):longint;
32 var p:point;k:longint;
33 begin
34 if x=t then exit(flow);
35 p:=a[x];dfs:=0;
36 while p<>nil do
37 begin
38 if (p^.w<>0) and (d[x]=(d[p^.g]+1)) then
39 begin
40 k:=dfs(p^.g,min(flow-dfs,p^.w));
41 if p^.w<>maxlongint then dec(p^.w,k);
42 if p^.anti^.w<>maxlongint then inc(p^.anti^.w,k);
43 inc(dfs,k);
44 if dfs=flow then exit;
45 end;
46 p:=p^.next;
47 end;
48 if d[s]=n then exit;
49 dec(dv[d[x]]);
50 if dv[d[x]]=0 then d[s]:=n;
51 inc(d[x]);inc(dv[d[x]]);
52 end;
53 begin
54 readln(n,m);
55 for i:=0 to n*2+m+1 do a[i]:=nil;
56 for i:=1 to n do
57 begin
58 add(0,i,1);add(n+m+i,n*2+m+1,1);
59 end;
60 for i:=1 to m do
61 begin
62 readln(j,k);
63 add(j,n+i,1);add(n+i,n+m+k,1);
64 end;
65 s:=0;t:=n*2+m+1;
66 n:=n*2+m+2;
67 fillchar(d,sizeof(d),0);
68 fillchar(dv,sizeof(dv),0);
69 dv[0]:=n;ans:=0;
70 while d[s]<n do inc(ans,dfs(s,maxlongint));
71 writeln(ans);
72 readln;
73 end.
二分图:(话说二分图居然比优化的不到位的网络流满是什么鬼QAQ)
1 /**************************************************************
2 Problem: 1741
3 User: HansBug
4 Language: Pascal
5 Result: Accepted
6 Time:44 ms
7 Memory:1460 kb
8 ****************************************************************/
9
10 type
11 point=^node;
12 node=record
13 g:longint;
14 next:point;
15 end;
16 var
17 i,j,k,l,m,n,ans:longint;
18 a:array[0..100000] of point;
19 c,f:array[0..100000] of longint;
20 procedure add(x,y:longint);
21 var p:point;
22 begin
23 new(p);p^.g:=y;
24 p^.next:=a[x];a[x]:=p;
25 end;
26 function check(x:longint):boolean;
27 var p:point;
28 begin
29 p:=a[x];
30 while p<>nil do
31 begin
32 if f[p^.g]<>i then
33 begin
34 f[p^.g]:=i;
35 if c[p^.g]=0 then
36 begin
37 c[p^.g]:=x;
38 exit(true);
39 end
40 else if check(c[p^.g]) then
41 begin
42 c[p^.g]:=x;
43 exit(true);
44 end;
45 end;
46 p:=p^.next;
47 end;
48 exit(false);
49 end;
50 begin
51 readln(n,m);
52 for i:=1 to n do a[i]:=nil;
53 for i:=1 to m do
54 begin
55 readln(j,k);
56 add(j,k);
57 end;
58 ans:=0;
59 fillchar(c,sizeof(c),0);
60 fillchar(f,sizeof(f),0);
61 for i:=1 to n do if check(i) then inc(ans);
62 writeln(ans);;
63 readln;
64 end.
- Java并发学习之CountDownLatch实现原理及使用姿势
- Linux基础(day58)
- 携程Android App插件化和动态加载实践
- 15.5 使用pure-ftpd搭建ftp服务
- JDK容器学习之Queue: ArrayBlockingQueue
- 手机APP安装包缩减方案
- react-native添加redux支持
- Java并发学习之ReentrantLock的工作原理及使用姿势
- Linux基础(day63)
- IOS WebView控件详解
- Java并发学习之synchronized使用小结
- 18.3/18.4/18.5 用keepalived配置高可用集群
- JDK容器学习之Queue:DelayQueue
- React-Native组件之 Navigator和NavigatorIOS
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 【go】剑指offer:用26个英文字母组合表示列数
- Flink——运行在数据流上的有状态计算框架和处理引擎
- goland破解教程
- HTTP接口设计
- NSQ分布式消息基础入门
- 【golang】剑指offer 最大n位数
- Spark提交Jar任务警告: Initial job has not accepted any resources;
- Defer,Panic,and Recover
- Spark——底层操作RDD,基于内存处理数据的计算引擎
- Go 项目最佳实践
- 虚拟机更换JDK版本步骤(Hadoop集群)
- JavaSE重点复习
- [数据结构与算法] 树结构之二叉排序树、平衡二叉树、多路查找树
- 工作后, 你一定不能错过技术之JDK1.8的新特性
- 【Go】剑指offer:二叉树子树的判断