bzoj1391 [Ceoi2008]order 网络流——最大权闭合子图

题目描述:
有N个工作,M种机器,每种机器你可以租或者买过来. 每个工作包括若干道工序,每道工序需要某种机器来完成,你可以通过购买或租用机器来完成。 现在给出这些参数,求最大利润n<=1200,m<=1200。

题目分析:
最大权闭合子图模型。
从源点向工作连容量为收益的边。
从机器向汇点连容量为购买的边。
从工作向机器连容量为租用的边。
用总收益减去最小割(即最大流)即可。

这题不仅卡时,而且卡内存(我会告诉你们我交了3遍RE,3遍MLE,3遍TLE么orz……
读入优化,当前弧优化,时间戳……我尽力了Orz
代码如下:

#include <cstdio>
#include <iostream>
#define N 1600000
using namespace std;
namespace IStream
{
    char get_char()
    {
        const int L=1<<15;
        static char buffer[L];
        static char *C,*mempool;
        if(C==mempool) mempool=(C=buffer)+fread(buffer,1,L,stdin);
        return *C++;
    }
    int get_int()
    {
        int re=0;
        char c;
        do c=get_char();while(c<'0' || c>'9');
        while(c>='0' && c<='9') re=(re<<1)+(re<<3)+c-'0',c=get_char();
        return re;
    }
}
const int INF=0x3f3f3f3f;
inline int Min(int x,int y) { return x<y?x:y; }
int n,m,ans=0,S,T;
int Time_Stamp=0;
int fir[2500],nes[N<<1],v[N<<1],q[N<<1],tot=1;
int d[2500],dl[2500],cur[2500],tim[2500];
void edge(int x,int y,int z)
{
    v[++tot]=y;
    q[tot]=z;
    nes[tot]=fir[x];
    fir[x]=tot;
}
#define edge(x,y,z) edge(x,y,z),edge(y,x,0)
bool bfs()
{
    static int c;
    Time_Stamp++;
    int l=1,r=1;
    d[S]=1; dl[1]=S;
    tim[S]=Time_Stamp;
    while(l<=r)
    {
        c=dl[l++];
        for(int t=fir[c];t;t=nes[t])
        {
            if(!q[t] || tim[v[t]]==Time_Stamp) continue;
            tim[v[t]]=Time_Stamp;
            d[v[t]]=d[c]+1;
            dl[++r]=v[t];
            if(v[t]==T) return true;
        }
    }
    return false;
}
int dfs(int c,int flow)
{
    if(c==T || flow==0) return flow;
    int ans=0;
    for(int& t=cur[c];t;t=nes[t])
    {
        if(!q[t] || tim[v[t]]!=Time_Stamp || d[v[t]]!=d[c]+1) continue;
        int tmp=dfs(v[t],Min(flow,q[t]));
        q[t]-=tmp; q[t^1]+=tmp;
        flow-=tmp; ans+=tmp;
        if(!flow) break;
    }
    if(!ans) d[c]=-1;
    return ans;
}
int dinic()
{
    int ans=0;
    while(bfs())
    {
        for(int i=0;i<=T;i++) cur[i]=fir[i];
        ans+=dfs(S,INF);
    }
    return ans;
}
int main()
{
    using namespace IStream;
    cin>>n>>m;
    S=0;T=n+m+1;
    for(int i=1,x,y,cost;i<=n;i++)
    {
        x=get_int(); y=get_int();
        edge(S,i,x);
        ans+=x;
        while(y--)
        {
            x=get_int(); cost=get_int();
            edge(i,n+x,cost);
        }
    }
    for(int i=1,x;i<=m;i++)
    {
        x=get_int();
        edge(n+i,T,x);
    }
    ans-=dinic();
    printf("%d\n",ans);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值