bzoj2424

水题

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL maxn = 10000;
const LL maxm = 1000000;
const LL inf = 0x3f3f3f3f3f3f3f3f;
struct mcmf{

    LL he[maxn],tot,edge[maxm],ne[maxm],ver[maxm],cost[maxm];

    LL pre[maxn],d[maxn];

    bool vis[maxn];

    LL n;

    void init(LL x){

        n = x;

        tot = 1;

        for( LL i = 0;i <= n;i++ ) he[i] = 0;

    }

    void add(LL x,LL y,LL cap,LL c){

        ver[++tot] = y;

        ne[tot] = he[x];

        he[x] = tot;

        edge[tot] = cap;

        cost[tot] = c;

        ver[++tot] = x;

        ne[tot] = he[y];

        he[y] = tot;

        edge[tot] = 0;

        cost[tot] = -c;

    }

    bool spfa(LL s,LL t){

        queue<LL>q;

        for(LL i = 0;i <= n;i++){

            d[i] = inf;

            vis[i] = false;

            pre[i] = -1;

        }

        d[s] = 0;

        vis[s] = true;

        q.push(s);

        while(!q.empty()){

            LL x = q.front();

            q.pop();

            vis[x] = false;

            for(LL cure = he[x]; cure ;cure = ne[cure]){

                LL y = ver[cure];

                if(edge[cure] && d[y] > d[x] + cost[cure] ){

                    d[y] = d[x] + cost[cure];

                    pre[y] = cure;

                    if(!vis[y]){

                        vis[y] = true;

                        q.push(y);

                    }

                }

            }

        }

        if(pre[t] == -1)return false;

        else return true;

    }



    LL mincost(LL s,LL t,LL &cc){

        LL flow = 0;

        cc = 0;

        while(spfa(s,t)){

            LL mn = inf;

            for(LL cure = pre[t];cure != -1;cure = pre[ ver[cure^1] ]){

                if(mn > edge[cure])

                    mn = edge[cure];

            }

            for(LL cure = pre[t];cure != -1;cure = pre[ ver[cure^1] ]){

                edge[cure] -= mn;

                edge[cure^1] += mn;

                cc += cost[cure] * mn;

            }

            flow += mn;

        }

        return flow;

    }

} g;
LL u[maxn],d[maxn];
int main()
{
    LL n,m,s;
    scanf("%lld%lld%lld",&n,&m,&s);
    for( LL i = 1;i <= n;i++ ){
        scanf("%lld",&u[i]);
    }
    for( LL i = 1;i <= n;i++ ){
        scanf("%lld",&d[i]);
    }
    LL SS = 0,TT = 2*n+1;
    g.init( TT );
    for( LL i = 1;i <= n;i++ ){
        g.add( i+n,i,s,0 );
        g.add( SS,i+n,inf,d[i] );
        g.add( i+n,TT,u[i],0 );
        if( i != n ){
            g.add( i,i+1+n,inf,m );
        }
    }
    LL cost;
    LL maxflow = g.mincost(SS,TT,cost);
    printf("%lld",cost);
    return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值