天天看點

hdu 4289 Control 網絡流,最小割,拆點題目連結:http://acm.hdu.edu.cn/showproblem.php?pid=4289 Control

題目連結:http://acm.hdu.edu.cn/showproblem.php?pid=4289

Control

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 1555    Accepted Submission(s): 682

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 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個點,每個點都有各自的cost, 然後M 無向條邊

要求割去S點到D路線中的點,使之無法從S到D ,而且要求消耗的cost和最小.  

這是一道網絡流的題. 算的是最小割. 根據最大流最小割定理. 可以直接算最大流;

但是這題的的流量限制是在點上的.是以要我們來拆點.

我這題是把i 點的  點首和點尾 分别設為 i 和 i+n;  顯然 最後會得到2*n個點

hdu 4289 Control 網絡流,最小割,拆點題目連結:http://acm.hdu.edu.cn/showproblem.php?pid=4289 Control

如圖, 然後把 點首到點尾的流量限制設成 題目要求的cost; 

而點與點之間的邊,要設成正無窮大, 因為邊不消耗cost;

然後從S的點首S 跑到 D的點尾 D+n  就可以計算出最小割了.

//13.4 ISAP+bfs 初始化+棧優化
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
#include <queue>
#include <stack>
#include <vector>
#include <deque>
#include <set>
#include <map>
const int MAXN = 600;//點數的最大值
const int MAXM =  90000;//邊數的最大值
const int INF = 2000000000;
struct Edge
{
	int to,next,cap,flow;
}edge[MAXM];//注意是MAXM
int tol;
int head[MAXN];
int gap[MAXN],dep[MAXN],cur[MAXN];
void init()
{
	tol = 0;
	memset(head,-1,sizeof (head));
}
void addedge (int u,int v,int w,int rw = 0)
{
	edge[tol].to = v; edge[tol].cap = w; edge[tol].flow = 0;
	edge[tol].next = head[u]; head[u] = tol++;
	edge[tol].to = u; edge[tol].cap = rw; edge[tol].flow = 0;
	edge[tol].next = head[v]; head[v] = tol++;
}
int Q[MAXN];
void BFS(int start,int end)
{
	memset(dep,-1,sizeof(dep));
	memset(gap,0,sizeof(gap));
	gap[0] = 1;
	int front = 0, rear = 0;
	dep[end] = 0;
	Q[rear++] = end;
	while(front != rear)
	{
		int u = Q[front++];
		for(int i = head[u]; i !=  -1; i = edge[i].next)
		{
			int v = edge[i]. to;
			if(dep[v] != -1)continue;
			Q[rear++] = v;
			dep[v] = dep[u] + 1;
			gap[dep[v]]++;
		}
	}
}
int S[MAXN];
int sap(int start,int end, int N)
{
	BFS(start,end);
	memcpy(cur,head,sizeof(head));
	int top = 0;
	int u = start;
	int ans = 0;
	int i;

	while(dep[start] < N)
	{
		if(u == end)
		{
			int Min = INF;
			int inser;
			for( i = 0;i < top;i++)
			{
				if(Min > edge[S[i]].cap - edge[S[i]].flow)
				{
					Min = edge[S[i]].cap - edge[S[i]].flow;
					inser = i;
				}
			}
			for( i = 0;i < top;i++)
			{
				edge[S[i]]. flow += Min;
				edge[S[i]^1].flow -= Min;
			}
			ans += Min;
			top = inser;
			u = edge[S[top]^1].to;
			continue;
		}
		bool flag =  false;
		int v;
		for( i = cur[u]; i != -1; i = edge[i]. next)
		{
			v = edge[i]. to;
			if(edge[i].cap - edge[i].flow && dep[v]+1 == dep[u])
			{
				flag =  true;
				cur[u] = i;
				break;
			}
		}
		if(flag)
		{
			S[top++] = cur[u];
			u = v;
			continue;
		}
		int Min = N;
		for( i = head[u]; i !=  -1; i = edge[i].next)
		{
			if(edge[i].cap - edge[i].flow && dep[edge[i].to] < Min)
			{
				Min = dep[edge[i].to];
				cur[u] = i;
			}
		}
		gap[dep[u]]--;
		if(!gap[dep[u]]) return ans;
		dep[u] = Min + 1;
		gap[dep[u]]++;
		if(u != start)u = edge[S[--top]^1].to;
	}
	return ans;
}
int main()
{
	int n,m,a,b,tem,s,t,i;
	while(scanf("%d%d",&n,&m)!=EOF)
	{
		init();
		scanf("%d%d",&s,&t);
		for(i=1;i<=n;i++)
        {
            scanf("%d",&tem);
            addedge(i,i+n,tem);//單向
        }
		for(int i=1;i<=m;i++)
		{
			scanf("%d%d",&a,&b);
			addedge(b+n,a,INF,0);
			addedge(a+n,b,INF,0);
		}
		printf("%d\n",sap(s,t+n,2*n));
	}
	return 0;
}