【dinic最小割】 I - Control HDU - 4289

Control

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4505    Accepted Submission(s): 1865


 

Problem Description

  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

Source

2012 ACM/ICPC Asia Regional Chengdu Online

/// 最大流 = 最小割

#include <bits/stdc++.h>
using namespace std;

const int inf = 0x7fffffff;
const int mn = 500, mm = 101000;

int edge;
int fr[mn];
int lv[mn];
struct node
{
	int to, val, nx, fan;
} e[mm];

void init()
{
	edge = 0;
	memset(fr, -1, sizeof fr);
}

void addedge(int u, int v, int w)
{
	edge++;
	e[edge].to = v, e[edge].val = w, e[edge].nx = fr[u], e[edge].fan = edge + 1;
	fr[u] = edge;
	edge++;
	e[edge].to = u, e[edge].val = 0, e[edge].nx = fr[v], e[edge].fan = edge - 1;
	fr[v] = edge;
}

void bfs(int s)
{
	memset(lv, 0, sizeof lv);
	lv[s] = 1;
	queue<int> q;
	q.push(s);
	while (!q.empty())
	{
		int t = q.front();
		q.pop();
		for (int i = fr[t]; i != -1; i = e[i].nx)
		{
			if (e[i].val > 0 && !lv[e[i].to])
			{
				lv[e[i].to] = lv[t] + 1;
				q.push(e[i].to);
			}
		}
	}
}

int dfs(int s, int t, int f)
{
	if (s == t)
		return f;
	for (int i = fr[s]; i != -1; i = e[i].nx)
	{
		if (e[i].val > 0 && lv[s] < lv[e[i].to])
		{
			int d = dfs(e[i].to, t, min(f, e[i].val));
			if (d > 0)
			{
				e[i].val -= d;
				e[e[i].fan].val += d;
				return d;
			}
		}
	}
	return 0;
}

int max_flow(int s, int t)
{
	int flow = 0;
	while (1)
	{
		bfs(s);
		if (!lv[t])
			break;

		int f = 0;
		while ((f = dfs(s, t, inf)) > 0)
			flow += f;
	}
	return flow;
}

int main()
{
	//freopen("D:\\in.txt", "r", stdin);
	int n, m;
	while (~scanf("%d %d", &n, &m))
	{
		init();

		int st, ed;
		scanf("%d %d", &st, &ed);

		for (int i = 1; i <= n; i++)
		{
			int x;
			scanf("%d", &x);
			addedge(i, 200 + i, x);
		}

		for (int i = 1; i <= m; i++)
		{
			int a, b;
			scanf("%d %d", &a, &b);
			addedge(a + 200, b, inf);
			addedge(b + 200, a, inf);
// 			建图注意方向 错↓
//			addedge(a, b, inf);
//			addedge(b + 200, a + 200, inf);
		}

		printf("%d\n", max_flow(st, ed + 200));
	}

	return 0;
}

猜你喜欢

转载自blog.csdn.net/ummmmm/article/details/81347549
今日推荐