Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 26351   Accepted: 14254

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).

Source

  给出一个N*N的地图,和数个障碍物的位置(i,j) 每次操作可以将某一行或者某一列的障碍物都清楚,问最少的操作次数。

  每一行/列缩一个点,对于每个(i,j),就把行i点和列j点连一条边,这样每一条边就对应着一个障碍物,操作转化为选取一个点之后,与这个点相连的边对应的障碍物都将被清除,问最少选出几个点能覆盖所有的边,又二分图中最小点覆盖=最大匹配数,所以就是求一个最大匹配。

  这个题网络流也能解,按理说复杂度比匈牙利低,建图思路一样,行向列建边容量为1,源点向行点建边,列点向汇点建边跑最大流。

  按理说树dp也可以求最小点覆盖但是一直WA不知为何= =

  匈牙利:

 #include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
using namespace std;
int match[],N;
bool vis[];
vector<int>g[];
int dfs(int u){
for(int i=;i<g[u].size();++i){
if(!vis[g[u][i]]){
vis[g[u][i]]=;
if(match[g[u][i]]==-||dfs(match[g[u][i]])){
match[g[u][i]]=u;
return ;
}
}
}
return ;
}
int main() {
int t,n,m,i,j;
scanf("%d%d",&n,&m);
for(i=;i<=n;++i)g[i].clear();
N=n;
while(m--){
scanf("%d%d",&i,&j);
g[i].push_back(j);
}
memset(match,-,sizeof(match));
int ans=;
for(i=;i<=n;++i){
memset(vis,,sizeof(vis));
ans+=dfs(i);
}
cout<<ans<<endl;
return ;
}

dinic:

  

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<vector>
#include<cstdlib>
using namespace std;
#define inf 0x3f3f3f3f
struct Edge{
int v,cap,flow,next;
}e[];
int first[],tot,N;
int d[],cur[];
bool vis[];
void add(int u,int v,int cap){
e[tot].v=v;
e[tot].cap=cap;
e[tot].flow=;
e[tot].next=first[u];
first[u]=tot++;
}
bool bfs(){
memset(vis,,sizeof(vis));
queue<int>q;
q.push();
d[]=;
vis[]=;
while(!q.empty()){
int u=q.front();
q.pop();
for(int i=first[u];~i;i=e[i].next){
if(!vis[e[i].v] && e[i].cap>e[i].flow){
vis[e[i].v]=;
d[e[i].v]=d[u]+;
q.push(e[i].v);
}
}
}
return vis[N*+];
}
int dfs(int x,int a){
if(x==N*+ || a==) return a;
int flow=,f;
for(int &i=cur[x];~i;i=e[i].next){
if(d[x]+==d[e[i].v] && (f=dfs(e[i].v,min(a,e[i].cap-e[i].flow)))>){
e[i].flow+=f;
e[i^].flow-=f;
flow+=f;
a-=f;
if(a==) break;
}
}
return flow;
}
int solve(){
int ans=;
while(bfs()){
for(int i=;i<=N*+;++i)cur[i]=first[i];
ans+=dfs(,inf);
}
return ans;
}
int main(){
int m,i,j;
while(cin>>N>>m){
memset(first,-,sizeof(first));
tot=;
for(i=;i<=N;++i){
add(,i,);
add(i,,);
add(i+N,N*+,);
add(N*+,i+N,);
}
while(m--){
scanf("%d%d",&i,&j);
add(i,j+N,);
add(j+N,i,);
}
cout<<solve()<<endl;
}
return ;
}

