您的位置:首页 > 理论基础 > 计算机网络

[网络流]bzoj1497 [NOI2006]最大获利

2018-02-05 17:19 423 查看
bzoj1497 [NOI2006]最大获利:http://www.lydsy.com/JudgeOnline/problem.php?id=1497

建边问题 建完边一切好说

用最大利润减去最小费用

st连人 容量为这个人能产生的利润 也就是割掉之后会损失多少利润

人连两个站点 容量为inf 防止被割掉

站点连ed 容量为建站费用

最小割=最大流

那么流出来的就是要割的部分

也就是最小费用啦

dalao:哎呀最大权闭合子图裸题 很简单的 balabalabala

我:……..????????我听不懂嘤嘤嘤

挂一个链接 http://blog.csdn.net/cold_chair/article/details/52841351

设s为源点,t为汇点。

使s连向所有的正权点(非负权点),边权为点权。

使所有非负权点(负权点)连向t,边权为点权的绝对值。

若需要选y才能选x,连一条由x到y的边,边权是∞。

最大点权和=正权点和-最小割

//最小割
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
struct node
{
int x,y,c,next,other;
}a[510000];
int last[110000],len;
int list[11000000],dep[110000];
int st,ed;
int x[51000],y[51000],c[51000],d[51000];
void build(int x,int y,int c)
{
int k1,k2;
len++;k1=len;
a[len].x=x;a[len].y=y;a[len].c=c;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].next=last[y];last[y]=len;
a[k1].other=k2;a[k2].other=k1;
}
bool bfs()
{
memset(dep,0,sizeof(dep));
int head=1,tail=1;
list[1]=st;dep[st]=1;
while (head<=tail)
{
int x=list[head];
for (int k=last[x];k;k=a[k].next)
{
int y=a[k].y;
if (dep[y]==0&&a[k].c>0)
{
dep[y]=dep[x]+1;
list[++tail]=y;
}
}
head++;
}
if (dep[ed]==0) return 0;
else return 1;
}
int dfs(int x,int flow)
{
int tot=0,sum;
if (x==ed) return flow;
for (int k=last[x];k;k=a[k].next)
{
int y=a[k].y;
if (dep[y]==dep[x]+1&&a[k].c>0&&flow-tot>0)
{
sum=dfs(y,min(flow-tot,a[k].c));
tot+=sum;a[k].c-=sum;a[a[k].other].c+=sum;
}
}
if (tot==0) dep[x]=0;
return tot;
}
int main()
{
//st-人-站-ed
int n,m;
int ans=0,sum=0;
scanf("%d%d",&n,&m);
for (int i=1;i<=n;i++) scanf("%d",&d[i]);
for (int i=1;i<=m;i++) {scanf("%d%d%d",&x[i],&y[i],&c[i]);ans+=c[i];}
st=n+m+1,ed=n+m+2;
for (int i=1;i<=m;i++) build(st,i,c[i]);//割掉这个点会损失多少利润(产生多少费用)
for (int i=1;i<=m;i++) {build(i,m+x[i],999999999);build(i,m+y[i],999999999);}//开inf防止被割
for (int i=1;i<=n;i++) build(m+i,ed,d[i]);//建站费用
while (bfs())
{
int t=dfs(st,999999999);
while (t>0)
{
sum+=t;
t=dfs(st,999999999);
}
}
printf("%d\n",ans-sum);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: