Asteroids
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 23963   Accepted: 12989

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

3 4
1 1
1 3
2 2
3 2

Sample Output

2

Hint

INPUT DETAILS: 
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).

 
 
题意就是消灭星星,一个N*N的网格,网格上有星星,打一次枪可以消灭一行或者一列。
问最少打多少次枪,消灭所有的星星。
 
 思路很神奇,可以用匈牙利算法写 。
把行当成一个集合,列当成一个集合。
求最小覆盖点,也就是求最大匹配。
思路很好,当然也可以用其他的写,在学二分图,就用匈牙利啦。
 
代码(直接改的hdu的2063,嘎嘎嘎):
 #include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <string>
#include <queue>
#include <ctime>
#include <vector>
using namespace std;
const int maxn= ;
const int maxm=+;
const int inf = 0x3f3f3f3f;
typedef long long ll;
int n,m;
int match[maxm];
bool visited[maxm];
bool mp[maxm][maxm];
bool Find(int x){
for(int i=;i<=n;i++){
if(mp[x][i]&&visited[i]==){
visited[i]=true;
if(match[i]==||Find(match[i])){
match[i]=x;
return true;
}
}
}
return false;
}
int main(){
int x,y,num;
while(~scanf("%d%d",&n,&m)){
memset(mp,,sizeof(mp));
memset(match,,sizeof(match));
memset(visited,,sizeof(visited));
for(int i=;i<m;i++){
scanf("%d%d",&x,&y);
mp[x][y]=true;
}
num=;
for(int i=;i<=n;i++){
memset(visited,,sizeof(visited));
if(Find(i))num++;
}
printf("%d\n",num);
}
return ;
}

溜啦溜啦,去写多校的二分图的题啦。

 
 

POJ 3041.Asteroids-Hungary(匈牙利算法)的更多相关文章

  1. POJ 3041 Asteroids(匈牙利+邻接表)

    ( ̄▽ ̄)" #include<iostream> #include<cstdio> #include<cstring> #include<algo ...

  2. 二分图最大匹配(匈牙利算法) POJ 3041 Asteroids

    题目传送门 /* 题意:每次能消灭一行或一列的障碍物,要求最少的次数. 匈牙利算法:把行和列看做两个集合,当有障碍物连接时连一条边,问题转换为最小点覆盖数==二分图最大匹配数 趣味入门:http:// ...

  3. POJ 3041 Asteroids (对偶性,二分图匹配)

    题目:POJ 3041 Asteroids http://poj.org/problem?id=3041 分析: 把位置下标看出一条边,这显然是一个二分图最小顶点覆盖的问题,Hungary就好. 挑战 ...

  4. POJ 3041 Asteroids / UESTC 253 Asteroids(二分图最大匹配,最小点匹配)

    POJ 3041 Asteroids / UESTC 253 Asteroids(二分图最大匹配,最小点匹配) Description Bessie wants to navigate her spa ...

  5. poj 3041——Asteroids

    poj       3041——Asteroids Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 22604   Accep ...

  6. poj - 3041 Asteroids (二分图最大匹配+匈牙利算法)

    http://poj.org/problem?id=3041 在n*n的网格中有K颗小行星,小行星i的位置是(Ri,Ci),现在有一个强有力的武器能够用一发光速将一整行或一整列的小行星轰为灰烬,想要利 ...

  7. POJ 3041 Asteroids(二分图 && 匈牙利算法 && 最小点覆盖)

    嗯... 题目链接:http://poj.org/problem?id=3041 这道题的思想比较奇特: 把x坐标.y坐标分别看成是二分图两边的点,如果(x,y)上有行星,则将(x,y)之间连一条边, ...

  8. poj 3041 Asteroids (最大匹配最小顶点覆盖——匈牙利模板题)

    http://poj.org/problem?id=3041 Asteroids Time Limit: 1000MS   Memory Limit: 65536K Total Submissions ...

  9. POJ 3041 Asteroids(模板——二分最大匹配(BFS增广))

    题目链接: http://poj.org/problem?id=3041 Description Bessie wants to navigate her spaceship through a da ...

  10. POJ:3041-Asteroids(匈牙利算法模板)

    传送门:http://poj.org/problem?id=3041 Asteroids Time Limit: 1000MS Memory Limit: 65536K Description Bes ...

随机推荐

  1. 通过广播关闭应用程序(每个Activity)和连续点击两次返回键关闭应用程序

    对于一个应用程序可能有很多个Activity,可能每个人并不想一个个的去关闭Activity,也有可能忘了,那怎么关闭所有的未关闭的Activity呢,其实有很多方法,但是我最喜欢的一种就是通过广播事 ...

  2. Jforum环境之Tomcat环境搭建

    Jforum环境搭建,需先安装JDK.JRE.Tomcat.Mysql(JDK.JRE暂不做说明).本文先说Tomcat环境搭建 1.进入Apache Tomcat官网下载,我选择的是免安装的zip包 ...

  3. IDEA调试快捷键

    F9            resume programe 恢复程序 F8            Step Over 相当于eclipse的f6      跳到下一步 Ctrl+Shift+F,全局查 ...

  4. DataSource的设置

    1.Centos和redhat,Fedora等版本无须设置,直接在cloud.cfg指定,默认是EC2 datasource_list: ['ConfigDrive','OpenStack'] 2.u ...

  5. hexo 配置文件 实例

    # Hexo Configuration ## Docs: https://hexo.io/docs/configuration.html ## Source: https://github.com/ ...

  6. web自动化测试:watir+minitest(四)

    脚本连跑: rake是ruby中的一个构建工具,和make很像.允许用ruby来写rakefile. 我们使用rake以任务的方式来运行我们的脚本集. 新建Rakefile文件,写入如下内容: req ...

  7. [NC189A]数字权重

    题目大意:有一个$n$位的数,设第$i$位为$a_i$(最高位为$a_1$).问满足$(\sum\limits_{i=2}^n(a_i-a_{i-1}))==k$的数的个数(不含前导零) 题解:发现$ ...

  8. POJ 2195 Going Home | 带权二分图匹配

    给个地图有人和房子 保证人==房子,每个人移动到房子处需要花费曼哈顿距离的代价 问让人都住在房子里最小代价 显然是个带权二分图最大匹配 转化成以一个网络,规定w是容量,c是代价 1.S向人连边,w=1 ...

  9. Codeforces 498D Traffic Jams in the Land | 线段树

    题目大意: 给坐标轴1~n的点,每个点有一个权值,从一个点走到下一个点需要1s,如果当前时间是权值的倍数就要多花1s 给出q组操作,C表示单点修改权值,A表示询问0时刻x出发到y的时间 题解:因为权值 ...

  10. 【CF Edu 28 C. Four Segments】

    time limit per test 1 second memory limit per test 256 megabytes input standard input output standar ...