BZOJ3442: 学习小组

19 篇文章 0 订阅

最小费用最大流
源连向每个学生流量为k,费用为-Fi
每个学生连向他能去的学习小组,流量为1,费用为0
每个学习小组的支出费用与参加的人数平方相关,所以拆边连向汇(类似石头剪刀布),拆出n条边代表参加的人数为1~n时增加的支出,因为最小费用,所以总是会先选小的边,比如第i条边,选的时候1~i-1一定已经流满了,那这时i的贡献为
Ci2C(i1)2=C(2i1)
流量为1,费用就是C*(2i-1)

这题有个坑,即要求参加的学生尽量多,并不要求每个学生参加尽量多的小组,只要参加至少一个即可,所以每个学生连k-1的流量到汇,费用0
code:

#include<set>
#include<map>
#include<deque>
#include<queue>
#include<stack>
#include<cmath>
#include<ctime>
#include<bitset>
#include<string>
#include<vector>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<climits>
#include<complex>
#include<iostream>
#include<algorithm>
#define ll long long
using namespace std;

inline void down(int &x,const int &y){if(x>y)x=y;}
const int maxn = 1100;
const int maxm = 41000;

int n,m,st,ed;

struct edge
{
    int y,c,d,nex;
    edge(){}
    edge(const int _y,const int _c,const int _d,const int _nex){y=_y;c=_c;d=_d;nex=_nex;}
}a[maxm<<1]; int len,fir[maxn];
inline void ins(const int x,const int y,const int c,const int d)
{
    a[++len]=edge(y,c,d,fir[x]); fir[x]=len;
    a[++len]=edge(x,0,-d,fir[y]); fir[y]=len;
}

struct node
{
    int x,i;
    node(){}
    node(const int &_x,const int &_i){x=_x;i=_i;}
};
inline bool operator <(node x,node y){return x.x>y.x;}
priority_queue<node>q;
int d[maxn],pre[maxn],pos[maxn];
bool bfs()
{
    for(int i=1;i<=ed;i++) d[i]=INT_MAX;
    q.push(node(0,st)); d[st]=0;
    while(!q.empty())
    {
        const node x=q.top(); q.pop();
        if(x.x!=d[x.i]) continue;
        for(int k=fir[x.i];k;k=a[k].nex)
        {
            const int y=a[k].y;
            if(a[k].c&&d[y]>x.x+a[k].d)
            {
                d[y]=x.x+a[k].d;
                pos[y]=x.i; pre[y]=k;
                q.push(node(d[y],y));
            }
        }
    }
    return d[ed]!=INT_MAX;
}
int Flow()
{
    int re=0;
    while(bfs())
    {
        int c=INT_MAX;
        for(int k=ed;k!=st;k=pos[k]) down(c,a[pre[k]].c);
        re+=d[ed]*c;
        for(int k=ed;k!=st;k=pos[k]) a[pre[k]].c-=c,a[pre[k]^1].c+=c;
    }
    return re;
}
int fi[maxn];

int main()
{
    len=1;
    int K; scanf("%d%d%d",&n,&m,&K); st=n+m+1,ed=st+1;
    for(int i=1;i<=n;i++) ins(st,i,K,0),ins(i,ed,K-1,0);
    for(int i=1;i<=m;i++)
    {
        int x; scanf("%d",&x);
        for(int j=1;j<=n;j++) ins(n+i,ed,1,x*(j*j-(j-1)*(j-1)));
    }
    for(int i=1;i<=m;i++) scanf("%d",&fi[i]);

    char str[maxn];
    for(int i=1;i<=n;i++)
    {
        scanf("%s",str+1);
        for(int j=1;j<=m;j++) if(str[j]=='1')
            ins(i,n+j,1,-fi[j]);
    }

    printf("%d\n",Flow());

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值