[上下界费用流] BZOJ2055 80人环游世界

%%% http://blog.csdn.net/popoqqq/article/details/44000835

#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#define V G[p].v
#define oo 1<<30
using namespace std;

inline char nc()
{
	static char buf[100000],*p1=buf,*p2=buf;
	if (p1==p2) { p2=(p1=buf)+fread(buf,1,100000,stdin); if (p1==p2) return EOF; }
	return *p1++;
}

inline void read(int &x)
{
	char c=nc(),b=1;
	for (;!(c>='0' && c<='9');c=nc()) if (c=='-') b=-1;
	for (x=0;c>='0' && c<='9';x=x*10+c-'0',c=nc()); x*=b;
}

struct edge{
	int u,v,w,f;
	int next;
};

edge G[1000005];
int head[215],inum=1;

inline void add(int u,int v,int f,int w,int p){
	G[p].u=u; G[p].v=v; G[p].f=f; G[p].w=w; G[p].next=head[u]; head[u]=p;
}

inline void link(int u,int v,int f,int w){
	add(u,v,f,w,inum+=2),add(v,u,0,-w,inum-1);
}

int s,t,S,T,Mincost;
const int NQ=1000;
int Q[NQ],l,r;
int dis[215],ins[215],pre[215];

inline bool SPFA()
{
	int u;
	for (int i=1;i<=T;i++) dis[i]=1<<30,pre[i]=0,ins[i]=0;
	memset(Q,0,sizeof(Q));
	l=r=-1; Q[(++r)%NQ]=S; dis[S]=0; ins[S]=1;
	while (l!=r)
	{
		u=Q[(++l)%NQ]; ins[u]=0;
		for (int p=head[u];p;p=G[p].next)
			if (G[p].f && dis[V]>dis[u]+G[p].w)
			{
				dis[V]=dis[u]+G[p].w; pre[V]=p;
				if (!ins[V])
					Q[(++r)%NQ]=V,ins[V]=1;
			}
	}
	if (dis[T]==1<<30) return false;
	int minimum=1<<30;
	for (int p=pre[T];p;p=pre[G[p].u])
		minimum=min(minimum,G[p].f);
	for (int p=pre[T];p;p=pre[G[p].u])
	{
		G[p].f-=minimum; G[p^1].f+=minimum;
		Mincost+=G[p].w*minimum;
	}
	return true;
}

int n,m;

int main()
{
	int x;
	freopen("t.in","r",stdin);
	freopen("t.out","w",stdout);
	read(n); read(m);
	s=2*n+1,t=2*n+2; S=2*n+3; T=2*n+4;
	link(t,s,m,0);
	for (int i=1;i<=n;i++)
	{
		read(x);
		link(S,i<<1,x,0);
		link(i*2-1,T,x,0);
		link(s,i*2-1,oo,0);
		link(i<<1,t,oo,0);
	}
	for (int i=1;i<=n;i++)
		for (int j=i+1;j<=n;j++)
		{
			read(x);
			if (~x) 
				link(i<<1,j*2-1,oo,x);
		}
	while (SPFA());
	printf("%d\n",Mincost);
	return 0;
}


你可能感兴趣的:([上下界费用流] BZOJ2055 80人环游世界)