Problem L.Videos
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 460 Accepted Submission(s): 224
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场电影,每个电影有开始时间l,结束时间r,看完这个电影将获得w权值,电影类型为0或1,现在有k个人去看电影,当一个人连着看了相同类型的电影获得W-w权值,问这k个人可以获得的最大权值和
这题意真难描述
分析:本来想着用分组背包写,把k个人看成一组,每个电影只选k个人中的一个,但是 看相同类型电影会减w,这个条件把我搞死了 QAQ,不扯了,现在说正解费用流
由于n<=200,可以对每个时间都建点,每个时间i到下个时间i+1 ( 流量=INF,费用=0 )
假设现在没有相同电影-w的限制,应该怎么搞?对于[L,R]这个电影,可以连接一条[L,R]的边,(流量=1,费用=w)这样就可以限制每个电影只有一个人看,而且也满足了看这个电影的初始和结束时间的限制
现在加上限制应该怎么写? 可以再建两条时间链,一条表示看类型0电影的时间轴,一条表示看类型1电影的时间轴 ,这样通过当前电影的类型,就可以在两个时间轴之间切换,而且边也很好连;
还有一些细节问题,比如拆点限制流量
(费用流直接套的模板,好像有点丑,不过能用)
#include<bits/stdc++.h>
#define mem(a,b) memset(a,b,sizeof(a))
#define INF 0x3f3f3f3f
using namespace std;
const int N=1e3+200;
const int M=1e4+200;
struct node
{
int v,w,cost,next;
} e[M*2];
int first[N],dis[N],c[N],vis[N],pre[N];
int tot,mincost;
void add_edge(int u,int v,int w,int cost)
{
e[tot].v=v;
e[tot].w=w;
e[tot].cost=cost;
e[tot].next=first[u];
first[u]=tot++;
}
void add(int u,int v,int w,int cost)
{
add_edge(u,v,w,cost);
add_edge(v,u,0,-cost);
}
bool spfa(int s,int t,int n)
{
mem(dis,INF);
mem(c,0);
mem(vis,0);
mem(pre,-1);
queue<int>q;
q.push(s);
vis[s]=1;
dis[s]=0;
c[s]++;
while(!q.empty())
{
int u=q.front();
q.pop();
vis[u]=0;
for(int i=first[u]; ~i; i=e[i].next)
{
if(e[i].w>0&&dis[e[i].v]>dis[u]+e[i].cost)
{
dis[e[i].v]=dis[u]+e[i].cost;
pre[e[i].v]=i;
if(!vis[e[i].v])
{
vis[e[i].v]=1;
if(++c[e[i].v]>n)
{
return 0;
}
q.push(e[i].v);
}
}
}
}
return dis[t]!=INF;
}
void MCMF(int s,int t,int n)
{
while(spfa(s,t,n))
{
int d=INF;
for(int i=pre[t]; ~i; i=pre[e[i^1].v])
d=min(d,e[i].w);
for(int i=pre[t]; ~i; i=pre[e[i^1].v])
{
e[i].w-=d;
e[i^1].w+=d;
}
mincost+=dis[t]*d;
}
}
int main()
{
int t,n,m,k,w,l,r,v,id;
scanf("%d",&t);
while(t--)
{
scanf("%d%d%d%d",&n,&m,&k,&w);
mem(first,-1);
tot=mincost=0;
add(0,2*n+2*m+2,k,0);
add(2*n+2*m+2,1,k,0);
add(2*n+2*m+2,n+1,k,0);
for(int i=1; i<n; i++)
{
add(i,i+1,INF,0);
add(n+i,n+i+1,INF,0);
}
add(n,2*n+2*m+1,INF,0);
add(2*n,2*n+2*m+1,INF,0);
for(int i=1; i<=m; i++)
{
scanf("%d%d%d%d",&l,&r,&v,&id);
add(2*n+i,2*n+m+i,1,0);
if(id)
{
add(l,2*n+i,1,-v);
add(n+l,2*n+i,1,w-v);
add(2*n+m+i,n+r,1,0);
}
else
{
add(l,2*n+i,1,w-v);
add(n+l,2*n+i,1,-v);
add(2*n+m+i,r,1,0);
}
}
MCMF(0,2*n+2*m+1,2*n+2*m+3);
printf("%d\n",-mincost);
}
return 0;
}