[Luogu4177][CEOI2008]order

luogu

sol

这题有点像网络流24题里面的太空飞行计划啊。
最大收益=总收益-最小损失。
先令\(ans=\sum\)任务收益。
源点向每个任务连容量为收益的边。
每个机器向汇点连容量为购买费用的边。
每个任务向对应的机器连容量为租赁费用的边。
最小割即可。

code

\(bfs\)里面用了一个玄学卡常技巧。并没有什么用

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
using namespace std;
int gi()
{
    int x=0,w=1;char ch=getchar();
    while ((ch<'0'||ch>'9')&&ch!='-') ch=getchar();
    if (ch=='-') w=0,ch=getchar();
    while (ch>='0'&&ch<='9') x=(x<<3)+(x<<1)+ch-'0',ch=getchar();
    return w?x:-x;
}
const int N = 2500;
const int inf = 1e9;[Luogu4177][CEOI2008]order
struct edge{int to,nxt,w;}a[N*N];
int n,m,S,T,head[N],cnt=1,dep[N],cur[N],ans;
queue<int>Q;
void link(int u,int v,int w)
{
    a[++cnt]=(edge){v,head[u],w};
    head[u]=cnt;
    a[++cnt]=(edge){u,head[v],0};
    head[v]=cnt;
}
bool bfs()
{
    memset(dep,0,sizeof(dep));dep[S]=1;
    while (!Q.empty()) Q.pop();Q.push(S);
    while (!Q.empty())
    {
        int u=Q.front();Q.pop();
        for (int e=head[u];e;e=a[e].nxt)
            if (a[e].w&&!dep[a[e].to])
            {
                dep[a[e].to]=dep[u]+1,Q.push(a[e].to);
                if (a[e].to==T) return true;
            }
    }
    return false;
}
int dfs(int u,int f)
{
    if (u==T) return f;
    for (int &e=cur[u];e;e=a[e].nxt)
        if (a[e].w&&dep[a[e].to]==dep[u]+1)
        {
            int tmp=dfs(a[e].to,min(a[e].w,f));
            if (tmp) {a[e].w-=tmp;a[e^1].w+=tmp;return tmp;}
        }
    return 0;
}
int Dinic()
{
    int res=0;
    while (bfs())
    {
        for (int i=1;i<=T;++i) cur[i]=head[i];
        while (int tmp=dfs(S,inf)) res+=tmp;
    }
    return res;
}
int main()
{
    n=gi();m=gi();S=n+m+1;T=S+1;
    for (int i=1;i<=n;++i)
    {
        int val=gi(),k=gi();
        link(S,i,val);ans+=val;
        while (k--)
        {
            int x=gi(),y=gi();
            link(i,n+x,y);
        }
    }
    for (int i=1,val;i<=m;++i) val=gi(),link(n+i,T,val);
    printf("%d\n",ans-Dinic());
    return 0;
}

转载于:https://www.cnblogs.com/zhoushuyu/p/8711006.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值