poj 3041 Asteroids(二分图 *【矩阵实现】【最小点覆盖==最大匹配数】)
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 16379 | Accepted: 8930 |
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).
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#include <iostream>
#include <string>
#include <stack>
#include <queue>
#include <algorithm>
#define N 500+20
#define INF 0x3f3f3f3f using namespace std;
int n, m;
int map[N][N];
int link[N];
bool vis[N]; bool match( int u )
{
for(int i=1; i<=n; i++)
{
if(vis[i]==false && map[u][i]==1 )
{
vis[i]=true;
if(link[i]==-1 || match(link[i]) )
{
link[i]=u;
return true;
}
}
}
return false;
} int main()
{
int i, j, k;
int u, v;
scanf("%d %d", &n, &m);
memset(map, 0, sizeof(map)); for(i=0; i<m; i++ ){
scanf("%d %d", &u, &v);
map[u][v] = 1;
}
memset(link, -1, sizeof(link));
int ans=0; for(int i=1; i<=n; i++)
{
memset(vis, false, sizeof(vis));
if(match(i))
ans++; }
printf("%d\n", ans );
return 0;
}
poj 3041 Asteroids(二分图 *【矩阵实现】【最小点覆盖==最大匹配数】)的更多相关文章
- POJ 3041 Asteroids (二分图最小点覆盖)
题目链接:http://poj.org/problem?id=3041 在一个n*n的地图中,有m和障碍物,你每一次可以消除一行或者一列的障碍物,问你最少消除几次可以将障碍物全部清除. 用二分图将行( ...
- poj 3041 Asteroids (二分图的最大匹配 第一题)
题目:http://poj.org/problem?id=3041 题意:在某个n*n的空间内,分布有一些小行星,某人在里面打炮,放一枪后某一行或某一列的行星就都没了,让求最少的打炮数. 然后把每行x ...
- POJ 3041 Asteroids 二分图匹配
以行列为点建图,每个点(x,y) 对应一条边连接x,y.二分图的最小点覆盖=最大匹配 //#pragma comment(linker, "/STACK:1024000000,1024000 ...
- POJ 3041 Asteroids 二分图
原题连接:http://poj.org/problem?id=3041 Asteroids Time Limit: 1000MS Memory Limit: 65536K Total Submis ...
- poj - 3041 Asteroids (二分图最大匹配+匈牙利算法)
http://poj.org/problem?id=3041 在n*n的网格中有K颗小行星,小行星i的位置是(Ri,Ci),现在有一个强有力的武器能够用一发光速将一整行或一整列的小行星轰为灰烬,想要利 ...
- POJ 3041 Asteroids 二分图之最大匹配
题意:在一个网格中有若干个点,每一次可以清除一行或者一列,问最少几次可以将网格中的点全部清除. 思路:这个题是一个入门的最大匹配题(这个好像不是思路..).一般的方式就是将 行 看作集合A,列 看作集 ...
- 【网络流#6】POJ 3041 Asteroids 二分图最大匹配 - 《挑战程序设计竞赛》例题
学习网络流中ing...作为初学者练习是不可少的~~~构图方法因为书上很详细了,所以就简单说一说 把光束作为图的顶点,小行星当做连接顶点的边,建图,由于 最小顶点覆盖 等于 二分图最大匹配 ,因此求二 ...
- POJ 3041 Asteroids(二分图模板题)
Bessie wants to navigate her spaceship through a dangerous asteroid field in the shape of an N x N g ...
- POJ 3041 Asteroids / UESTC 253 Asteroids(二分图最大匹配,最小点匹配)
POJ 3041 Asteroids / UESTC 253 Asteroids(二分图最大匹配,最小点匹配) Description Bessie wants to navigate her spa ...
随机推荐
- Linux系统救援模式应用:恢复误删的系统文件
利用Linux系统救援模式找回误删的系统文件 背景:在操作中误删了某些重要的系统文件如/lib64/libc.so.6这个文件,可以利用Linux系统的救援模式来找回 步骤: 将系统光盘或U盘在Bio ...
- C 标准库 - <assert.h>
C 标准库 - <assert.h> 简介 C 标准库的 assert.h头文件提供了一个名为 assert 的宏,它可用于验证程序做出的假设,并在假设为假时输出诊断消息. 已定义的宏 a ...
- jquery插件jTemplates使用方法
简单记一下我所做项目中用到的代码,以备以后用的时候一看明确了. 1.jsp(jquery-jtemplates.js下载地址:http://download.csdn.net/detail/xlb74 ...
- 用PHP调用证件识别接口识别本地图片
前置条件 在开始前,请作如下准备:1.学会用PHP输出“Hello World” 2.去聚合数据申请证件识别专用的KEY:https://www.juhe.cn/docs/api/id/153 操作步 ...
- java查看工具jstack-windows
Prints Java thread stack traces for a Java process, core file, or remote debug server. This command ...
- GitFlow工作流常用操作流程
1. 主要分支介绍 1.1 master分支 主分支,产品的功能全部实现后,最终在master分支对外发布. 1.2 develop分支 开发分支,基于master分支克隆,产品的编码工作在此分支进行 ...
- Android利用reative_layout生成梅花界面
XML代码: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:and ...
- URL Handle in Swift (一) -- URL 分解
更新时间: 2018-6-6 在程序开发过程之中, 我们总是希望模块化处理某一类相似的事情. 在 ezbuy 开发中, 我接触到了对于 URL 处理的优秀的代码, 学习.改进.记录下来.希望对你有所帮 ...
- 利用xlrd模块实现Python读取Excel文档
# -*- coding: cp936 -*- #python读取excel import xlrd def main(): xls=xlrd.open_workbook("d:\\11.x ...
- oracle不同用户间访问表不添加用户名(模式)前缀
默认的情况下,oracle里面的用户A,要访问用户B的表需要带用户B的前缀,如访问用户B的 user表,需要这样访问 select * from B.user;如果想要不添加用户前缀,需要这样处理: ...