HDU-4289-Control(最大流,Dinic+向前星,双向通道,拆点)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4289

 

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
 

扫描二维码关注公众号,回复: 2908870 查看本文章

Sample Output
3

题目大意:类似于:HDU-4292,多组输入。

开局n个城市,m条路;然后是每组的 起点s市 和 终点 t市

接下来是路过每个城市的花费 cost[i] 。然后是m条路(双向道路),我们要找到s到m的最大流

很明显的拆点,道路之间的流量限制为INF,城市的流量限制为cost[i];向前星就很容易建图,水题,套板子直接就过了,TLE数组加个0(反正是这样过的 23333....)

ac:

#include<stdio.h>
#include<string.h>  
#include<math.h>  
  
#include<map>   
//#include<set>
#include<deque>  
#include<queue>  
#include<stack>  
#include<bitset> 
#include<string>  
#include<fstream>
#include<iostream>  
#include<algorithm>  
using namespace std;  

#define ll long long  
#define INF 0x3f3f3f3f  
#define mod 998244353
//#define max(a,b) (a)>(b)?(a):(b)
//#define min(a,b) (a)<(b)?(a):(b) 
#define clean(a,b) memset(a,b,sizeof(a))// 水印 
//std::ios::sync_with_stdio(false);

struct node{
	int v,w,nxt;
	node(int _v=0,int _w=0,int _nxt=0):
	v(_v),w(_w),nxt(_nxt){}
}edge[100005<<1];
int head[100005],e;
int dis[100005];
int n,m;
int s,t;

void intt()
{
	clean(head,-1);
	clean(dis,-1);
	e=0;
}

void add(int u,int v,int w)
{
	edge[e]=node(v,w,head[u]);
	head[u]=e++;
	edge[e]=node(u,0,head[v]);
	head[v]=e++;
}

bool bfs()
{
	clean(dis,-1);
	dis[s]=0;
	queue<int> que;
	que.push(s);
	while(que.size())
	{
		int u=que.front();
		que.pop();
		if(u==t)
			return 1;
		for(int i=head[u];i+1;i=edge[i].nxt)
		{
			int temp=edge[i].v;
			if(dis[temp]<0&&edge[i].w>0)
			{
				dis[temp]=dis[u]+1;
				que.push(temp);
			}
		}
	}
	return 0;
}

int dfs(int u,int low)
{
	if(u==t)
		return low;
	int res=0;
	for(int i=head[u];i+1;i=edge[i].nxt)
	{
		int temp=edge[i].v;
		if(dis[temp]==dis[u]+1&&edge[i].w>0)
		{
			int d=dfs(temp,min(low-res,edge[i].w));
			edge[i].w-=d;
			edge[i^1].w+=d;
			res=res+d;
			if(res==low)
				break;
		}
	}
	return res;
}

void Dinic()
{
	int ans=0,res;
	while(bfs())
	{
		while(res=dfs(s,INF))
			ans=ans+res;
	}
	cout<<ans<<endl;
}

int main()
{
	while(cin>>n>>m)
	{
		intt();
		cin>>s>>t;
		int cost;
		for(int i=1;i<=n;++i)
		{
			cin>>cost;
			add(i,i+200,cost);
			add(i+200,i,cost);
		}
		int a,b;
		for(int i=1;i<=m;++i)
		{
			cin>>a>>b;
			add(a+200,b,INF);
			add(b+200,a,INF);
		}
		add(0,s,INF);
		add(t+200,401,INF);
		s=0,t=401;
		Dinic();
	}
}

猜你喜欢

转载自blog.csdn.net/qq_40482358/article/details/81907645
今日推荐