码迷,mamicode.com
首页 > 其他好文 > 详细

[Luogu4177][CEOI2008]order

时间:2018-04-03 22:12:15      阅读:149      评论:0      收藏:0      [点我收藏+]

标签:min   body   最大   容量   int   while   for   gpo   inline   

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;
}

[Luogu4177][CEOI2008]order

标签:min   body   最大   容量   int   while   for   gpo   inline   

原文地址:https://www.cnblogs.com/zhoushuyu/p/8711006.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!