I - Control HDU - 4289(最小割+拆点+双向边)

You, the head of Department of Security, recently received a top-secret information that a group of terrorists is planning to transport some WMD 1 from one city (the source) to another one (the destination). You know their date, source and destination, and they are using the highway network.
  The highway network consists of bidirectional highways, connecting two distinct city. A vehicle can only enter/exit the highway network at cities only.
  You may locate some SA (special agents) in some selected cities, so that when the terrorists enter a city under observation (that is, SA is in this city), they would be caught immediately.
  It is possible to locate SA in all cities, but since controlling a city with SA may cost your department a certain amount of money, which might vary from city to city, and your budget might not be able to bear the full cost of controlling all cities, you must identify a set of cities, that:
  * all traffic of the terrorists must pass at least one city of the set.
  * sum of cost of controlling all cities in the set is minimal.
  You may assume that it is always possible to get from source of the terrorists to their destination.

1 Weapon of Mass Destruction
Input
  There are several test cases.
  The first line of a single test case contains two integer N and M ( 2 <= N <= 200; 1 <= M <= 20000), the number of cities and the number of highways. Cities are numbered from 1 to N.
  The second line contains two integer S,D ( 1 <= S,D <= N), the number of the source and the number of the destination.
  The following N lines contains costs. Of these lines the ith one contains exactly one integer, the cost of locating SA in the ith city to put it under observation. You may assume that the cost is positive and not exceeding 10 7.
  The followingM lines tells you about highway network. Each of these lines contains two integers A and B, indicating a bidirectional highway between A and B.
  Please process until EOF (End Of File).
Output
  For each test case you should output exactly one line, containing one integer, the sum of cost of your selected set.
  See samples for detailed information.
Sample Input
5 6
5 3
5
2
3
4
12
1 5
5 4
2 3
2 4
4 3
2 1
Sample Output
3
给定一个n个点,m条边组成的无向图,再给出源点S和汇点D,每个点都有点权值,求这张图的最小割。
网络流中最小割等于最大流,题目由于是点权值,所以需要拆点,i到i+n建边,权值为点权值,后面给出的路径i+n到j建边,权值为无限,j+n到i建边,权值为无限,然后跑最大流。

#include<stdio.h>
#include<iostream>
#include<cmath>
#include<queue>
#include<cstring>
using namespace std;
const int INF=0x3f3f3f3f;
const int maxn=1e4+5;
const int maxm=1e5+5;
int n,m,S,T,ne,head[maxn],cur[maxn],depth[maxn],maxflow;
queue<int> q;
struct edge
{
	int next,to,cost;
}e[maxm*2];
void init()
{
	memset(head,-1,sizeof(head));
	ne=0;
	maxflow=0;
}
void add(int u,int v,int w)
{
	e[ne].to=v;
	e[ne].cost=w;
	e[ne].next=head[u];
	head[u]=ne++;
	e[ne].to=u;
	e[ne].cost=0;
	e[ne].next=head[v];
	head[v]=ne++;
}
int bfs(int s,int t)
{
	while(!q.empty()) q.pop();
	memset(depth,-1,sizeof(depth));
	for(int i=1;i<=n*2;i++) cur[i]=head[i];
	depth[s]=0;
	q.push(s);
	while(!q.empty())
	{
		int u=q.front();
		q.pop();
		for(int i=head[u];i!=-1;i=e[i].next)
		{
			if(depth[e[i].to]==-1&&e[i].cost)
			{
				depth[e[i].to]=depth[u]+1;
				q.push(e[i].to);
			}
		}
	}
	if(depth[t]==-1) return -1;
	else return 1;
}
int dfs(int s,int t,int limit)
{
	if(!limit||s==t) return limit;
	int f,flow=0;
	for(int i=cur[s];i!=-1;i=e[i].next)
	{
		cur[s]=i;
		if(depth[e[i].to]==depth[s]+1&&(f=dfs(e[i].to,t,min(limit,e[i].cost))))
		{
			flow+=f;
			limit-=f;
			e[i].cost-=f;
			e[i^1].cost+=f;
			if(!limit) break;
		}
	}
	return flow;
}
void dinic(int s,int t)
{
	while(bfs(s,t)!=-1)
	{
		maxflow+=dfs(s,t,INF);
	}
}
int main()
{
	while(~scanf("%d%d",&n,&m))
	{
		init();
		scanf("%d%d",&S,&T);
		T=T+n;
		for(int i=1;i<=n;i++)
		{
			int x;
			scanf("%d",&x);
			add(i,i+n,x);
		}
		for(int i=1;i<=m;i++)
		{
			int x,y;
			scanf("%d%d",&x,&y);
			add(x+n,y,INF);
			add(y+n,x,INF);
		}
		dinic(S,T);
		printf("%d\n",maxflow);
	}
}

猜你喜欢

转载自blog.csdn.net/zufe_cst/article/details/86499680