[题目链接]

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. hust 1017 dancing links 精确覆盖模板题

    最基础的dancing links的精确覆盖题目 #include <iostream> #include <cstring> #include <cstdio> ...

  2. Codeforces 892 A.Greed

    A. Greed time limit per test 2 seconds memory limit per test 256 megabytes input standard input outp ...

  3. HUNAN 11567 Escaping (最大流)

    http://acm.hunnu.edu.cn/online/?action=problem&type=list&courseid=0&querytext=&pagen ...

  4. Codeforces 659B Qualifying Contest【模拟,读题】

    写这道题题解的目的就是纪念一下半个小时才读懂题...英文一多读一读就溜号... 读题时还时要静下心来... 题目链接: http://codeforces.com/contest/659/proble ...

  5. 最近公共祖先(Least Common Ancestors)

    题意: 给定一棵有根树T,给出若干个查询lca(u, v)(通常查询数量较大),每次求树T中两个顶点u和v的最近公共祖先,即找一个节点,同时是u和v的祖先,并且深度尽可能大(尽可能远离树根).通常有以 ...

  6. 洛谷——P2916 [USACO08NOV]为母牛欢呼Cheering up the Cows

    https://www.luogu.org/problem/show?pid=2916 题目描述 Farmer John has grown so lazy that he no longer wan ...

  7. Best Time to Buy and Sell Stock(动态规划)

    Say you have an array for which the ith element is the price of a given stock on day i. If you were ...

  8. 2017多校Round7(hdu6120~hdu6132)

    补题进度:9/13 1001 待填坑 1002(数学推导) 题意 有一个按顺序的n个点的k叉树,问每个点子树个数的异或和是多少(n,k<=1e18) 分析 可以先求出最大的d,满足d以上都是满K ...

  9. Extjs.panel.Panel赋值的问题

    初学extjs,很是不爽.也是只有初学者才犯的错误,发出来以免再犯. 先创建一个panel var panel1 = Ext.create('Ext.panel.Panel', { id: 'p1', ...

  10. MD5加密Java工具类

    原文:http://www.open-open.com/code/view/1421764946296 import java.security.MessageDigest; public class ...