hdu 4289 Control(拆点+最大流)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=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 107.
  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

题意:给一个无向图,有些不法分子要从vs点vt点,现在要抓住所有的不法分子阻止他们去vt,那么就要控制某一些城市等待他们,控制每个城市花费不同,问最少花费是多少。
解题:最小割,割断所有的通路,花费使得最少,这样就一定能抓住所有的不法分子。拆点,每个点拆成一条有向边v->v ’ 边权为控制这个城市的花费,原图中的边u->v,则建成:u+n->v,v+n->u,边权都为INF。再跑一下最大流,就是ans。

#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<queue>
using namespace std;
const int maxn = 500;
const int inf = 0x3f3f3f3f;
struct node
{
	int to, cost, next;
}edge[maxn*maxn];
int head[maxn], num[maxn], level[maxn];
int n, m, tot, st, ed;
void init()
{
	tot = 0;
	memset(head, -1, sizeof(head));
}
void addedge(int u, int v,int w)
{
	edge[tot].to = v;
	edge[tot].cost = w;
	edge[tot].next = head[u];
	head[u] = tot++;

	edge[tot].to = u;
	edge[tot].cost = 0;
	edge[tot].next = head[v];
	head[v] = tot++;
	return;
}
bool bfs(int s, int t)
{
	queue<int>q;
	memset(level, 0, sizeof(level));
	q.push(s);
	level[s] = 1;
	while (!q.empty())
	{
		int u = q.front();
		q.pop();
		for (int i = head[u]; i != -1; i = edge[i].next)
		{
			int v = edge[i].to;
			if (!level[v] && edge[i].cost > 0)
			{
				level[v] = level[u] + 1;
				q.push(v);
			}
		}
	}
	return level[t] != 0;
}
int dfs(int s, int t, int f)
{
	if (s == t)
	{
		return f;
	}
	int cost = 0;
	for (int i = head[s]; i != -1; i = edge[i].next)
	{
		int v = edge[i].to;
		if (edge[i].cost > 0 && level[v] > level[s])
		{
			int d = dfs(v, t, min(f - cost, edge[i].cost));
			if (d > 0)
			{
				edge[i].cost -= d;
				edge[i ^ 1].cost += d;
				cost += d;
				if (cost == f)
				{
					break;
				}
			}
			else
			{
				level[v] = -1;
			}
		}
	}
	return cost;
}
int dinic()
{
	int ans = 0;
	while (bfs(st, ed))
	{
		ans += dfs(st, ed, inf);
	}
	return ans;
}
int main()
{
	//freopen("C://input.txt", "r", stdin);
	while (scanf("%d%d", &n, &m) != EOF)
	{
		init();
		st = 0, ed = 200 + n + 1;
		int x, y;
		scanf("%d%d", &x, &y);
		addedge(st, x, inf);
		addedge(y+200, ed, inf);
		for (int i = 1; i <= n; i++)
		{
			scanf("%d", &num[i]);
		}
		for (int i = 1; i <= n; i++)
		{
			addedge(i, 200 + i, num[i]);
			addedge(200 + i, i, num[i]);
		}
		for (int i = 1; i <= m; i++)
		{
			int u, v;
			scanf("%d%d", &u, &v);
			addedge(u + 200, v, inf);
			addedge(v + 200, u, inf);
		}
		printf("%d\n", dinic());
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/Evildoer_llc/article/details/83246968
今日推荐