[bzoj]3876: [Ahoi2014]支线剧情

可以看出来是有上下界的费用流,我们对于一条边权为z的边u,v,从s向u连一条容量为1的边,从u到v连上容量为

INF的边,让此边可以重复走。对于每个点p,从p到T连上一条流量为p,费用无的边,再从x到1连上一条费用无,

流量无限的边,直接跑网络流就行了。

跑的好慢啊。。。

#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
using namespace std;
const int N=305;
const int M=500005,Inf=1<<29;
int x,y;
int k;
int n;
struct edge{
	int to,nxt,ro,c,flow;
}e[M];
int head[N],tot;
inline void add(int u,int v,int ro,int c)
{
	e[tot].to=v,e[tot].ro=ro,e[tot].c=c,e[tot].nxt=head[u],head[u]=tot,tot++;
	e[tot].to=u,e[tot].ro=0,e[tot].c=-c,e[tot].nxt=head[v],head[v]=tot,tot++;
}
int sk[N];
bool vis[N];
int dis[N],prp[N],prd[N],qaq[N];
inline bool spfa(int s,int t)
{
	register int i;
	for (i=0;i<=n+1;i++) dis[i]=Inf;
	vis[s]=1;qaq[s]=Inf;dis[s]=0;
	queue<int> q;
	q.push(s);
	while (!q.empty())
	{
		int x=q.front();
		q.pop(),vis[x]=false;
		for (i=head[x];i!=-1;i=e[i].nxt)
		{
			if (e[i].ro>e[i].flow&&dis[x]+e[i].c<dis[e[i].to])
			{
				prp[e[i].to]=i;
				prd[e[i].to]=x;
				dis[e[i].to]=dis[x]+e[i].c;
				qaq[e[i].to]=min(qaq[x],e[i].ro-e[i].flow);
				if (!vis[e[i].to]) q.push(e[i].to),vis[e[i].to]=1;;
			}
		}
	}
	return dis[t]==Inf?0:1;
}
int sum;
inline void MCMF(int s,int t)
{
	int flow=0,cost=0;
	while (spfa(s,t))
	{
		for (int i=t;i!=s;i=prd[i])
		{
			e[prp[i]].flow+=qaq[t];
			e[prp[i]^1].flow-=qaq[t];
		}
		flow+=qaq[t];
		cost+=dis[t]*qaq[t];
	}
	printf("%d",cost);
}
int main()
{
	register int i,j;	
	scanf("%d",&n);
	memset(head,-1,sizeof(head));
	for (i=1;i<=n;i++)
	{	
		scanf("%d",&k);
		if (i!=1) add(i,1,Inf,0);
		add(i,n+1,k,0);
		for (j=1;j<=k;j++)
		{
			sum+=y;
			scanf("%d %d",&x,&y);
			add(i,x,Inf,y);
			add(0,x,1,y);
		}
	}
	MCMF(0,n+1);
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值