【bzoj1834】[ZJOI2010]network 網絡擴容

Description

給定一張有向圖,每條邊都有一個容量C和一個擴容費用W。這裏擴容費用是指將容量擴大1所需的費用。
求: 
1、在不擴容的情況下,1到N的最大流; 
2、將1到N的最大流增加K所需的最小擴容費用。

Input

第一行包含三個整數N,M,K,表示有向圖的點數、邊數以及所需要增加的流量。 
接下來的M行每行包含四個整數u,v,C,W,表示一條從u到v,容量爲C,擴容費用爲W的邊。
N<=1000,M<=5000,K<=10

Output

輸出文件一行包含兩個整數,分別表示問題1和問題2的答案。

Sample Input

5 8 2
1 2 5 8
2 5 9 9
5 1 6 2
5 1 1 8
1 2 8 7
2 5 4 9
1 2 1 1
1 4 2 1

Sample Output

13 19
【解析】

網絡流+費用流
第一問很簡單,走一遍最大流算法
第二問要最小費用最大流,主要是建圖,從第一問的殘留網絡上繼續建圖,
對殘留網絡上的每一條邊建一條容量是999999999費用是w的邊
然後建個源點,從源點向1建一條容量爲k,費用爲0的邊
對新圖進行最小費用最大流
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
struct node
{
	int x,y,c,d,t,next,other;
}a[51000];int len,last[1100];
int st,ed;
void ins(int x,int y,int c,int d)
{
	int k1,k2;
	len++;k1=len;
	a[len].x=x;a[len].y=y;a[len].c=c;a[len].t=d;
	a[len].next=last[x];last[x]=len;
	
	len++;k2=len;
	a[len].x=y;a[len].y=x;a[len].c=0;a[len].t=-d;
	a[len].next=last[y];last[y]=len;
	
	a[k1].other=k2;
	a[k2].other=k1;
}
void inss(int x,int y,int c,int d)
{
	int k1,k2;
	len++;k1=len;
	a[len].x=x;a[len].y=y;a[len].c=c;a[len].d=d;
	a[len].next=last[x];last[x]=len;
	
	len++;k2=len;
	a[len].x=y;a[len].y=x;a[len].c=0;a[len].d=-d;
	a[len].next=last[y];last[y]=len;
	
	a[k1].other=k2;
	a[k2].other=k1;
}
int list[1100],head,tail,h[1100];
bool bt_h()
{
	memset(h,0,sizeof(h));h[st]=1;
	list[1]=st;head=1;tail=2;
	while(head!=tail)
	{
		int x=list[head];
		for(int k=last[x];k;k=a[k].next)
		{
			int y=a[k].y;
			if(a[k].c>0 && h[y]==0)
			{
				h[y]=h[x]+1;
				list[tail++]=y;
			}
		}
		head++;
	}
	if(h[ed]>0) return true;
	else return false;
}
int findflow(int x,int f)
{
	if(x==ed) return f;
	int t,s=0;
	for(int k=last[x];k;k=a[k].next)
	{
		int y=a[k].y;
		if(a[k].c>0 && h[y]==(h[x]+1) && s<f)
		{
			t=findflow(y,min(a[k].c,f-s));
			s+=t;
			a[k].c-=t;
			a[a[k].other].c+=t;	
		}
	}
	if(s==0) h[x]=0;
	return s;
}
bool v[1100];
int pre[1100],d[1100];
int ans;
int n,m,k;
bool spfa()
{
	for(int i=0;i<=ed;i++) d[i]=999999999;
	d[st]=0;
	memset(v,true,sizeof(v));v[st]=false;
	list[1]=st;head=1;tail=2;
	while(head!=tail)
	{
		int x=list[head];
		for(int k=last[x];k;k=a[k].next)
		{
			int y=a[k].y;
			if(a[k].c>0 && d[y]>d[x]+a[k].d)
			{
				d[y]=d[x]+a[k].d;
				pre[y]=k;
				if(v[y]==true)
				{
					v[y]=false;
					list[tail++]=y;
					if(tail==ed+1) tail=1;
				}
			}
		}
		head++;
		if(head==ed+1) head=1;
		v[x]=true;
	}
	if(d[ed]>=999999999) return false;
	return true;
}
void get()
{
	int minn=999999999,x=ed;
	while(x!=st)
	{
		int k=pre[x];
		minn=min(minn,a[k].c);
		x=a[k].x;
	}
	x=ed;
	while(x!=st)
	{
		int k=pre[x];
		a[k].c-=minn;
		a[a[k].other].c+=minn;
		ans+=minn*a[k].d;
		x=a[k].x;
	}
}
int p[1100];
int main()
{
	scanf("%d%d%d",&n,&m,&k);
	len=0;memset(last,0,sizeof(last));
	st=1;ed=n;
	for(int i=1;i<=m;i++)
	{
		int x,y,c,dd;
		scanf("%d%d%d%d",&x,&y,&c,&dd);
		ins(x,y,c,dd);
	}
	int s=0;
	while(bt_h()==true) s+=findflow(st,999999999);
	printf("%d ",s);
	
	memset(list,0,sizeof(list));
	st=n+1;
	int f=len;
	for(int i=1;i<=f-1;i+=2) inss(a[i].x,a[i].y,999999999,a[i].t);
	ins(st,1,k,0);
	ans=0;
	while(spfa()==true) get();
	printf("%d\n",ans);
	return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章