[题目链接]

https://www.lydsy.com/JudgeOnline/problem.php?id=1585

[算法]

一个最小割的经典模型 , 详见代码

时间复杂度 : O(dinic(2N , 2C))

[代码]

#include<bits/stdc++.h>
using namespace std;
#define MAXN 8010
#define MAXC 50010
const int inf = 2e9; struct edge
{
int to , w , nxt;
} e[MAXC << ]; int tot , n , c , p , S , T;
int a[MAXN] , depth[MAXN], head[MAXN]; template <typename T> inline void chkmax(T &x,T y) { x = max(x,y); }
template <typename T> inline void chkmin(T &x,T y) { x = min(x,y); }
template <typename T> inline void read(T &x)
{
T f = ; x = ;
char c = getchar();
for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
for (; isdigit(c); c = getchar()) x = (x << ) + (x << ) + c - '';
x *= f;
}
inline void addedge(int u , int v , int w)
{
++tot;
e[tot] = (edge){v , w , head[u]};
head[u] = tot;
++tot;
e[tot] = (edge){u , , head[v]};
head[v] = tot;
}
inline bool bfs()
{
int l , r;
static int q[MAXN];
q[l = r = ] = S;
memset(depth , ,sizeof(depth));
depth[S] = ;
while (l <= r)
{
int cur = q[l++];
for (int i = head[cur]; i; i = e[i].nxt)
{
int v = e[i].to , w = e[i].w;
if (w > && !depth[v])
{
depth[v] = depth[cur] + ;
q[++r] = v;
if (v == T) return true;
}
}
}
return false;
}
inline int dinic(int u , int flow)
{
int rest = flow , ret = ;
if (u == T) return flow;
for (int i = head[u]; i && rest; i = e[i].nxt)
{
int v = e[i].to , w = e[i].w;
if (depth[v] == depth[u] + && w)
{
int k = dinic(v , min(rest , w));
if (!k) depth[v] = ;
e[i].w -= k;
e[i ^ ].w += k;
rest -= k;
}
}
return flow - rest;
} int main()
{ read(n); read(c); read(p);
S = * n + , T = S + ;
tot = ;
addedge(S , , inf);
for (int i = ; i <= c; i++)
{
int x , y;
read(x); read(y);
addedge(x + n , y , inf);
addedge(y + n , x , inf);
}
for (int i = ; i <= p; i++)
{
int x;
read(x);
a[x] = true;
}
addedge( , n + , inf);
for (int i = ; i <= n; i++)
{
if (a[i])
{
addedge(i , n + i , inf);
addedge(i , T , inf);
} else addedge(i , n + i , );
}
int ans = ;
while (bfs())
{
while (int flow = dinic(S , inf)) ans += flow;
}
printf("%d\n" , ans);
return ; }

