【bzoj1570】[JSOI2008]Blue Mary的旅行 最大流

反正我自己肯定想不出来。

Popoqqq大爷的题解:http://blog.csdn.net/popoqqq/article/details/44095061


#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<iostream>
#define maxn 10010
#define maxm 1000010
#define inf 1000000000

using namespace std;

struct yts
{
	int x,y,z;
}e[maxn];

int head[maxn],to[maxm],c[maxm],next[maxm],q[maxn],d[maxn];
int num,s,t,n,m,ans,T;

void addedge(int x,int y,int z)
{
	num++;to[num]=y;c[num]=z;next[num]=head[x];head[x]=num;
	num++;to[num]=x;c[num]=0;next[num]=head[y];head[y]=num;
}

bool bfs()
{
	memset(d,-1,sizeof(d));
	int l=0,r=1;
	q[1]=s;d[s]=0;
	while (l<r)
	{
		int x=q[++l];
		for (int p=head[x];p;p=next[p])
		  if (c[p] && d[to[p]]==-1)
		  {
		  	d[to[p]]=d[x]+1;
		  	q[++r]=to[p];
		  }
	}
	if (d[t]==-1) return 0; else return 1;
}

int find(int x,int low)
{
	if (x==t || low==0) return low;
	int totflow=0;
	for (int p=head[x];p;p=next[p])
	  if (c[p] && d[to[p]]==d[x]+1)
	  {
	  	int a=find(to[p],min(low,c[p]));
	  	c[p]-=a;c[p^1]+=a;
	  	low-=a;totflow+=a;
	  	if (low==0) return totflow;
	  }
	if (low) d[x]=-1;
	return totflow;
}

int main()
{
	scanf("%d%d%d",&n,&m,&T);
	for (int i=1;i<=m;i++) scanf("%d%d%d",&e[i].x,&e[i].y,&e[i].z);
	num=1;s=0;t=(n+T+1)*n+1;ans=0;
	addedge(s,1,T);
	for (int k=1;k<=n+T;k++)
	{
		for (int i=1;i<=m;i++) addedge((k-1)*n+e[i].x,k*n+e[i].y,e[i].z);
		for (int i=1;i<=n;i++) addedge((k-1)*n+i,k*n+i,inf);
		addedge((k+1)*n,t,T);
		while (bfs()) ans+=find(s,inf);
		if (ans==T)
		{
			printf("%d\n",k);
			return 0;
		}
	}
	return 0;
}


你可能感兴趣的:(【bzoj1570】[JSOI2008]Blue Mary的旅行 最大流)