原题面
题目描述
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.
输入
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
输出
Your output should include T lines, for each line, output the maximum happiness for the corresponding datum.
翻译
有nnn个小时,mmm个电影(就当作是电影了…),kkk个人,每个电影有一种类型,连续看相同类型的节目会扣WWW快乐值。
电影播放的区间是(l,r)(l,r)(l,r)。一个人同时间只能看一个节目,看完可以获得快乐值www。问最多可以获得多少快乐?
题解
考试的时候莫名其妙就猜出是道费用流的题
这道题实际上非常容易想成DP。。。但经过dalao的提醒我们发现其实是一个与费用流相关的题。
接下来就是建模的问题了。
最开始,我们组的想法是将所有人放在X部
,所有电影设为Y部
。
1.源点与人全部相连,费用为0
.
2.人与电影两两相连,费用为看电影iii的花费wiw_iwi?。
3.如果电影XXX与电影YYY之间不相互影响,则在XXX,YYY之间连一条边。注意若两个电影种类相同,费用为wY?Ww_Y-WwY??W。否则,费用为wYw_YwY?。
4.将电影与汇点全部相连,费用为0
。
5.所有边的容量都为1
。
如下图所示:
然而这样连有一个问题。
虽然看上去再最后到汇点时已经限制了人数。但是当还是无法防止有多个人看一个电影,例如上图中人可以通过红色的边或者黑色的边到达同一个电影。
经dalao的提示,我们需要拆点。
我们可以将电影拆成两个点(下图中是一个橙色的点,一个绿色[当是绿色吧…]的点),两点之间连一个容量为1
费用为0
的边。
还有在电影XXX与电影YYY之间不相互影响的情况下,则在XXX的绿色的点,YYY的橙色的点之间连一条边。费用同前。
如下图所示:
对了…还有要注意千万不能这样连…
代码
队友写的…并不是我写的…
注意跑的是最大费用最大流。
#include<cstdio>
#include<cstring>
#include<queue>
#include<iostream>
#include<algorithm>
using namespace std;
const int N = 50002;
const int M = 500005;
#define INF 0x3f3f3f3f
struct E
{
int to,cap,cost,flow,next;
}e[2*M];int head[N] , ecnt;
int pre[N];
int dis[N];
bool vis[N];
int n,m,S,T;void Clear()
{
ecnt = 0;memset(head,-1,sizeof head);
}void adde(int fr,int to,int cap,int cost)
{
e[ecnt]=(E){
to,cap,-cost,0,head[fr]};head[fr] = ecnt++;e[ecnt]=(E){
fr,0,cost,0,head[to]};head[to] = ecnt++;
}bool SPFA(int s,int t)
{
memset(vis,0,sizeof vis);memset(dis,0x3f,sizeof dis);memset(pre,-1,sizeof pre);queue <int> q;q.push(s);dis[s] = 0;vis[s]=1;while (!q.empty()){
int cur = q.front();q.pop();vis[cur] = false;for (int j=head[cur];j!=-1;j=e[j].next){
int to = e[j].to;if (dis[to] > dis[cur] + e[j].cost && e[j].cap > e[j].flow ){
dis[to] = dis[cur] + e[j].cost;pre[to] = j;if (!vis[to]){
q.push(to);vis[to] = true;}}}}return pre[t] != -1;
}void MCMF (int s,int t,int &maxflow,int &mincost)
{
maxflow = mincost = 0;while (SPFA(s,t)){
int MIN = INF;for (int j=pre[t]; j!=-1;j=pre[e[j^1].to]){
MIN = min(MIN,e[j].cap - e[j].flow);}for (int j=pre[t]; j!=-1;j=pre[e[j^1].to]){
e[j].flow += MIN;e[j^1].flow -= MIN;mincost += MIN * e[j].cost;}maxflow += MIN;}
}#define MAXN 3000int L[MAXN+5],R[MAXN+5],Lk[MAXN+5],Kd[MAXN+5];
int main(){
int TT;cin>>TT;while(TT--){
Clear();int nn,mm,kk,ht;cin>>nn>>mm>>kk>>ht;for(int i=1;i<=mm;i++)cin>>L[i]>>R[i]>>Lk[i]>>Kd[i];S=1,T=kk+2*mm+2;for(int i=1;i<=kk;i++)adde(S,i+1,1,0);for(int i=1;i<=kk;i++)for(int j=1;j<=mm;j++){
adde(i+1,j+kk+1,1,Lk[j]);}for(int i=1;i<=mm;i++)adde(i+kk+1,i+kk+mm+1,1,0);for(int i=1;i<=mm;i++)adde(i+kk+mm+1,T,1,0);for(int i=1;i<=mm;i++)for(int j=1;j<=mm;j++)if(i!=j&&R[i]<=L[j]){
if(Kd[i]!=Kd[j])adde(i+kk+mm+1,j+kk+1,1,Lk[j]);elseadde(i+kk+mm+1,j+kk+1,1,Lk[j]-ht);}int ans1,ans2;MCMF(S,T,ans1,ans2);cout<<-ans2<<"\n";}
}