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

[网络流] bzoj3442: 学习小组

2018-03-22 19:46 281 查看
bzoj3442: 学习小组 http://www.lyd 4000
sy.com/JudgeOnline/problem.php?id=3442

很强的费用流

费用Ci*a^2 这就很令人尴尬了

最开始想的是先统计每个小组有多少人参加 最后利用分配率加一加

现在想一想我当时怎么这么蠢

膜了题解才会做

太强了:https://www.cnblogs.com/GXZlegend/p/6809670.html

拆边+费用流 由于有Ci*a^2的存在,使得正常加边的费用流无法处理。我们可以加容量为1,费用为Ci*1、Ci*3、Ci*5、Ci*7、…的一堆边,这样在最小费用的前提下总花费满足题意。(1+3=4=2^2 1+3+5=9=3^2 1+3+5+7=16=4^2 <-太强辣%%)

每个学习小组向T连上述所说的边,S向每个学生连一条容量为k,费用为0的边,每个学生向其能参加的学习小组连一条容量为1,费用为Fi的边。

所以还要从每个学生向T连一条容量为k-1,费用为0的边,保证费用最小。(不必选满k个)

然后跑最小费用最大流即可。

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
struct node
{
int x,y,c,cost,next,other;
}a[51000];
int last[210],len;
int n,m,kk,c[110],p[110];
int f[210],list[11000000],pre[210];
bool v[210];
int st,ed,ans=0;
void build(int x,int y,int c,int cost)
{
len++;int k1=len;
a[len].x=x;a[len].y=y;a[len].c=c;a[len].cost=cost;a[len].next=last[x];last[x]=len;
len++;int k2=len;
a[len].x=y;a[len].y=x;a[len].c=0;a[len].cost=-cost;a[len].next=last[y];last[y]=len;
a[k1].other=k2;a[k2].other=k1;
}
bool spfa()
{
memset(f,63,sizeof(f));
memset(v,false,sizeof(v));
int head=1,tail=1;
list[1]=st;v[st]=true;f[st]=0;
while (head<=tail)
{
int x=list[head];
for (int k=last[x];k;k=a[k].next)
{
int y=a[k].y;
if (f[y]>f[x]+a[k].cost&&a[k].c>0)
{
pre[y]=k;
f[y]=f[x]+a[k].cost;
if (v[y]==false)
{
v[y]=true;
if (tail+1>11000000) tail=0;
list[++tail]=y;
}
}
}
if (head+1>11000000) head=0;
head++;
v[x]=false;
}
if (f[ed]>=999999999) return 0;
else
{
ans+=f[ed];
int x=ed,minn=999999999;
while (x!=st)
{
int k=pre[x];
minn=min(minn,a[k].c);
x=a[k].x;
}
x=ed;
while (x!=st)
{
int k=pre[x];
a[k].c-=minn;
a[a[k].other].c+=minn;
x=a[k].x;
}
return 1;
}
}
char ch[110];
int main()
{
//freopen("group.in","r",stdin);
//freopen("group.out","w",stdout);
scanf("%d%d%d",&n,&m,&kk);
for (int i=1;i<=m;i++) scanf("%d",&c[i]);
for (int i=1;i<=m;i++) scanf("%d",&p[i]);
st=n+m+1;ed=st+1;
for (int i=1;i<=n;i++)
{
scanf("%s",ch+1);
for (int j=1;j<=m;j++)
{
int x;
x=ch[j]-'0';
if (x==1) build(i,j+n,1,-p[j]);
}
}
for (int i=1;i<=n;i++) {build(st,i,kk,0);build(i,ed,kk-1,0);}
for (int i=1;i<=m;i++)
for (int j=1;j<=n;j++) {build(i+n,ed,1,c[i]*(2*j-1));}
while (spfa()) {}
printf("%d\n",ans);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: