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
Source
2018 Multi-University Training Contest 10
思路:最优解可以用到尽量多的人,考虑最大费用最大流,电影拆点建容量1,价值-w的边,电影之间根据时间冲突与否建边,价值根据op值设置,即s->k->m->m(复制)->t,然后跑最小费用最大流。
# include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn = 200000;
const int INF= 0x3f3f3f3f;
int cnt=0, Next[maxn], dis[maxn], vis[maxn], pre[maxn];
int source, sink;
struct node
{
int u, v, w, c, next;
node(){}
node(int u, int v, int w, int c, int next):u(u),v(v),w(w),c(c),next(next){}
}edge[maxn];
struct mov{
int l, r, w, op;
}mov[300];
void add(int u, int v, int w, int c)
{
edge[cnt] = node(u,v,w,c,Next[u]);
Next[u] = cnt++;
edge[cnt] = node(v,u,0,-c,Next[v]);
Next[v] = cnt++;
}
bool spfa()
{
memset(vis, 0, sizeof(vis));
memset(dis, INF, sizeof(dis));
int l=0, r=0;
dis[source] = 0;
vis[source] = 1;
queue<int>q;
q.push(source);
while(!q.empty())
{
int u=q.front();
q.pop();
vis[u] = 0;
for(int i=Next[u]; i!=-1; i=edge[i].next)
{
int v=edge[i].v, w=edge[i].w, c=edge[i].c;
if(w>0 && dis[v] > dis[u]+c)
{
dis[v] = dis[u] + c;
pre[v] = i;
if(!vis[v])
{
q.push(v);
vis[v] = 1;
}
}
}
}
return dis[sink] != INF;
}
int mcmf()
{
int full = 0;
int cost = 0;
while(spfa())
{
int imin = INF;
for(int i=sink; i!=source; i=edge[pre[i]].u) imin = min(imin, edge[pre[i]].w);
for(int i=sink; i!=source; i=edge[pre[i]].u)
{
edge[pre[i]].w -= imin;
edge[pre[i]^1].w += imin;
}
cost += imin*dis[sink];
full += imin;
}
return cost;
}
int main(){
int T, n, m, k, w;
for(scanf("%d",&T); T; --T){
cnt = 0;
memset(Next, -1, sizeof(Next));
scanf("%d%d%d%d",&n,&m,&k,&w);
for(int i=1; i<=m; ++i){
scanf("%d%d%d%d",&mov[i].l, &mov[i].r, &mov[i].w, &mov[i].op);
}
source = 0, sink = 2*m+k+1;
for(int i=1; i<=m; ++i){
add(i, i+m, 1, -mov[i].w);
add(i+m, sink, 1, 0);
for(int j=1; j<=m; ++j){
if(mov[i].r <= mov[j].l){
if(mov[i].op == mov[j].op) add(i+m, j, 1, w);
else add(i+m, j, 1, 0);
}
}
}
for(int i=1; i<=k; ++i){
add(source, i+2*m, 1, 0);
for(int j=1; j<=m; ++j){
add(i+2*m, j, 1, 0);
}
}
printf("%d\n",-mcmf());
}
return 0;
}