n<=500 *n的格子,给m<=10000个格子有人,一炮可以清掉一行或一列的人(莫名的爽!)求最少几炮干掉所有人。

经典二分图模型!行成点,列成点,一个点就连接一行一列,表示这一行或这一列必选其一!

不喜欢匈牙利!跑网络流!

 #include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
#include<iostream>
using namespace std; int n,m;
#define maxn 1011
#define maxm 22011
struct Edge{int to,next,cap,flow;};
const int inf=0x3f3f3f3f;
struct Network
{
Edge edge[maxm];int first[maxn],le,n,s,t;
void clear(int n)
{
memset(first,,sizeof(first));
le=;this->n=n;
}
void in(int x,int y,int cap)
{
Edge &e=edge[le];
e.to=y;e.cap=cap;e.flow=;
e.next=first[x];first[x]=le++;
}
void insert(int x,int y,int cap) {in(x,y,cap);in(y,x,);}
int dis[maxn],cur[maxn],que[maxn],head,tail;
bool bfs()
{
memset(dis,,sizeof(dis));
dis[s]=;
que[head=(tail=)-]=s;
while (head!=tail)
{
const int now=que[head++];
for (int i=first[now];i;i=edge[i].next)
{
Edge &e=edge[i];
if (e.cap>e.flow && !dis[e.to])
{
dis[e.to]=dis[now]+;
que[tail++]=e.to;
}
}
}
return dis[t];
}
int dfs(int x,int a)
{
if (x==t || !a) return a;
int flow=,f;
for (int &i=cur[x];i;i=edge[i].next)
{
Edge &e=edge[i];
if (dis[e.to]==dis[x]+ && (f=dfs(e.to,min(a,e.cap-e.flow)))>)
{
e.flow+=f;
edge[i^].flow-=f;
a-=f;
flow+=f;
if (!a) break;
}
}
return flow;
}
int Dinic(int s,int t)
{
this->s=s,this->t=t;
int ans=;
while (bfs())
{
for (int i=;i<=n;i++) cur[i]=first[i];
ans+=dfs(s,inf);
}
return ans;
}
}g;
int x,y;
int main()
{
scanf("%d%d",&n,&m);
g.clear(n*+);
int s=g.n-,t=g.n;
for (int i=;i<=n;i++) g.insert(s,i,);
for (int i=n+;i<=n*;i++) g.insert(i,t,);
for (int i=;i<=m;i++)
{
scanf("%d%d",&x,&y);
g.insert(x,y+n,);
}
printf("%d\n",g.Dinic(s,t));
return ;
}

