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. VHDL 乐曲演奏电路设计

    前言 无源蜂鸣器在直流信号下不会响,需要方波驱动.输入不同频率的方波会发出不同音调的声音,方波的幅值决定了声音的响度. 目标 乐曲发生电路在节拍(4Hz)的控制下根据乐谱产生合适的分频系数.分频器根据 ...

  2. ngui处理不规则按钮点击

    吐个槽  棋牌类游戏做什么中国地图!!!  然后就要用到不规则按钮点击了 你懂的 213的unity虽然已经加入了polygoncollider 2d的支持 但是 但是 但是 是2d的 也就是说如果不 ...

  3. Codeforces 781D Axel and Marston in Bitland

    题目链接:http://codeforces.com/contest/781/problem/D ${F[i][j][k][0,1]}$表示是否存在从${i-->j}$的路径走了${2^{k}} ...

  4. Python day1_Base1笔记

    1.helloworld print('helloword') 2.输入输出 a=input('Please input a value') print(a) 3.标识符 1.由字母数字下划线构成 2 ...

  5. Missing artifact com.oracle:ojdbc6:jar:10.2.0.4.0问题解决 ojdbc包pom.xml出错

    遇到的问题:ojdbc.jar包出错 原因:因为oracle的ojdbc.jar是收费的,所以maven的中央仓库中没有这个资源,只能通过配置本地库才能加载到项目中去. 解决办法: (前提是安装好了m ...

  6. maven项目, 单元测试失败提示 Class not found datastorage........

    ---恢复内容开始--- 单元测试失败:  提示 Class not found datastorage........ 原因:   maven  环境变量问题,   eclipse 没有自动更新下载 ...

  7. JDBC的通用查询的方法

    PreparedStatement 1.Why 1):使用Statement需要进行拼写SQL语句,很辛苦,而且容易出错. 2):使用Statement可以发生SQL注入. SQl注入: SQL注入是 ...

  8. JS _proto_ 和 prototype

    转载自:https://www.cnblogs.com/wuwenjie/p/5433776.html 大佬讲得很明白,自己也收藏一下! 初学javascript的时候也跟题主一样搞不清楚,自己好好总 ...

  9. spring cloud: Hystrix(二):简单使用@HystrixCommand的commandProperties配置@HistrixProperty隔离策略

    spring cloud: Hystrix(二):简单使用@HystrixCommand的commandProperties配置@HistrixProperty隔离策略 某电子商务网站在一个黑色星期五 ...

  10. Shell脚本字体颜色

    [root@web01 scripts]# man console_codesecho -e "\033[背景颜色:字体颜色m字符串\033[0m",例:echo -e " ...