[BZOJ2245][SDOI2011]工作安排(费用流)

211 篇文章 0 订阅
72 篇文章 0 订阅

题目描述

传送门

题解

对于每个产品,s->i,Ci,INF
对于员工j可以生产产品i,i->j,INF,Ci
对于每一个员工i以及枚举到的si=j,i->t,T[i][j]-T[i][j-1],W[i][j]

代码

#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
using namespace std;
#define LL long long

const int max_n=300;
const int max_N=max_n*10+2;
const int max_m=max_N*100;
const int max_e=max_m*2;
const LL INF=1e18;

LL m,n,sum,N,mincost;
LL A[max_n][max_n],s[max_n],ss[max_n],C[max_n],T[max_n][max_n],W[max_n][max_n];
LL tot,point[max_N],nxt[max_e],v[max_e],remain[max_e],c[max_e];
LL dis[max_N],last[max_N]; bool vis[max_N];
queue <LL> q;

inline void addedge(LL x,LL y,LL cap,LL z)
{
    ++tot; nxt[tot]=point[x]; point[x]=tot; v[tot]=y; remain[tot]=cap; c[tot]=z;
    ++tot; nxt[tot]=point[y]; point[y]=tot; v[tot]=x; remain[tot]=0; c[tot]=-z;
}
inline LL addflow(LL s,LL t)
{
    LL now=t,ans=INF;
    while (now!=s)
    {
        ans=min(ans,remain[last[now]]);
        now=v[last[now]^1];
    }
    now=t;
    while (now!=s)
    {
        remain[last[now]]-=ans;
        remain[last[now]^1]+=ans;
        now=v[last[now]^1];
    }
    return ans;
}
inline bool bfs(LL s,LL t)
{
    for (LL i=1;i<=N;++i) dis[i]=INF; dis[s]=0;
    memset(vis,0,sizeof(vis)); vis[s]=true;
    while (!q.empty()) q.pop(); q.push(s);

    while (!q.empty())
    {
        LL now=q.front(); q.pop();
        vis[now]=false;
        for (LL i=point[now];i!=-1;i=nxt[i])
            if (dis[v[i]]>dis[now]+c[i]&&remain[i])
            {
                dis[v[i]]=dis[now]+c[i];
                last[v[i]]=i;
                if (!vis[v[i]])
                {
                    vis[v[i]]=true;
                    q.push(v[i]);
                }
            }
    }
    if (dis[t]==INF) return false;
    LL flow=addflow(s,t);
    mincost+=flow*dis[t];
    return true;
}
inline void mincost_flow(LL s,LL t)
{
    while (bfs(s,t));
}
int main()
{
    tot=-1;
    memset(point,-1,sizeof(point));
    memset(nxt,-1,sizeof(nxt));

    scanf("%lld%lld",&m,&n);
    for (LL i=1;i<=n;++i) scanf("%lld",&C[i]);
    for (LL i=1;i<=m;++i)
        for (LL j=1;j<=n;++j)
            scanf("%lld",&A[i][j]);
    for (LL i=1;i<=m;++i)
    {
        scanf("%lld",&s[i]);
        for (LL j=1;j<=s[i];++j) scanf("%lld",&T[i][j]); T[i][s[i]+1]=INF;
        for (LL j=1;j<=s[i]+1;++j) scanf("%lld",&W[i][j]);
    }

    N=n+m+2;
    for (LL i=1;i<=n;++i)
        addedge(1,1+i,C[i],0);
    for (LL i=1;i<=n;++i)
        for (LL j=1;j<=m;++j)
            if (A[j][i])
                addedge(1+i,1+n+j,INF,0);
    for (LL i=1;i<=m;++i)
        for (LL j=1;j<=s[i]+1;++j)
            addedge(1+n+i,N,T[i][j]-T[i][j-1],W[i][j]);

    mincost_flow(1,N);
    printf("%lld\n",mincost);
}

总结

刚开始读错题了233

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值