HDU 4289 Control(最大流)

【题目链接】
http://acm.hdu.edu.cn/showproblem.php?pid=4289

题目意思

有n个城市,m条边。每个城市用有花费权值。花费对应权值可以去除这个城市问断绝s-t连通的最少花费

解题思路

最小割值上等于最大流,把每个城市拆成p,p’.权值为城市权值,其他边p进p’出权值为无限大

代码部分

#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
#define inf 0x3f3f3f3
#define LL long long
#define pii pair
const int N = 450;
int dis[N]; 
int mp[N][N];
int n,m,s,t;
bool bfs ()
{
	memset(dis,-1,sizeof(dis));
	dis[s] = 0;
	queue<int> q;
	q.push(s);
	while (!q.empty())
	{
		int u = q.front();
		q.pop();
		for (int i = 1; i <= 2*n; i++)
		{
			if (dis[i] == -1 && mp[u][i] > 0)
			{
				dis[i] = dis[u]+1;
				q.push(i);
			}
		}
	}
	return dis[t+n] != -1;
}
int dfs(int v,int flow)
{
	if (v == t+n)
		return flow;
	int delta = flow;
	for (int i = 1; i <= 2*n; i++)
	{
		if(dis[i]==dis[v]+1 && mp[v][i] > 0)
		{
			int a = dfs(i,min(delta,mp[v][i]));
			if(a)
			{
				mp[v][i] -= a;
				mp[i][v] += a;
				delta -= a;
				if (delta == 0)
					return flow;
			}
		}
	}
	return flow-delta;
}
int dinic()
{
	int ans = 0;
	while(bfs())
	{
		ans += dfs(s,inf);
	}
	return ans;
}
int main()
{
	int v,u,w;
	while(~scanf("%d %d",&n,&m))
	{
		memset(mp,0,sizeof(mp));
		scanf("%d %d",&s,&t);
		for (int i = 1; i <= n; i++)
		{
			scanf("%d",&w);
			mp[i][i+n] = w;
		}
		
		for (int i = 1; i <= m; i++)
		{
			scanf("%d %d",&u,&v);
			mp[u+n][v] = inf;
			mp[v+n][u] = inf; 
		}
		cout<<dinic()<<endl;
	}
	return 0;
}

你可能感兴趣的:(网络流)