POJ-3041-建图/二分图匹配/网络流的更多相关文章

  1. POJ 3041 Asteroids (二分图匹配)

    [题目链接] http://poj.org/problem?id=3041 [题目大意] 一个棋盘上放着一些棋子 每次操作可以拿走一行上所有的棋子或者一列上所有的棋子 问几次操作可以拿完所有的棋子 [ ...

  2. POJ 2226 缩点建图+二分图最大匹配

    这个最小覆盖但不同于 POJ 3041,只有横或者竖方向连通的点能用一块板子覆盖,非连续的,就要用多块 所以用类似并查集方法,分别横向与竖向缩点,有交集的地方就连通,再走一遍最大匹配即可 一开始还有点 ...

  3. LOJ 2548 「JSOI2018」绝地反击 ——二分图匹配+网络流手动退流

    题目:https://loj.ac/problem/2548 如果知道正多边形的顶点,就是二分答案.二分图匹配.于是写了个暴力枚举多边形顶点的,还很愚蠢地把第一个顶点枚举到 2*pi ,其实只要 \( ...

  4. cogs_396_魔术球问题_(最小路径覆盖+二分图匹配,网络流24题#4)

    描述 http://cojs.tk/cogs/problem/problem.php?pid=396 连续从1开始编号的球,按照顺寻一个个放在n个柱子上,\(i\)放在\(j\)上面的必要条件是\(i ...

  5. POJ 3057 Evacuation(二分图匹配+BFS)

    [题目链接] http://poj.org/problem?id=3057 [题目大意] 给出一个迷宫,D表示门,.表示人,X表示不可通行, 每个门每时间单位只允许一个人通过, 每个人移动一格的为一时 ...

  6. POJ 3041 Asteroids (二分图最小点覆盖集)

    Asteroids Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 24789   Accepted: 13439 Descr ...

  7. POJ 2724 Purifying Machine (二分图匹配)

    题意 给定m个长度为n的01串(*既表示0 or 1.如*01表示001和101).现在要把这些串都删除掉,删除的方法是:①一次删除任意指定的一个:②如果有两个串仅有一个字符不同,则可以同时删除这两个 ...

  8. P3386 【模板】二分图匹配 -网络流版

    二分图匹配 题目背景 二分图 感谢@一扶苏一 提供的hack数据 题目描述 给定一个二分图,结点个数分别为n,m,边数为e,求二分图最大匹配数 输入输出格式 输入格式: 第一行,n,m,e 第二至e+ ...

  9. poj 2226 Muddy Fields(合理建图+二分匹配)

    /* 题意:用木板盖住泥泞的地方,不能盖住草.木板任意长!可以重叠覆盖! '*'表示泥泞的地方,'.'表示草! 思路: 首先让我们回忆一下HDU 2119 Matrix这一道题,一个矩阵中只有0, 1 ...

随机推荐

  1. HashMap 和 HashTable 的区别

    1. 存储结构 HashMap HashTable 数组 + 链表/红黑树 数组 + 链表 HashMap的存储规则: 优先使用数组存储, 如果出现Hash冲突, 将在数组的该位置拉伸出链表进行存储( ...

  2. skype for business 无法共享桌面、无法传输图片

    以管理员身份运行如下PowerShell命令,清除Skype for Business缓存记录 #以管理员身份运行如下PowerShell命令,清除Skype for Business缓存记录 Sto ...

  3. [转载]linux下core文件设置与查看

    转自:https://blog.csdn.net/dingqinghui/article/details/77855330?locationNum=9&fps=1 linux下core文件设置 ...

  4. 针对list集合进行分页展示

    直接定义个工具类,代码如下: package com.jk51.modules.wechat.web.util; import java.util.Collections; import java.u ...

  5. android 利用CountDownTimer实现时分秒倒计时效果

    https://blog.csdn.net/mrzhao_perfectcode/article/details/81289578

  6. centos7 下载安装tomcat9

    需要Java环境 https://www.cnblogs.com/sea-stream/p/10404360.html 官网下载安装包 wget http://archive.apache.org/d ...

  7. java之双缓冲的代码粘贴

    private Image offScreenImage = null; public void update(Graphics g) { if(offScreenImage == null) off ...

  8. java 常用异常及作用

    先看看图, Exception就明白了 关于异常 大体分为 不可查异常 可查异常 runtimeException三类~异常都继承throwable这个类~ 下面有error和Exception两大类 ...

  9. 《剑指offer》第四十八题(最长不含重复字符的子字符串)

    // 面试题48:最长不含重复字符的子字符串 // 题目:请从字符串中找出一个最长的不包含重复字符的子字符串,计算该最长子 // 字符串的长度.假设字符串中只包含从'a'到'z'的字符. #inclu ...

  10. Eclipse Jee环境配置

    最近下载了新的Eclipse Jee Neon版本,记录一下如何进行开发环境的配置. 1.下载必要的开发环境文件 ①下载Java SE Development Kit (简称JDK) ②下载Tomca ...