• HDU 6437 最(大) 小费用最大流


    Problem L.Videos

    Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)
    Total Submission(s): 455    Accepted Submission(s): 222


    Problem Description
    C-bacteria takes charge of two kinds of videos: ’The Collection of Silly Games’ and ’The Collection of Horrible Games’.
    For simplicity’s sake, they will be called as videoA and videoB.
    There are some people who want to watch videos during today, and they will be happy after watching videos of C-bacteria.
    There are n hours a day, m videos are going to be show, and the number of people is K.
    Every video has a type(videoA or videoB), a running time, and the degree of happi- ness after someone watching whole of it.
    People can watch videos continuous(If one video is running on 2pm to 3pm and another is 3pm to 5pm, people can watch both of them).
    But each video only allows one person for watching.
    For a single person, it’s better to watch two kinds to videos alternately, or he will lose W happiness.
    For example, if the order of video is ’videoA, videoB, videoA, videoB, …’ or ’B, A, B, A, B, …’, he won’t lose happiness; But if the order of video is ’A, B, B, B, A, B, A, A’, he will lose 3W happiness.
    Now you have to help people to maximization the sum of the degree of happiness.
     
    Input
    Multiple query.
    On the first line, there is a positive integer T, which describe the number of data. Next there are T groups of data.
    for each group, the first line have four positive integers n, m, K, W : n hours a day, m videos, K people, lose W happiness when watching same videos).
    and then, the next m line will describe m videos, four positive integers each line S, T, w, op : video is the begin at S and end at T, the happiness that people can get is w, and op describe it’s tpye(op=0 for videoA and op=1 for videoB).
    There is a blank line before each groups of data.
    T<=20, n<=200, m<=200, K<=200, W<=20, 1<=S<T<=n, W<=w<=1000,
    op=0 or op=1
     
    Output
    Your output should include T lines, for each line, output the maximum happiness for the corresponding datum.
     
    Sample Input
    2
    10 3 1 10
    1 5 1000 0
    5 10 1000 1
    3 9 10 0
    10 3 1 10
    1 5 1000 0
    5 10 1000 0
    3 9 10 0
    Sample Output
    2000
    1990

     解析   很容易看出来 这是一道费用流的题  首先数量级很小 然后很多个电影 每个电影只能看一遍   可以得到一个快乐值 (容量,费用)

       关键有k个人,按照一个人一个人来贪心,策略貌似不对,所以试一下网络流。

      建图    

      1 把每个电影拆成俩个点v,v' 有向边 v->v' 的边权为观看的该电影的快乐值的相反数  容量为1(只能观看一次) 

      2 然后源点s到次源点s'连一条有向边s->s' 权值为0  容量为 k 

      3  次源点s' 到每个电影的 v 点都连一条s'->v的有向边 权值为0  容量为1 

      4  每个电影的v'点到汇点 t 连一条有向边 v'-> t   权值为0 容量为1  

      5 电影与电影之间 根据开始 结束时间是否相交,判断是否可以跳转观看。若可以,连一条有向边 u'->v ,再判断两个电影类型是否相同,相同权值为w,不相同权值为0,容量为1

    然后跑一边最小费用最大流  答案就是费用取反 

    AC代码

    #include<bits/stdc++.h>
    using namespace std;
    #define pb push_back
    #define mp make_pair
    #define X first
    #define Y second
    #define all(a) (a).begin(), (a).end()
    #define fillchar(a, x) memset(a, x, sizeof(a))
    #define huan printf("
    ");
    #define debug(a,b) cout<<a<<" "<<b<<" ";
    const int maxn=1e3+20,mod=1e9+7,inf=0x3f3f3f3f;
    typedef long long ll;
    struct MCMF {
        struct Edge {
            int from, to, cap, cost;
            Edge(int u, int v, int w, int c): from(u), to(v), cap(w), cost(c) {}
        };
        int n, s, t;
        vector<Edge> edges;
        vector<int> G[maxn];
        int inq[maxn], d[maxn], p[maxn], 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, cost));
            edges.push_back(Edge(to, from, 0, -cost));
            int 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 && 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);
                        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]].cap -= a[t];
                edges[p[u] ^ 1].cap += a[t];
                u = edges[p[u]].from;
            }
            return true;
        }
        int solve(int s, int t) {
            int flow = 0, cost = 0;
            while (BellmanFord(s, t, flow, cost));
            return cost;
        }
    }solver;
    struct node
    {
        int l,r,w,type,id;
    }a[maxn];
    void build(int n,int m,int k,int w)
    {
        solver.init(2*m+2);
        for(int i=0;i<m;i++)
        {
            solver.addedge(a[i].id,a[i].id^1,1,-a[i].w);
            solver.addedge(2*m+1,a[i].id,1,0);
            solver.addedge(a[i].id^1,2*m+2,1,0);
        }
        for(int i=0;i<m;i++)
        {
            for(int j=i+1;j<m;j++)
            {
                if(a[i].r<=a[j].l)
                {
                    if(a[i].type==a[j].type)
                        solver.addedge(a[i].id^1,a[j].id,1,w);
                    else
                        solver.addedge(a[i].id^1,a[j].id,1,0);
                }
                else if(a[i].l>=a[j].r)
                {
                    if(a[i].type==a[j].type)
                        solver.addedge(a[j].id^1,a[i].id,1,w);
                    else
                        solver.addedge(a[j].id^1,a[i].id,1,0);
                }
            }
        }
        solver.addedge(2*m,2*m+1,k,0);
    }
    int main()
    {
        int n,m,t,w,k;
        scanf("%d",&t);
        while(t--)
        {
            scanf("%d%d%d%d",&n,&m,&k,&w);
            for(int i=0;i<m;i++)
            {
                scanf("%d%d%d%d",&a[i].l,&a[i].r,&a[i].w,&a[i].type);
                a[i].id=i*2;
            }
            build(n,m,k,w);
            int maxflow;    // 汇点 2m+2   源点 2m    次源点2m+1
            maxflow=solver.solve(2*m,2*m+2);
            printf("%d
    ",-maxflow);
        }
        return 0;
    }
  • 相关阅读:
    android 样式开发
    Android studio 开发环境搭建
    nodejs+express 4.x笔记
    C#: switch语句的重构『网摘』
    从实例谈OOP、工厂模式和重构
    ASP.NET FileUpload 上传文件类型验证
    asp.net 网页中播放 flash 和flv
    数据库设计中主键问题
    修改Sqlserver实例默认排序规则
    查看sqlserver被锁的表以及如何解锁
  • 原文地址:https://www.cnblogs.com/stranger-/p/9524802.html
Copyright © 2020-2023  润新知