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

Source


思路:  最小割最大流定理,网上证明多多也比较简单。 所以题目就是一道基础的网络流了,建图的时候要拆点 ,为什么呢:

举个例子:假设 有a的流量流到了点u ,假设 u的cost为b ,如果b<a;  那么我只需要去掉u,即花费为b,就可以使得s到t不连通了,所以需要拆点限流

还是那句话,网络流很多地方都可以拆点建图滴

#include<stdio.h>
#include<string.h>
#include <string>
#include <cmath>
#include <iostream>
#include <map>
#include<vector>
#include<queue>
#include<algorithm>
#define fr(i,s,n) for(int i=s;i<n;i++)
#define pf printf
#define sf scanf
#define sfv1(a) scanf("%d",&a)
#define sfv2(n,m) scanf("%d%d",&n,&m)
#define sfv3(u,v,w) scanf("%d%d%d",&u,&v,&w)
#define sfstr(c) scanf("%s",c)
#define pfv1(a) printf("%d\n",a)
#define fi freopen("in.txt","r",stdin)
#define fo freopen("out.txt","w",stdout)
#define cl(a) memset(a,0,sizeof(a))
#define me(a,x) memset(a,x,sizeof(a))
#define inf 1000000000
using namespace std;
typedef long long ll;

const int pN=2000,eN=3000000;

struct Edge{
	int u,v,next;
	int w;
}e[eN];

int en,head[pN];
int f[210],d[210];

void init(){
	me(head,-1);
	en=0;
}
void add(int u,int v,int w){
	e[en].u=u;e[en].v=v;e[en].w=w;e[en].next=head[u];head[u]=en++;
	e[en].u=v;e[en].v=u;e[en].w=0;e[en].next=head[v];head[v]=en++;
}

int cur[pN],ps[pN],dep[pN];    //dinic实现
int max_flow(int n,int s,int t){   
	int tr,res=0;
	int i,j,k,f,r,top;
	while(1){
		memset(dep,-1,n*sizeof(int));
		for(f=dep[ps[0]=s]=0,r=1;f!=r;)
			for(i=ps[f++],j=head[i];j!=-1;j=e[j].next){
				if(e[j].w&&-1==dep[k=e[j].v]){
					dep[k]=dep[i]+1;
					ps[r++]=k;
					if(k==t){
						f=r;
						break;
					}
				}
			}
			if(-1==dep[t])break;
			memcpy(cur,head,n*sizeof(int));
			for(i=s,top=0;;){
				if(i==t){
					for(k=0,tr=inf;k<top;++k)
						if(e[ps[k]].w<tr)
							tr=e[ps[f=k]].w;
					for(k=0;k<top;++k)
						e[ps[k]].w-=tr,e[ps[k]^1].w+=tr;
					res+=tr;
					i=e[ps[top=f]].u;
				}
				for(j=cur[i];cur[i]!=-1;j=cur[i]=e[cur[i]].next)
					if(e[j].w&&dep[i]+1==dep[e[j].v])break;
				if(cur[i]!=-1){
					ps[top++]=cur[i];
					i=e[cur[i]].v;
				}else{
					if(0==top)break;
					dep[i]=-1;
					i=e[ps[--top]].u;
				}
			}
	}
	return res;
}

int n,m;
int main(){
	int s,t;
	int u,v;
	int x;
	while(sfv2(n,m)!=EOF){
		init();
		sfv2(s,t);
		fr(i,1,n+1){
			sfv1(x);
			add(i,i+n,x);
		}
		fr(i,0,m){
			sfv2(u,v);
			add(u+n,v,inf);
			add(v+n,u,inf);
		}
		int ans=max_flow(2*n+1,s,t+n);
		pfv1(ans);
	}
	return 0;
}

sap  : 当前弧+gap+前向星优化

#include<stdio.h>
#include<string.h>
#include <string>
#include <cmath>
#include<algorithm>
#define fr(i,s,n) for(int i=s;i<n;i++)
#define pf printf
#define sf scanf
#define sfv1(a) scanf("%d",&a)
#define sfv2(n,m) scanf("%d%d",&n,&m)
#define fi freopen("in.txt","r",stdin)
#define inf 1000000000
using namespace std;