BZOJ1693: [Usaco2007 Demo]Asteroids的更多相关文章

  1. 【BZOJ】1693: [Usaco2007 Demo]Asteroids(匈牙利)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1693 裸匈牙利.. #include <cstdio> #include <cst ...

  2. BZOJ1629: [Usaco2007 Demo]Cow Acrobats

    1629: [Usaco2007 Demo]Cow Acrobats Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 601  Solved: 305[Su ...

  3. BZOJ1628: [Usaco2007 Demo]City skyline

    1628: [Usaco2007 Demo]City skyline Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 256  Solved: 210[Su ...

  4. 【BZOJ1630/2023】[Usaco2007 Demo]Ant Counting DP

    [BZOJ1630/2023][Usaco2007 Demo]Ant Counting 题意:T中蚂蚁,一共A只,同种蚂蚁认为是相同的,有一群蚂蚁要出行,个数不少于S,不大于B,求总方案数 题解:DP ...

  5. bzoj2023[Usaco2005 Nov]Ant Counting 数蚂蚁*&&bzoj1630[Usaco2007 Demo]Ant Counting*

    bzoj2023[Usaco2005 Nov]Ant Counting 数蚂蚁&&bzoj1630[Usaco2007 Demo]Ant Counting 题意: t个族群,每个族群有 ...

  6. BZOJ1695 : [Usaco2007 Demo]Walk the Talk

    观察单词表可以发现: 对于长度为3的单词,前两个字母相同的单词不超过7个 对于长度为4的单词,前两个字母相同的单词不超过35个 于是首先$O(26*26*nm)$预处理出 s1[x][i][j]表示( ...

  7. BZOJ 1695 [Usaco2007 Demo]Walk the Talk 链表+数学

    题意:链接 方法:乱搞 解析: 出这道题的人存心报复社会. 首先这个单词表-先上网上找这个单词表- 反正总共2265个单词.然后就考虑怎么做即可了. 刚開始我没看表,找不到怎么做,最快的方法我也仅仅是 ...

  8. BZOJ 1629: [Usaco2007 Demo]Cow Acrobats

    Description Farmer John's N (1 <= N <= 50,000) cows (numbered 1..N) are planning to run away a ...

  9. bzoj1630 [Usaco2007 Demo]Ant Counting

    Description Bessie was poking around the ant hill one day watching the ants march to and fro while g ...

随机推荐

  1. 学JAVA第二十四天,Set集合与StringBuilder

    下面的内容需要慢慢看,因为,我的语言表达能力不是很好 首先说Set把,Set集合是一个无序且不允许重复的集合,而且查找效率也是快的可怕的. 但是,有些时候,我们必须要用储存多个相同的值时,Set也是可 ...

  2. a=a+b与a+=b的区别

    在一次工作中身边的一位资深的同事突然问了个a=a+b与a+=b有什么区别 此时有点尴尬了 不知道是真的不知道咧还是别有用意....今天抽点时间针对此问题做个小总结 一.性能方面 a=a+b是加法运算 ...

  3. CF385C Bear and Prime Numbers

    思路: 需要对埃氏筛法的时间复杂度有正确的认识(O(nlog(log(n)))),我都以为肯定超时了,结果能过. 实现: #include <bits/stdc++.h> using na ...

  4. 【学习笔记】深入理解js原型和闭包(7)——原型的灵活性

    在Java和C#中,你可以简单的理解class是一个模子,对象就是被这个模子压出来的一批一批月饼(中秋节刚过完).压个啥样,就得是个啥样,不能随便动,动一动就坏了. 而在javascript中,就没有 ...

  5. 探究SQL添加非聚集索引,性能提高几十倍之谜

    上周,技术支持反映:客户的一个查询操作需要耗时6.1min左右,在跟进代码后,简化了数据库的查询后仍然收效甚微.后来,技术总监分析了sql后,给其中的一个表添加的一个非聚集索引(三个字段)后,同样的查 ...

  6. Android开发实现高德地图定位

    1.获取Key 参考官方文档:http://lbs.amap.com/api/android-location-sdk/guide/create-project/get-key 对于签名文件的获取建议 ...

  7. 洛谷 P3038 [USACO11DEC]牧草种植Grass Planting

    题目描述 Farmer John has N barren pastures (2 <= N <= 100,000) connected by N-1 bidirectional road ...

  8. 洛谷 P2788 数学1(math1)- 加减算式

    题目背景 蒟蒻HansBug在数学考场上,挠了无数次的头,可脑子里还是一片空白. 题目描述 好不容易啊,HansBug终于熬到了做到数学最后一题的时刻了,眼前是一堆杂乱的加减算式.显然成功就在眼前了. ...

  9. javaee 第六周作业

    一.jsf(java server faces)的运行原理(工作方式) 1.jsf应用是事件驱动的,当一个事件发生时(比如用户单击一个按钮),事件通知通过HTTP发往服务器,服务器端使用叫做Faces ...

  10. 2015Java参赛邀请函

    [导读]甲骨文公司值Java语言发布20周年之际,携手全国高等级学校计算机教育研究会.教育部高等学校计算机类专业教学指导委员会,共同举办了2015年“甲骨文杯”全国Java程序设计大赛,为二百万名中国 ...