zoukankan      html  css  js  c++  java
  • BZOJ 1061: [Noi2008]志愿者招募(线性规划与网络流)

     http://www.lydsy.com/JudgeOnline/problem.php?id=1061

    题意:

    思路:

    直接放上大神的建模过程!!!(https://www.byvoid.com/zhs/blog/noi-2008-employee

    这道题正确的解法是构造网络,求网络最小费用最大流,但是模型隐藏得较深,不易想到。构造网络是该题的关键,以下面一个例子说明构图的方法和解释。

    例如一共需要4天,四天需要的人数依次是4,2,5,3。有5类志愿者,如下表所示:

    种类 1 2 3 4 5
    时间 1-2 1-1 2-3 3-3 3-4
    费用 3 4 3 5 6

    设雇佣第i类志愿者的人数为X[i],每个志愿者的费用为V[i],第j天雇佣的人数为P[j],则每天的雇佣人数应满足一个不等式,如上表所述,可以列出

    P[1] = X[1] + X[2] >= 4

    P[2] = X[1] + X[3] >= 2

    P[3] = X[3] + X[4] +X[5] >= 5

    P[4] = X[5] >= 3

    对于第i个不等式,添加辅助变量Y[i] (Y[i]>=0) ,可以使其变为等式

    P[1] = X[1] + X[2] - Y[1] = 4

    P[2] = X[1] + X[3] - Y[2] = 2

    P[3] = X[3] + X[4] +X[5] - Y[3] = 5

    P[4] = X[5] - Y[4] = 3

    在上述四个等式上下添加P[0]=0,P[5]=0,每次用下边的式子减去上边的式子,得出

    ① P[1] - P[0] = X[1] + X[2] - Y[1] = 4

    ② P[2] - P[1] = X[3] - X[2] -Y[2] +Y[1] = -2

    ③ P[3] - P[2] = X[4] + X[5] - X[1] - Y[3] + Y[2] =3

    ④ P[4] - P[3] = - X[3] - X[4] + Y[3] - Y[4] = -2

    ⑤ P[5] - P[4] = - X[5] + Y[4] = -3

    观察发现,每个变量都在两个式子中出现了,而且一次为正,一次为负。所有等式右边和为0。接下来,根据上面五个等式构图。

    • 每个等式为图中一个顶点,添加源点S和汇点T。
    • 如果一个等式右边为非负整数c,从源点S向该等式对应的顶点连接一条容量为c,权值为0的有向边;如果一个等式右边为负整数c,从该等式对应的顶点向汇点T连接一条容量为c,权值为0的有向边。
    • 如果一个变量X[i]在第j个等式中出现为X[i],在第k个等式中出现为-X[i],从顶点j向顶点k连接一条容量为∞,权值为V[i]的有向边。
    • 如果一个变量Y[i]在第j个等式中出现为Y[i],在第k个等式中出现为-Y[i],从顶点j向顶点k连接一条容量为∞,权值为0的有向边。

    构图以后,求从源点S到汇点T的最小费用最大流,费用值就是结果。

    根据上面的例子可以构造出如下网络,红色的边为每个变量X代表的边,蓝色的边为每个变量Y代表的边,边的容量和权值标已经标出(蓝色没有标记,因为都是容量∞,权值0)。

    noi_employee_1

    在这个图中求最小费用最大流,流量网络如下图,每个红色边的流量就是对应的变量X的值。

    noi_employee_2

    所以,答案为43+23+3*6=36。

    上面的方法很神奇得求出了结果,思考为什么这样构图。我们将最后的五个等式进一步变形,得出以下结果

    ① - X[1] - X[2] + Y[1] + 4 = 0

    ② - X[3] + X[2] + Y[2] - Y[1] - 2 = 0

    ③ - X[4] - X[5] + X[1] + Y[3] - Y[2] + 3 = 0

    ④ X[3] + X[4] - Y[3] + Y[4] - 2 = 0

    ⑤ X[5] - Y[4] - 3 = 0

    可以发现,每个等式左边都是几个变量和一个常数相加减,右边都为0,恰好就像网络流中除了源点和汇点的顶点都满足流量平衡。每个正的变量相当于流入该顶点的流量,负的变量相当于流出该顶点的流量,而正常数可以看作来自附加源点的流量,负的常数是流向附加汇点的流量。因此可以据此构造网络,求出从附加源到附加汇的网络最大流,即可满足所有等式。而我们还要求noi_employee_3最小,所以要在X变量相对应的边上加上权值,然后求最小费用最大流

    #include<iostream>
    #include<algorithm>
    #include<cstring>
    #include<cstdio>
    #include<vector>
    #include<stack>
    #include<queue>
    #include<cmath>
    #include<map>
    #include<set>
    using namespace std;
    typedef long long ll;
    typedef pair<int,int> pll;
    const int INF = 0x3f3f3f3f;
    const int maxn = 1000 + 5;
    
    int n,m;
    
    struct Edge
    {
        int from, to, cap, flow, cost;
        Edge(int u, int v, int c, int f, int w) :from(u), to(v), cap(c), flow(f), cost(w) {}
    };
    
    struct MCMF
    {
        int n, m;
        vector<Edge> edges;
        vector<int> G[maxn];
        int inq[maxn];
        int d[maxn];
        int p[maxn];
        int a[maxn];
    
        void init(int n)
        {
            this->n = n;
            for (int i = 0; i<n; i++) G[i].clear();
            edges.clear();
        }
    
        void AddEdge(int from, int to, int cap, int cost)
        {
            edges.push_back(Edge(from, to, cap, 0, cost));
            edges.push_back(Edge(to, from, 0, 0, -cost));
            m = edges.size();
            G[from].push_back(m - 2);
            G[to].push_back(m - 1);
        }
    
        bool BellmanFord(int s, int t, int &flow, int & cost)
        {
            for (int i = 0; i<n; i++) d[i] = INF;
            memset(inq, 0, sizeof(inq));
            d[s] = 0; inq[s] = 1; p[s] = 0; a[s] = INF;
    
            queue<int> Q;
            Q.push(s);
            while (!Q.empty()){
                int u = Q.front(); Q.pop();
                inq[u] = 0;
                for (int i = 0; i<G[u].size(); i++){
                    Edge& e = edges[G[u][i]];
                    if (e.cap>e.flow && d[e.to]>d[u] + e.cost){
                        d[e.to] = d[u] + e.cost;
                        p[e.to] = G[u][i];
                        a[e.to] = min(a[u], e.cap - e.flow);
                        if (!inq[e.to]) { Q.push(e.to); inq[e.to] = 1; }
                    }
                }
            }
    
            if (d[t] == INF) return false;
            flow += a[t];
            cost += d[t] * a[t];
            for (int u = t; u != s; u = edges[p[u]].from)
            {
                edges[p[u]].flow += a[t];
                edges[p[u] ^ 1].flow -= a[t];
            }
            return true;
        }
    
        int MincostMaxdflow(int s, int t){
            int flow = 0, cost = 0;
            while (BellmanFord(s, t, flow, cost));
            return cost;
        }
    }t;
    
    int a[maxn];
    
    int main()
    {
        //freopen("in.txt","r",stdin);
        scanf("%d%d",&n,&m);
        int src = 0, dst = n+2;
        t.init(dst+1);
        for(int i=1;i<=n;i++)  scanf("%d",&a[i]);
        a[0] = a[n+1] = 0;
        for(int i=1;i<=n+1;i++)
        {
            int tmp = a[i] - a[i-1];
            if(tmp>0)  t.AddEdge(src,i,tmp,0);
            else t.AddEdge(i,dst,-tmp,0);
        }
        for(int i=1;i<=n;i++)  t.AddEdge(i+1,i,INF,0);
        for(int i=1;i<=m;i++)
        {
            int x,y,z;
            scanf("%d%d%d",&x,&y,&z);
            t.AddEdge(x,y+1,INF,z);
        }
        printf("%d
    ",t.MincostMaxdflow(src,dst));
        return 0;
    }
    

      

  • 相关阅读:
    Google调试技巧总结
    Reorder List -- leetcode
    Java回合阵列List
    他们控制的定义(2.3):SurfaceView和SurfaceHolder
    ImageView建立selector在录音中遇到的小问题及解决方案
    cocos2d 简单的日常高仿酷跑游戏
    Xcode的小标记旁边的文件的名称的作用
    c++中的对象引用(object reference)与对象指针的区别
    UIColor深入研究(CGColor,CIColor)
    UIImage图片处理
  • 原文地址:https://www.cnblogs.com/zyb993963526/p/7871956.html
Copyright © 2011-2022 走看看