【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
",Dinic());
	return 0;	
}

原文地址:https://www.cnblogs.com/cjyyb/p/8143140.html