bzoj1391: [Ceoi2008]order(最小割)

本文介绍了一个基于最小割算法的问题解决方案,通过将问题转化为图论中的最小割问题来求解最大获利。具体步骤包括构建图模型、设定源点与汇点、定义边权等,并给出了完整的C++代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目传送门

解法:
裸的最小割吧。
st连任务流量为收益。
任务连机器流量为租金
机器连ed流量为花费。
这样的话一条路径的流量可以看作把某部分割掉。
跟最大获利同理。。
听说要用什么弧优化。。
去看hzwer博客?

代码实现:

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<queue>
using namespace std;
struct node {int x,y,next,other,c;}a[3100000];int len,last[3100];
void ins(int x,int y,int c) {
    int k1,k2;
    len++;k1=len;a[len].x=x;a[len].y=y;a[len].c=c;a[len].next=last[x];last[x]=len;
    len++;k2=len;a[len].x=y;a[len].y=x;a[len].c=0;a[len].next=last[y];last[y]=len;
    a[k1].other=k2;a[k2].other=k1;
}
int head,tail,list[3100],st,ed,h[3100];
bool bt_h() {
    head=1;tail=2;list[1]=st;memset(h,0,sizeof(h));h[st]=1;
    while(head!=tail) {
        int x=list[head];
        for(int k=last[x];k;k=a[k].next) {
            int y=a[k].y;
            if(h[y]==0&&a[k].c>0) {h[y]=h[x]+1;list[tail++]=y;}
        }head++;
    }if(h[ed]==0)return false;return true;
}
int cur[3100];
int find_flow(int x,int f) {
    if(x==ed)return f;
    int s=0,t;
    for(int k=cur[x];k;k=a[k].next) {
        int y=a[k].y;
        if(h[y]==h[x]+1&&a[k].c>0&&s<f) {
            t=find_flow(y,min(a[k].c,f-s));s+=t;a[k].c-=t;a[a[k].other].c+=t;
            if(a[k].c>0) cur[x]=k;if(s==f)break;
        }
    }if(s==0)h[x]=0;return s;
}
int main() {
    int n,m,sum=0;scanf("%d%d",&n,&m);st=n+m+1;ed=st+1;
    len=0;memset(last,0,sizeof(last));
    for(int i=1;i<=n;i++) {
        int c,k;scanf("%d%d",&c,&k);ins(st,i,c);sum+=c;
        for(int j=1;j<=k;j++) {int x;scanf("%d%d",&x,&c);ins(i,x+n,c);}
    }
    for(int i=1;i<=m;i++) {int x;scanf("%d",&x);ins(i+n,ed,x);}
    int ans=0;
    while(bt_h()==true) {
        for(int i=1;i<=ed;i++)cur[i]=last[i];
        ans+=find_flow(st,999999999);
    }printf("%d\n",sum-ans);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值