[網絡流] bzoj3442: 學習小組

bzoj3442: 學習小組 http://www.lydsy.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;
}
發佈了105 篇原創文章 · 獲贊 18 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章