BZOJ 2055 80人环游世界 有上下界的费用流

12 篇文章 0 订阅

题目大意:给定n个点,每个点有固定的经过次数,m个人从任意节点出发任意节点结束,只能向右走,要求总边权和最小

有源汇、有上下界的费用流

其实上下界费用流有两种写法- - 一种是按照上下界网络流那么转化- - 一种是把必经边的费用减掉一个INF 跑完再加回去

我比较倾向于第一种写法- - 第二种写法在INF的取值上有点麻烦- -

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define M 220
#define S 210
#define T 211
#define _S 212
#define _T 213
#define INF 0x3f3f3f3f
using namespace std;
struct abcd{
	int to,flow,cost,next;
}table[1001001];
int head[M],tot=1;
int n,m,ans;
void Add(int x,int y,int f,int c)
{
	table[++tot].to=y;
	table[tot].flow=f;
	table[tot].cost=c;
	table[tot].next=head[x];
	head[x]=tot;
}
void Link(int x,int y,int f,int c)
{
	Add(x,y,f,c);
	Add(y,x,0,-c);
}
bool Edmonds_Karp()
{
	static int q[65540],flow[M],cost[M],from[M];
	static unsigned short r,h;
	static bool v[M];
	int i;
	memset(cost,0x3f,sizeof cost);
	flow[S]=INF;cost[S]=0;q[++r]=S;
	while(r!=h)
	{
		int x=q[++h];v[x]=0;
		for(i=head[x];i;i=table[i].next)
			if(table[i].flow&&cost[table[i].to]>cost[x]+table[i].cost)
			{
				cost[table[i].to]=cost[x]+table[i].cost;
				flow[table[i].to]=min(flow[x],table[i].flow);
				from[table[i].to]=i;
				if(!v[table[i].to])
					v[table[i].to]=true,q[++r]=table[i].to;

			}
	}
	if(cost[T]==INF) return false;
	ans+=flow[T]*cost[T];
	for(i=from[T];i;i=from[table[i^1].to])
		table[i].flow-=flow[T],table[i^1].flow+=flow[T];
	return true;
}
int main()
{
	int i,j,x;
	cin>>n>>m;
	Link(_T,_S,m,0);
	for(i=1;i<=n;i++)
	{
		scanf("%d",&x);
		Link(S,i<<1,x,0);
		Link(i+i-1,T,x,0);
		Link(_S,i+i-1,INF,0);
		Link(i<<1,_T,INF,0);
	}
	for(i=1;i<=n;i++)
		for(j=i+1;j<=n;j++)
		{
			scanf("%d",&x);
			if(~x) Link(i<<1,j+j-1,INF,x);
		}

	while( Edmonds_Karp() );
	cout<<ans<<endl;
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值