【BZOJ1585】【Luogu2944】地震损失2(网络流)
【BZOJ1585】【Luogu2944】地震损失2(网络流)
题面
题目描述
Wisconsin has had an earthquake that has struck Farmer John's farm! The earthquake has damaged some of the pastures so that they are unpassable. Remarkably, none of the cowpaths was damaged.
As usual, the farm is modeled as a set of P (1 <= P <= 3,000)
pastures conveniently numbered 1..P which are connected by a set of C (1 <= C <= 20,000) non-directional cowpaths conveniently
numbered 1..C. Cowpath i connects pastures a_i and b_i (1 <= a_i <= P; 1 <= b_i <= P). Cowpaths might connect a_i to itself or perhaps might connect two pastures more than once. The barn is located in pasture 1.
A total of N (1 <= N <= P) cows (in different pastures) sequentially contacts Farmer John via moobile phone with an integer message report_j (2 <= report_j <= P) that indicates that pasture report_j is undamaged but that the calling cow is unable to return to the barn from pasture report_j because she could not find a path that does not go through damaged pastures.
After all the cows report in, determine the minimum number of
pastures that are damaged.
地震袭击了威斯康星州,一些牧场被摧毁了.
一共有P个牧场.由C条双向路连接.两个牧场间可能有多条路.一条路也可能连接相同的牧场.牛棚坐落在牧场1.
N (1 <= N <= P) 只奶牛打来了求救电话,说她们的农场没有被摧毁,但是已经无法到达牛棚. 求出最少可能有多少牧场被摧毁.
输入输出格式
输入格式:
Line 1: Three space-separated integers: P, C, and N
Lines 2..C+1: Line i+1 describes cowpath i with two integers: a_i and b_i
Lines C+2..C+N+1: Line C+1+j contains a single integer: report_j
输出格式:
Line 1: One number, the minimum number of damaged pastures.
输入输出样例
输入样例#1:
5 5 2
1 2
2 3
3 5
2 4
4 5
4
5
输出样例#1:
1
题解
很明显的最小割
因为是割点
所以拆点
强制只能割掉点之间的边
因此其他边的流量为INF
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<set>
#include<map>
#include<vector>
#include<queue>
using namespace std;
#define MAXL 1000000
#define MAX 100000
#define INF 1000000000
inline int read()
{
int x=0,t=1;char ch=getchar();
while((ch<'0'||ch>'9')&&ch!='-')ch=getchar();
if(ch=='-')t=-1,ch=getchar();
while(ch<='9'&&ch>='0')x=x*10+ch-48,ch=getchar();
return x*t;
}
struct Line
{
int v,next,w;
}e[MAXL];
int h[MAX],cnt;
int ans,S,T,n,m;
inline void Add(int u,int v,int w)
{
e[cnt]=(Line){v,h[u],w};
h[u]=cnt++;
e[cnt]=(Line){u,h[v],0};
h[v]=cnt++;
}
int level[MAX];
int cur[MAX];
bool BFS()
{
memset(level,0,sizeof(level));
level[S]=1;
queue<int> Q;
Q.push(S);
while(!Q.empty())
{
int u=Q.front();Q.pop();
for(int i=h[u];i!=-1;i=e[i].next)
{
int v=e[i].v;
if(e[i].w&&!level[v])
level[v]=level[u]+1,Q.push(v);
}
}
return level[T];
}
int DFS(int u,int flow)
{
if(flow==0||u==T)return flow;
int ret=0;
for(int i=h[u];i!=-1;i=e[i].next)
{
int v=e[i].v;
if(e[i].w&&level[v]==level[u]+1)
{
int dd=DFS(v,min(flow,e[i].w));
flow-=dd;ret+=dd;
e[i].w-=dd;e[i^1].w+=dd;
}
}
return ret;
}
int Dinic()
{
int ret=0;
while(BFS())
{
//for(int i=S;i<=T;++i)cur[i]=h[i];
ret+=DFS(S,INF);
}
return ret;
}
int P,C,N;
bool vis[MAX];
int U[MAX],V[MAX],R[MAX];
int main()
{
memset(h,-1,sizeof(h));
P=read();C=read();N=read();
S=1;T=P+P+1;
Add(1,1+P,INF);
for(int i=1;i<=C;++i)
{
int u=read(),v=read();
Add(u+P,v,INF);Add(v+P,u,INF);
}
for(int i=1;i<=N;++i)vis[read()]=true;;
for(int i=2;i<=P;++i)
if(!vis[i])
Add(i,i+P,1);
else Add(i,i+P,INF),Add(i+P,T,INF);
printf("%d\n",Dinic());
return 0;
}
【BZOJ1585】【Luogu2944】地震损失2(网络流)的更多相关文章
- P2944 [USACO09MAR]地震损失2Earthquake Damage 2(网络流)
P2944 [USACO09MAR]地震损失2Earthquake Damage 2 $P$个点,$C$条双向边.求最少删去几个点使$N$个给定的点与点$1$分开. 显然的最小割. 将点$i$套路地拆 ...
- [USACO09MAR]地震损失2Earthquake Damage 2
地震破坏 时间限制: 1 Sec 内存限制: 128 MB 题目描述 威斯康星发生了一场地震!约翰的牧场遭到了打击,有一些牛棚变成了废墟,如果一间牛棚遭到 了破坏,那么所有和它相连的道路都不能使用了 ...
- 洛谷2944 [USACO09MAR]地震损失2Earthquake Damage 2
https://www.luogu.org/problem/show?pid=2944 题目描述 Wisconsin has had an earthquake that has struck Far ...
- p2944 [USACO09MAR]地震损失2Earthquake Damage 2
传送门 分析 我们让s到1,关键点到t分别连流量为inf的边 于是我们可以考虑跑s到t的最小割 于是我们将所有点拆为两个点,关键点和1的两个点之间连inf,其余点连1 将原图的边也连上,流量为inf ...
- 【P2944】地震损失(最大流,洛谷)
绝对难度虚高的一题 看到题目,至少损坏几个房子,开始考虑最小割,建的是双向边,所以拆点,边权除了自己与自己的之外都连inf.然后把所有求救的点都连到超级源上,跑一遍最大流就可以了. #include& ...
- 【bzoj1585】[Usaco2009 Mar]Earthquake Damage 2 地震伤害 网络流最小割
题目描述 Farmer John的农场里有P个牧场,有C条无向道路连接着他们,第i条道路连接着两个牧场Ai和Bi,注意可能有很多条道路连接着相同的Ai和Bi,并且Ai有可能和Bi相等.Farmer J ...
- BZOJ 1585: Earthquake Damage 2 地震伤害 网络流 + 最小割
Description Farmer John的农场里有P个牧场,有C条无向道路连接着他们,第i条道路连接着两个牧场Ai和Bi,注意可能有很多条道路连接着相同的Ai和Bi,并且Ai有可能和Bi相等.F ...
- BZOJ1585: [Usaco2009 Mar]Earthquake Damage 2 地震伤害
n<=3000个点m<=20000条无向边的图,有p<=n个出发点,每个出发点都不可拆,现拆一些点使每个出发点都不能到达点1,求最小点数. 简单的最小割.每个点拆成两个x和y,无向边 ...
- 洛谷 P10P1343 地震逃生 改错
P1343 地震逃生 题目描述 汶川地震发生时,四川**中学正在上课,一看地震发生,老师们立刻带领x名学生逃跑,整个学校可以抽象地看成一个有向图,图中有\(n\)个点,\(m\)条边.1号点为教室,\ ...
随机推荐
- java单例模式学习笔记
最近一直在学习多线程,在学习过程中遇到了关于单例模式的多线程安全问题,内容如下: 一:首先什么是单例模式 单例模式具有的三要点: 一个类只能有一个实例: 必须是由它自己创建的这个实例: 它必须自行向外 ...
- linux 下yum使用技巧
本文来自我的github pages博客http://galengao.github.io/ 即www.gaohuirong.cn 经常会遇上一些linux系统允许你上外网,而一些是不允许的,这时我们 ...
- Halcon一日一练:图像拼接技术2:步骤与例程
上一篇主要介绍了图像拼接的一些原理和方法,这一篇将主要介绍步骤和例程: 接上一篇: 基于特征的接拼方法,分为四个步骤 1.特征检测:从图像中检测出显著且独特的图像特征,诸如:闭合区域,直线段,边缘,轮 ...
- Java经典编程题50道之三十
有一个已经排好序的数组.现输入一个数,要求按原来的规律将它插入数组中. public class Example30 { public static void main(String[] arg ...
- Asp.Net Core Identity+EFCore + Mysql踩坑记录
搭建基础框架准备试试传说中的Identity,本以为很顺利,结果一路踩了N多坑 遂就把过程记录下来.方便自己以后查看,也希望能帮到遇到同样问题的朋友. 1.首先,引入Identity需要的类库,还有M ...
- APP性能测试(电量)
#encoding:utf-8 import csv import os import time #控制类 class Controller(object): def __init__(self, c ...
- 总结MySQL大数据量下如何进行优化
写在建库前: 在确定数据库业务后.建立数据库表格时,就应对一些常见问题有所考虑,以避免在数据增长一段时间后再做应对,可能造成时间及维护成本增加: 数据的月增量,年增量 数据的快速增长点 是否需要触发器 ...
- MS SQL xp_instance_regwrite设置注册表疑惑
以前写过一篇博文"MS SQL 日志记录管理",里面介绍了如何设置SQL Server的错误日志的最大归档数量,如果在SSMS的UI界面设置,可以从"Manageme ...
- 利用 xrandr 命令修改屏幕分辨率
问题背景 电脑外接着一个 23' 的显示器,分辨率为 1920*1080. 操作系统:ubuntu 14.04 今天,通过 Setting->Displays 中选择外接屏的分辨率时,发现最大只 ...
- Jenkins+Gradle实现android开发持续集成、打包
Jenkins简介 Jenkins 是一个开源项目,提供了一种易于使用的持续集成系统,使开发者从繁杂的集成中解脱出来,专注于更为重要的业务逻辑实现上.同时 Jenkins 能实施监控集成中存在的错误, ...