【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号点为教室,\ ...
随机推荐
- php+redis 学习 一 连接
<?php header('content-type:text/html;chaeset=utf-8'); $redis = new Redis(); $redis->connect('1 ...
- WebSocket就是这么简单
前言 今天在慕课网上看到了Java的新教程(Netty入门之WebSocket初体验):https://www.imooc.com/learn/941 WebSocket我是听得很多,没有真正使用过的 ...
- Websocket原理及使用场景[转载]
WebSocket的使用场景 社交聊天.弹幕.多玩家游戏.协同编辑.股票基金实时报价.体育实况更新.视频会议/聊天.基于位置的应用.在线教育.智能家居等需要高实时的场景 由轮询到WebSocket 1 ...
- git使用基本故障
warning: LF will be replaced by CRLF in README.md. The file will have its original line endings in y ...
- MySQL select语句直接导出数据
select * into outfile '文件存放路径' from 表名; (先记下来,还未测试)
- Ubuntu 配置FTP服务器
第三方的文件传输软件用着很不爽,想着自己搭建一个FTP来干活. 首先检查是否已经安装了FTP,输入命令: vsftpd -v 可以查看版本,如果没有安装,无法执行. 安装FTP p.p1 { mar ...
- 洛谷P2832 行路难 分析+题解代码【玄学最短路】
洛谷P2832 行路难 分析+题解代码[玄学最短路] 题目背景: 小X来到了山区,领略山林之乐.在他乐以忘忧之时,他突然发现,开学迫在眉睫 题目描述: 山区有n座山.山之间有m条羊肠小道,每条连接两座 ...
- 9、flask之SQLAlchemy
本篇导航: 介绍 使用 SQLAlchemy-Utils 一. 介绍 SQLAlchemy是一个基于Python实现的ORM框架.该框架建立在 DB API之上,使用关系对象映射进行数据库操作,简言之 ...
- 自制PHP高防防盗链(不是一般的高)(思路)
原理:根据IP,资源ID,时间戳,一次性Access_Token,APPKEY(暴露在前台)和APPSERECT(后台)来生成参数,具体见下面: 浏览器请求页面=>后台引用防盗链代码=>生 ...
- HDU - 1584 IDA*
思路:裸的IDA*,估计当前状态至少需要多少距离才能达到目标状态,剪枝即可.每一墩牌只需记录其最上面和最下面的牌型即可完成移动. AC代码 #include <cstdio> #inclu ...