hdu6437 Problem L.Videos(网络流)

                                                           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

题目大意:一天有N个小时,有m个节目(每种节目都有类型),有k个人,连续看相同类型的节目会扣w快乐值

每一种节目有都一个播放区间[l,r]。每个人同一时间只能看一个节目,看完可以获得快乐值。问最多可以获得多少快乐?

以下是根据样例一建立的图

建立源点、次源点、汇点。

将每个区间视为一个点,连续经过相同类型的点需要减去w的快乐值。

  1 #include <cstdio>
  2 #include <cstring>
  3 #include <queue>
  4 #include <algorithm>
  5 using namespace std;
  6 
  7 ///费用流
  8 struct MCMF
  9 {
 10     static const int MAXN = 50100;
 11     static const int MAXM = 5001000;
 12     static const int INF = 0x7FFFFFFF;
 13     static const int INF0X3F = 0x3f3f3f3f;
 14     int n, m, first[MAXN], s, t, sign;
 15     int dist[MAXN], inq[MAXN], pre[MAXN], incf[MAXN];
 16     int max_flow, min_cost;
 17 
 18     struct Edge
 19     {
 20         int to, cap, cost, next;
 21     } edge[MAXM * 4];
 22 
 23     void init(int l, int r, int ss, int tt)
 24     {
 25         //for(int i = l; i <= r; i++ ){first[i] = -1;}
 26         memset(first, -1, sizeof(first));
 27         s = ss, t = tt, sign = 0;
 28         max_flow = min_cost = 0;
 29     }
 30 
 31     void add_edge(int u, int v, int cap, int cost)
 32     {
 33         edge[sign].to = v, edge[sign].cap = cap, edge[sign].cost = cost;
 34         edge[sign].next = first[u], first[u] = sign++;
 35         edge[sign].to = u, edge[sign].cap = 0, edge[sign].cost = -cost;
 36         edge[sign].next = first[v], first[v] = sign++;
 37     }
 38 
 39     bool spfa(int s, int t)
 40     {
 41         for(int i = 0; i < MAXN; i++ )
 42         {
 43             dist[i] = INF, inq[i] = 0;
 44         }
 45         queue<int>que;
 46         que.push(s), inq[s] = 1, dist[s] = 0;
 47         incf[s] = INF0X3F;
 48         while(!que.empty())
 49         {
 50             int now = que.front();
 51             que.pop();
 52             inq[now] = 0;
 53             for(int i = first[now]; ~i; i = edge[i].next)
 54             {
 55                 int to = edge[i].to, cap = edge[i].cap, cost = edge[i].cost;
 56                 if(cap > 0 && dist[to] > dist[now] + cost)
 57                 {
 58                     dist[to] = dist[now] + cost;
 59                     incf[to] = min(incf[now], cap);
 60                     pre[to] = i;
 61                     if(!inq[to])
 62                     {
 63                         que.push(to);
 64                         inq[to] = 1;
 65                     }
 66                 }
 67             }
 68         }
 69         return dist[t] != INF;
 70     }
 71     void update(int s, int t)
 72     {
 73         int x = t;
 74         while(x != s)
 75         {
 76             int pos = pre[x];
 77             edge[pos].cap -= incf[t];
 78             edge[pos ^ 1].cap += incf[t];
 79             x = edge[pos ^ 1].to;
 80         }
 81         max_flow += incf[t];
 82         min_cost += dist[t] * incf[t];
 83     }
 84     void minCostMaxFlow(int s, int t)
 85     {
 86         while(spfa(s, t))
 87         {
 88             update(s, t);
 89         }
 90     }
 91 } p;
 92 
 93 const int MAXM = 1500 + 7;
 94 struct Node
 95 {
 96     int l, r, w, tp;
 97 } arr[MAXM];
 98 
 99 void solve()
100 {
101     /// 0 源点
102     /// m+m+1 次源点
103     /// m+m+2 汇点
104     int n, m, K, W;
105     scanf("%d %d %d %d", &n, &m, &K, &W);
106     p.init(0, m+m+2 , 0, m+m+2);  ///初始化 l,r,开始点,结束点
107 
108     /*
109     * 建图
110     */
111     p.add_edge(0, m+m+1, K, 0);
112     for(int i = 1; i <= m; i++ )
113         scanf("%d%d%d%d", &arr[i].l, &arr[i].r, &arr[i].w, &arr[i].tp);
114     for(int i = 1; i <= m; i++ )
115     {
116         for(int j = 1; j <= m; j++ )
117         {
118             if(i == j)continue;
119             if(arr[i].r <= arr[j].l)
120             {
121                 p.add_edge(i + m, j, 1, (arr[i].tp == arr[j].tp ? W : 0));  ///i+m为i拆出来的点
122             }
123         }
124     }
125     for(int i = 1; i <= m; i++ )
126     {
127         p.add_edge(i, i + m, 1, -arr[i].w);
128     }
129     for(int i = 1; i <= m; i++ )
130     {
131         p.add_edge(m+m+1, i, 1, 0);
132     }
133     for(int i = 1; i <= m; i++ )
134     {
135         p.add_edge(i + m, m+m+2, 1, 0);
136     }
137     p.minCostMaxFlow(0, m+m+2);
138     printf("%d\n", -p.min_cost);
139 };
140 
141 int main()
142 {
143     int T;
144     scanf("%d", &T);
145     while(T--)
146     {
147         solve();
148     }
149     return 0;
150 }
View Code

 

 

 

posted @ 2018-08-22 18:32  xcantaloupe  阅读(864)  评论(0编辑  收藏  举报