zoukankan      html  css  js  c++  java
  • bzoj1391 [Ceoi2008]order

    1391: [Ceoi2008]order

    Time Limit: 10 Sec  Memory Limit: 64 MB
    Submit: 2073  Solved: 625
    [Submit][Status][Discuss]

    Description

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

    Input

    第一行给出 N,M(1<=N<=1200,1<=M<=1200) 下面将有N块数据,每块数据第一行给出完成这个任务能赚到的钱(其在[1,5000])及有多少道工序 接下来若干行每行两个数,分别描述完成工序所需要的机器编号及租用它的费用(其在[1,20000]) 最后M行,每行给出购买机器的费用(其在[1,20000])

    Output

    最大利润

    Sample Input

    2 3
    100 2
    1 30
    2 20
    100 2
    1 40
    3 80
    50
    80
    110

    Sample Output

    50

    HINT

    分析:和bzoj1497很像,只不过可以租机器.
       我们先按照最大权闭合子图的模型建图。考虑一条增广路,一定是S →项目→机器→ T。割第一条边是放弃项目,割第三条边是买机器。第二条边是无穷大。
       第二条边是无穷大的原因是一个机器会和多个项目对应. 那么在这道题中,只需要把无穷大改成租机器的费用就好了.
       直接上还是会T掉,需要用到当前弧优化.  这个优化挺简单的,记录一个cur数组,表示第i个点的第一条有流量的边是cur[i],和head数组差不多.  当递归到i这个点的时候,从cur[i]这条边开始遍历即可. 因为cur[i]之前的边不能再有流量了. 
       每次bfs完后都要将cur数组还原.  bfs中用head数组.
    #include <cstdio>
    #include <queue>
    #include <cstring>
    #include <iostream>
    #include <algorithm>
    
    using namespace std;
    
    const int maxn = 3000010,inf = 0x7fffffff;
    int n,m,a[maxn],head[3000],nextt[maxn],w[maxn],to[maxn],tot = 2,S,T;
    int d[3000],ans,cur[3000];
    
    void add(int x,int y,int z)
    {
        w[tot] = z;
        to[tot] = y;
        nextt[tot] = head[x];
        head[x] = tot++;
    
        w[tot] = 0;
        to[tot] = x;
        nextt[tot] = head[y];
        head[y] = tot++;
    }
    
    bool bfs()
    {
        memset(d,-1,sizeof(d));
        d[S] = 0;
        queue <int>q;
        q.push(S);
        while (!q.empty())
        {
            int u = q.front();
            q.pop();
            if (u == T)
                return true;
            for (int i = head[u];i;i = nextt[i])
            {
                int v = to[i];
                if (w[i] && d[v] == -1)
                {
                    d[v] = d[u] + 1;
                    q.push(v);
                }
            }
        }
        return false;
    }
    
    int dfs(int u,int f)
    {
        if (u == T)
            return f;
        int res = 0;
        for (int i = cur[u];i;i = nextt[i])
        {
            int v = to[i];
            if (w[i] && d[v] == d[u] + 1)
            {
                int temp = dfs(v,min(f - res,w[i]));
                w[i] -= temp;
                w[i ^ 1] += temp;
                res += temp;
                if (w[i] > 0)
                    cur[u] = i;
                if (res == f)
                    return res;
            }
        }
        if (res == 0)
            d[u] = -1;
        return res;
    }
    
    void dinic()
    {
        while (bfs())
        {
            for (int i = 1; i <= T; i++)
                cur[i] = head[i];
            ans -= dfs(S,inf);
        }
    }
    
    int main()
    {
        scanf("%d%d",&n,&m);
        S = n + m + 1;
        T = n + m + 2;
        for (int i = 1; i <= n; i++)
        {
            int x,num;
            scanf("%d%d",&x,&num);
            ans += x;
            add(S,i + m,x);
            for (int j = 1; j <= num; j++)
            {
                int temp,temp2;
                scanf("%d%d",&temp,&temp2);
                add(i + m,temp,temp2);
            }
        }
        for (int i = 1; i <= m; i++)
        {
            int x;
            scanf("%d",&x);
            add(i,T,x);
        }
        dinic();
        printf("%d
    ",ans);
    
        return 0;
    }
  • 相关阅读:
    Linux环境进程间通信
    monitor
    用prctl给线程命名
    openfire本地环境搭建和openfire插件开发实例
    ubuntu12.04的vim配置
    SensorThread线程
    AndroidPN中的心跳检测
    openfire插件开发之完美开发
    poj3322 Bloxorz I
    设计模式总结
  • 原文地址:https://www.cnblogs.com/zbtrs/p/8588455.html
Copyright © 2011-2022 走看看