1. 程式人生 > >hdu 4289 Control(拆點+最大流)

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;
}