[Usaco2009 MAR] Earthquake Damage 2的更多相关文章

  1. bzoj 1585: [Usaco2009 Mar]Earthquake Damage 2 地震伤害

    1585: [Usaco2009 Mar]Earthquake Damage 2 地震伤害 Description Farmer John的农场里有P个牧场,有C条无向道路连接着他们,第i条道路连接着 ...

  2. 【BZOJ】1585: [Usaco2009 Mar]Earthquake Damage 2 地震伤害

    [题意]给定无向图,现在可能有一些点已经被删除,只给出信息是c个点未被删除且不能到达结点1,求最少的删除点个数. [算法]最小割 [题解]本题和1的区别是:1求的是最少的不能到达1的结点数,那么就把损 ...

  3. 【bzoj1585】[Usaco2009 Mar]Earthquake Damage 2 地震伤害 网络流最小割

    题目描述 Farmer John的农场里有P个牧场,有C条无向道路连接着他们,第i条道路连接着两个牧场Ai和Bi,注意可能有很多条道路连接着相同的Ai和Bi,并且Ai有可能和Bi相等.Farmer J ...

  4. BZOJ1585: [Usaco2009 Mar]Earthquake Damage 2 地震伤害

    n<=3000个点m<=20000条无向边的图,有p<=n个出发点,每个出发点都不可拆,现拆一些点使每个出发点都不能到达点1,求最小点数. 简单的最小割.每个点拆成两个x和y,无向边 ...

  5. bzoj 1585: [Usaco2009 Mar]Earthquake Damage 2 地震伤害【最小割】

    枚举建图.jpg 一开始建的图挂了,于是枚举了几种建图方式-- 因为要删点,所以拆点,连接(i,i',1),对于原来图上的边(u,v),连接(u',v,inf),(v',u,inf),然后连接(s,i ...

  6. bzoj 3399: [Usaco2009 Mar]Sand Castle城堡

    3399: [Usaco2009 Mar]Sand Castle城堡 Time Limit: 3 Sec  Memory Limit: 128 MB Description 约翰用沙子建了一座城堡.正 ...

  7. BZOJ3401: [Usaco2009 Mar]Look Up 仰望

    3401: [Usaco2009 Mar]Look Up 仰望 Time Limit: 3 Sec  Memory Limit: 128 MBSubmit: 87  Solved: 58[Submit ...

  8. BZOJ3400: [Usaco2009 Mar]Cow Frisbee Team 奶牛沙盘队

    3400: [Usaco2009 Mar]Cow Frisbee Team 奶牛沙盘队 Time Limit: 3 Sec  Memory Limit: 128 MBSubmit: 89  Solve ...

  9. BZOJ3399: [Usaco2009 Mar]Sand Castle城堡

    3399: [Usaco2009 Mar]Sand Castle城堡 Time Limit: 3 Sec  Memory Limit: 128 MBSubmit: 22  Solved: 17[Sub ...

随机推荐

  1. B/S 开发和 C/S开发的区别

    导读:每天都从应用中心下载很多软件安装尝试,在自己的电脑上也装了很多软件,但是,就出现了一个问题,好比QQ,为什么有了APP,还要有网站应用呢?由此,结合到自己的学习,就衍生出一个问题:C/S 开发就 ...

  2. BZOJ2059: [Usaco2010 Nov]Buying Feed 购买饲料

    数轴上n<=500个站可以买东西,每个站位置Xi,库存Fi,价格Ci,运东西价格是当前运载重量的平方乘距离,求买K<=10000个东西到达点E的最小代价. f[i,j]--到第i站不买第i ...

  3. 积累js中的一些问题及解决方案

    一.取字符串的第i位不兼容的问题 1.问题:对于字符串str来说,要获取第i位,常见的是str[i],但是在低版本的浏览器中不兼容,例如ie7. 2.解决:使用str.charAt(i); 二.使用定 ...

  4. vagrant的学习之Git学习

    vagrant的学习之Git学习 参考学习网址: http://www.runoob.com/git/git-install-setup.html. http://www.bootcss.com/p/ ...

  5. C/C++ (一)

    c语言中的逻辑运算符都是短路运算,一旦能够确定整个表达式的值就不再计算,配合c的定义的灵活性,可以写出很多漂亮的程序. 例如 如果要在一个长为n的数列s中找到第k个没被标记过的数 for(i=1,j= ...

  6. C# 获得图片的分辨率和大小

    double DPI = pictureBox1.Image.HorizontalResolution;//获得分辨率 gisoracle double w = 1.0 * pictureBox1.I ...

  7. ubuntu安装nginx时提示error: the HTTP rewrite module requires the PCRE library

    ubuntu安装nginx时提示error: the HTTP rewrite module requires the PCRE library 须要安装pcre包. sudo apt-get upd ...

  8. C语言必会面试题(3、耶稣有13个门徒,当中有一个就是出卖耶稣的叛徒,请用排除法找出这位叛徒:13人围坐一圈,从第一个開始报号:1,2,3,1,2,3...。凡是报到“3”就退出圈子,...)

    3.耶稣有13个门徒.当中有一个就是出卖耶稣的叛徒,请用排除法找出这位叛徒:13人围坐一圈,从第一个開始报号:1.2,3.1,2,3.... 凡是报到"3"就退出圈子.最后留在圈子 ...

  9. css中使input输入框与img(图片)在同一行居中对齐

    input,img{vertical-align:middle;},同时设置input和img的vertical-align属性,兼容ie7

  10. YII获取当前URL

    <?php //当前域名   echoYii::app()->request->hostInfo;   //除域名外的URL   echoYii::app()->request ...