const int pN=5000,eN=100000;

struct Edge{
	int u,v,nxt;
	int w;
}e[eN];

int en,head[pN];

void init(){
	memset(head,-1,sizeof(head));
	en=0;
}
void add(int u,int v,int w){
	e[en].u=u;e[en].v=v;e[en].w=w;e[en].nxt=head[u];head[u]=en++;
	e[en].u=v;e[en].v=u;e[en].w=0;e[en].nxt=head[v];head[v]=en++;
}
int dep[pN],gap[pN],que[pN]; //gap  每一次重标号时 若出现了断层,则可以证明st无可行流,此时可以直接退出算法
void BFS(int n,int s,int t){
	memset(dep,-1,n * sizeof(int));
	memset(gap, 0 ,n * sizeof (int));
	gap[0] = 1;
	int f = 0,r = 0,u,v;
	dep[ t ] = 0; que[r ++] = t; //从后外前面标号
	while(f != r){
		u = que[f ++];
		if ( f == pN) f = 0;
		for(int i = head[u];i != -1;i = e[i].nxt){
			v = e[i].v;
			if (e[i].w != 0 || dep[v] != -1) continue;  
			que[ r++ ] = v;
			if (r == pN) r = 0;
			dep[ v ] = dep[ u ] + 1;
			++ gap[dep[ v ]];   //这里的gap就是每一层有多少个点
		}
	}
}

int cur[pN],sta[pN];
int sap(int n,int s,int t){        //n为总的点个数 包括源点和汇点
	int flow = 0;
	BFS(n,s,t);
	int top = 0,u = s,i;       
	memcpy(cur,head,n*sizeof(int));   //当前弧
	while( dep[s] < n){
		if ( u == t){
			int tmp = inf;
			int pos;     
			for(i = 0;i < top;i++){
				if (tmp > e[ sta[i] ].w){
					tmp = e[ sta[i] ].w;
					pos = i;
				}
			}
			for(i = 0;i < top; ++i){
				e[ sta[i] ].w -= tmp;
				e[ sta[i]^1 ].w += tmp;
			}
			flow += tmp;
			top = pos;
			u = e[sta[top]].u;
		}
		if(u != t && gap[dep[u] - 1] == 0)  break;  //gap 优化 出现断层后直接退出
		for(i = cur[u] ; i != -1 ;i = e[i].nxt)          //当前弧优化  因为以前的弧绝对不满足要求
			if(e[i].w != 0 && dep[u] == dep[e[i].v] + 1) break;  //找到了一条最短增广路
		if (i != -1) cur[ u ] = i,sta[top ++] = i, u = e[i].v;   
		else{                                     
			/*这里是与dinic不同的地方不用每次的bfs 而是充分利用以前的距离标号的信息
			有这个定理:从源点到汇点的最短路一定是用允许弧构成。 
			所以每次扩展路径都找允许弧,
			如果i没有允许弧就更新dis[i] = min{ dis[j] + 1 | r[i][j] > 0);*/
			int mn = n;
			for (i = head[u] ;i != -1;i = e[i].nxt){     
				if ( e[i].w != 0 && mn > dep[ e[i].v ] ){
					mn = dep[ e[i].v ] ;
					cur[u] = i;                          
				}
			}
			-- gap[ dep[u] ];  
			dep[u] = mn + 1;   
			++ gap[ dep[u] ];  
			if (u != s) u = e[sta[--top]].u;
		}
	}
	return flow;
}
int n,m;
int main(){
	int s,t;
	int u,v;
	int x;
	while(sfv2(n,m)!=EOF){
		init();
		sfv2(s,t);
		fr(i,1,n+1){
			sfv1(x);
			add(i,i+n,x);
		}
		fr(i,0,m){
			sfv2(u,v);
			add(u+n,v,inf);
			add(v+n,u,inf);
		}
		int ans=sap(2*n+1,s,t+n);
		printf("%d\n",ans);
	}
	return 0;
}





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值