hdu 6437 Problem L.Videos maximum cost maximum flow 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

The maximum cost maximum flow problem it is simply board (post-XXX),

 

Ah so built FIG split point, note that the intermediate v1 'v2 to the case where that edge to consider reduction of w, is the subject of the same type mentioned subtracting w, of different types without Save

Super sub Meeting Point Meeting Point to control the total number of visitors

 

  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 is  {
 52 is      int S, T, C, OP, IDX;
 53 is      BOOL  operator <( const Node & X) const 
54 is      {
 55          return T < XT;
 56 is      }
 57 is  } V [MAXV];
 58  
59  int n-, m, S, T; // point source-side sink 
60  int CNT, MaxFlow, minCost;
 61 is  struct edge
 62 is  {
 63 is      int V, NXT, W, C; // end, while the serial number, remaining capacity, the cost per unit flow rate 
64 } edg [maxE *2 ];
 65  int head [MAXV], CUR [MAXV]; // first edge number, the next access number sides (arc current for optimization) 
66  int DIS [MAXV];              // layered graph, similar to dep maximum flow array 
67  int INQ [MAXV];              // SPFA record 
68  void the init ()
 69  {
 70      CNT = 0 ;
 71 is      Memset (head, - . 1 , the sizeof head);
 72      Memset (INQ, 0 , the sizeof INQ) ;
 73 is      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]) // there is residual capacity and the cost can be updated 
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) //Not in the queue, as well as the remaining capacity, to meet the cost of hierarchical diagram 
125          {
 126              CUR [U] = I; // current arc optimization 
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 is              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 << "\n";
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

 

 

Guess you like

Origin www.cnblogs.com/mooleetzi/p/11323281.html