HDU 4289 Control(拆点+最大流) ʕ •ᴥ•ʔ

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个城市,现在城市s出现了一群人,他们想运送一些炸弹到e城市,警方采取封锁城市的办法来阻断暴徒,不过封锁城市是需要花费一定代价的,求阻断暴徒从S城市到达D城市的最小需要花费的代价。

思路:还是最大流问题,建图:将每个城市点拆开i和i’,连接两点权值就是花费;可连每个城市和每个城市之间的权值为N,跑一边最大流即可。 然后一开始对弧优化那里 还是不理解 一直死循环  有多少点就优化多少个点 这题有2*n个点 所有是 从0 -> 2*n

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<vector>
#include<queue>
#define N 0x3f3f3f3f
#define ll long long
using namespace std;
int s,e;
int per[80000];
int cur[80000];
int dis[80000];
struct ac
{
	int v;
	int w;
	int nxt;
}r[80010];
int cnt;
int 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=per[u];i+1;i=r[i].nxt)
		{
			int x=r[i].v;
			if(dis[x]==-1&&r[i].w)
			{
				dis[x]=dis[u]+1;
				q.push(x);
			}
		}
	}
	if(dis[e]>-1)
	return 1;
	return 0;
}
int dfs(int u,int h)
{
	if(u==e||h==0)
	return h;
	int flow,f=0;
	for(int &i=cur[u];i!=-1;i=r[i].nxt)// & 取地址符  
	{
		if(dis[r[i].v]==dis[u]+1&&r[i].w>0&&(flow=dfs(r[i].v,min(h,r[i].w)))>0)
		{
			r[i].w-=flow;
			r[i^1].w+=flow;
			h-=flow;
			f+=flow;
			if(h==0)
			break;
		}
	}
	return f;
}
void add(int u,int v,int w)
{
	r[cnt].v=v;
	r[cnt].w=w;
	r[cnt].nxt=per[u];
	per[u]=cnt++;
	
	r[cnt].v=u;
	r[cnt].w=0;
	r[cnt].nxt=per[v];
	per[v]=cnt++;
}
int main()
{
	int n,m;
	while(cin>>n>>m)
	{
		cnt=0;
		memset(per,-1,sizeof(per));
		cin>>s>>e;
		for(int i=1;i<=n;i++)
		{
			int p;
			cin>>p;
			add(i,i+n,p);
		}
		for(int i=1;i<=m;i++)
		{
			int a,b;
			cin>>a>>b;
			add(a+n,b,N);
			add(b+n,a,N);
		}
		e=e+n;
		int sum=0;
		while(bfs())
		{
			for(int i=0;i<=2 * n;i++)
			{
				cur[i]=per[i];//弧优化 
			}
			sum+=dfs(s,N);
		}
		cout<<sum<<endl;
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/henucm/article/details/81988020
今日推荐