HDU 6437 Problem L.Videos——费用流

版权声明:本人蒟蒻,如有大佬转发,感激不尽,带上我的博客链接即可。 https://blog.csdn.net/qq_36300700/article/details/81984723

借鉴了一位大佬的博客: https://blog.csdn.net/hao_zong_yin/article/details/81951354

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6437

题意:一天共有n小时,有m场电影,每个电影有开始时间l,结束时间r,看完这个电影将获得w权值,电影类型为0或1,现在有k个人去看电影,当一个人连着看了相同类型的电影获得W-w权值,问这k个人可以获得的最大权值和。

分析:

s到每个人容量为1,费用为0的边

每个人到每个视频建容量为1, 费用为0的边

每个视频拆点,建容量为1,费用为-w的边

视频之间若时间不覆盖,则容量为1的边,若两视频op相同,费用为W, 否则为0

每个视频到t建容量为1, 费用为0的边

跑一遍s-t最小费用流,答案取负即可

ac代码:

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <queue>
#include <map>
#include <vector>
#include <algorithm>
#include <stdlib.h>
#include <math.h>
using namespace std;
const int maxn =1000+10;
const int INF =0x3f3f3f3f;

struct video
{
    int s,t,w,op;
    bool operator < (const video & rhs) const{
        if(s==rhs.s)
            return t<rhs.t;
        return s<rhs.s;
    }
}v[maxn];

struct Edge
{
    int from,to,cap,flow,cost;
};

struct MCMF {
    int n, m, s, t;
    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];
        int u = t;
        while (u != s) {
            edges[p[u]].flow += a[t];
            edges[p[u]^1].flow -= a[t];
            u = edges[p[u]].from;
        }
        return true;
    }
    int mincost(int s, int t) {
        int flow = 0, cost = 0;
        while (BellmanFord(s, t, flow, cost));
        return cost;
    }
}mcmf;

int main()
{
    int T,n,m,K,W;
     scanf("%d",&T);
     while(T--)
     {
         int s,t;
        scanf("%d %d %d %d",&n,&m,&K,&W);

         mcmf.init(m*2+K+5);
         s=0,t=m*2+K+1;

        for(int i=0; i<m; i++)
          scanf("%d %d %d %d",&v[i].s,&v[i].t,&v[i].w,&v[i].op);

        sort(v,v+m);

        for(int i=1;i<=K; i++)///源点与人建一条容量为1,费用为0的边。
        {
            mcmf.addedge(s,i,1,0);

        }

        for(int i=1; i<=K; i++)///人与录像带建一条容量为1,费用为0的边。
            for(int j=0; j<m; j++)
        {
            mcmf.addedge(i,K+2*j+1,1,0);
        }

        for(int i=0; i<m; i++)///录像带与汇点建一条容量为1,费用为0的边。
        {
            mcmf.addedge(2*i+K+2,t,1,0);
        }

        for(int i=0; i<m; i++)///不同录像带之间建一条容量为1,费用为W的边。
        for(int j=i+1; j<m; j++)
        {
            if(v[i].t<=v[j].s)
            {
                if(v[i].op!=v[j].op)
                mcmf.addedge(K+2*i+2,K+2*j+1,1,0);
                else
                mcmf.addedge(K+2*i+2,K+2*j+1,1,W);
            }
        }

        for(int i=0; i<m; i++)///每一个录像带的两个点之间建一条容量为1,费用-w的边。
        {
            mcmf.addedge(K+2*i+1,K+2*i+2,1,-v[i].w);
        }

        printf("%d\n",-mcmf.mincost(s,t));


     }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_36300700/article/details/81984723
今日推荐