zoukankan      html  css  js  c++  java
  • hdu 6437 Problem L.Videos 最大费用最大流 dinic

    Problem L.Videos

    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
     
     

    Solution

    最大费用最大流板题呀简直(事后XXX),

    嗯就这样拆点建图,注意中间v1’到v2的那条边需要考虑减w的情况,就是题目提到的相同类型减去w,不同类型不用减

    次汇点到超级汇点控制访问总人数

      1 /*
      2     最大费用最大流
      3     拆点建图
      4  */
      5 #include <bits/stdc++.h>
      6 #define lson rt << 1, l, mid
      7 #define rson rt << 1 | 1, mid + 1, r
      8 using namespace std;
      9 using ll = long long;
     10 using ull = unsigned long long;
     11 using pa = pair<int, int>;
     12 using ld = long double;
     13 const int maxV = 1e4 + 10;
     14 const int maxE = 5e4 + 10;
     15 const int inf = 0x3f3f3f3f;
     16 template <class T>
     17 inline T read(T &ret)
     18 {
     19     int f = 1;
     20     ret = 0;
     21     char ch = getchar();
     22     while (!isdigit(ch))
     23     {
     24         if (ch == '-')
     25             f = -1;
     26         ch = getchar();
     27     }
     28     while (isdigit(ch))
     29     {
     30         ret = (ret << 1) + (ret << 3) + ch - '0';
     31         ch = getchar();
     32     }
     33     ret *= f;
     34     return ret;
     35 }
     36 template <class T>
     37 inline void write(T n)
     38 {
     39     if (n < 0)
     40     {
     41         putchar('-');
     42         n = -n;
     43     }
     44     if (n >= 10)
     45     {
     46         write(n / 10);
     47     }
     48     putchar(n % 10 + '0');
     49 }
     50 struct node
     51 {
     52     int s, t, c, op, idx;
     53     bool operator<(const node &x) const
     54     {
     55         return t < x.t;
     56     }
     57 } v[maxV];
     58 
     59 int n, m, s, t; //点 边 源点 汇点
     60 int cnt, maxflow, mincost;
     61 struct edge
     62 {
     63     int v, nxt, w, c; //终点,下一边序号,残余容量,单位流量费用
     64 } edg[maxE * 2];
     65 int head[maxV], cur[maxV]; //第一条边序号,下一次访问的边序号(用于当前弧优化)
     66 int dis[maxV];             //分层图,类似最大流的dep数组
     67 int inq[maxV];             //spfa记录
     68 void init()
     69 {
     70     cnt = 0;
     71     memset(head, -1, sizeof head);
     72     memset(inq, 0, sizeof inq);
     73     maxflow = mincost = 0;
     74 }
     75 void add(int u, int v, int w, int c)
     76 {
     77     edg[cnt].v = v;
     78     edg[cnt].w = w;
     79     edg[cnt].c = c;
     80     edg[cnt].nxt = head[u];
     81     head[u] = cnt++;
     82 }
     83 bool spfa()
     84 {
     85     queue<int> que;
     86     while (!que.empty())
     87         que.pop();
     88     que.push(s);
     89     memset(dis, 0x3f, sizeof dis); //注意初始化,防止tle
     90     dis[s] = 0;
     91     while (!que.empty())
     92     {
     93         int u = que.front();
     94         que.pop();
     95         inq[u] = 0;
     96         for (int i = head[u]; ~i; i = edg[i].nxt)
     97         {
     98             int v = edg[i].v;
     99             if (edg[i].w && dis[u] + edg[i].c < dis[v]) //还有残余容量且费用可以更新
    100             {
    101                 dis[v] = dis[u] + edg[i].c;
    102                 if (!inq[v])
    103                 {
    104                     que.push(v);
    105                     inq[v] = 1;
    106                 }
    107             }
    108         }
    109     }
    110     return dis[t] != inf;
    111 }
    112 int dfs(int u, int flow)
    113 {
    114     if (u == t) //到达汇点
    115     {
    116         maxflow += flow;
    117         return flow;
    118     }
    119     int res = 0;
    120     inq[u] = 1;
    121     for (int i = cur[u]; ~i; i = edg[i].nxt)
    122     {
    123         int v = edg[i].v;
    124         if (!inq[v] && edg[i].w && dis[v] == dis[u] + edg[i].c) //没在队列,还有残余容量,满足费用分层图
    125         {
    126             cur[u] = i; //当前弧优化
    127             int fl = dfs(v, min(flow - res, edg[i].w));
    128             res += fl;
    129             edg[i ^ 1].w += fl;
    130             edg[i].w -= fl;
    131             mincost += fl * edg[i].c;
    132             if (res == flow)
    133                 break;
    134         }
    135     }
    136     inq[u] = 0;
    137     return res;
    138 }
    139 void dinic()
    140 {
    141     while (spfa())
    142     {
    143         memcpy(cur, head, sizeof head);
    144         dfs(s, inf);
    145     }
    146 }
    147 inline int left(int x)
    148 {
    149     return x;
    150 }
    151 inline int right(int x)
    152 {
    153     return x + m;
    154 }
    155 void debug()
    156 {
    157     for (int i = 1; i <= 2 * m + 3; i++)
    158     {
    159         cout << i << ": ";
    160         for (int j = head[i]; ~j; j = edg[j].nxt)
    161         {
    162             cout << edg[j].v << " " << edg[j].w << " " << edg[j].c << "; ";
    163         }
    164         cout << "
    ";
    165     }
    166 }
    167 int main(int argc, char const *argv[])
    168 {
    169 #ifndef ONLINE_JUDGE
    170     freopen("in.txt", "r", stdin);
    171     freopen("out.txt", "w", stdout);
    172 #endif
    173     int z;
    174     read(z);
    175     while (z--)
    176     {
    177         init();
    178         int k, w;
    179         read(n), read(m), read(k), read(w);
    180         s = 2 * m + 1;
    181         int pt = 2 * m + 2;
    182         t = 2 * m + 3;
    183         for (int i = 1; i <= m; i++)
    184         {
    185             read(v[i].s), read(v[i].t), read(v[i].c), read(v[i].op);
    186             v[i].idx = i;
    187             add(s, left(i), inf, 0);
    188             add(left(i), s, 0, 0);
    189             add(left(i), right(i), 1, 0);
    190             add(right(i), left(i), 0, 0);
    191             add(right(i), pt, 1, -v[i].c);
    192             add(pt, right(i), 0, v[i].c);
    193         }
    194         sort(v + 1, v + m + 1);
    195         for (int i = 1; i < m; i++)
    196             for (int j = i + 1; j <= m; j++)
    197             {
    198                 if (v[j].s >= v[i].t)
    199                 {
    200                     int f = v[i].op == v[j].op;
    201                     int p = v[i].idx, q = v[j].idx;
    202                     add(right(p), left(q), 1, f * w - v[i].c);
    203                     add(left(q), right(p), 0, -f * w + v[i].c);
    204                 }
    205             }
    206         add(pt, t, k, 0);
    207         add(t, pt, 0, 0);
    208         dinic();
    209         write(-mincost);
    210         puts("");
    211     }
    212     return 0;
    213 }
    View Code
  • 相关阅读:
    文件上传
    .NET格式化字符串详细说明
    外键
    VS IIS 注册 以及IIS浏览提示无权限访问
    CentOS 6.x 系统中安装原生 Hadoop 2
    Scrapy启动spider出错
    cmd进入pycharm所创建的虚拟环境
    基于mysql和Java Swing的简单课程设计
    python 内置函数
    java中的静态内部类
  • 原文地址:https://www.cnblogs.com/mooleetzi/p/11323281.html
Copyright © 2011-2022 走看看