Problem L.Videos
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 163 Accepted Submission(s): 63
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.
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
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
思路:
拆点建边,
将一个点分别与 m 场电影建一条容量为1,费用为0的边;
起点与该店建一条容量为 k,费用为0的边;
自己与自己建一条容量为1,费用为 -w 的边;
与自身类型相同的点建一条容量为1,费用为 W 的边;
与自身不相同的点建一条容量为1,费用为0的边。
代码如下:
#include <bits/stdc++.h> using namespace std; const int maxn=1010; const int inf=0x3f3f3f3f; struct Edge{ int u,v,cost,cap,flow,next; }e[maxn*maxn]; int _,n,m,k,w; struct Mcmf{ int head[maxn],cnt; int dis[maxn],f[maxn],pre[maxn]; bool vis[maxn]; inline void init(){ memset(head,-1,sizeof(head)); cnt=0; } inline void add(int u,int v,int cost,int cap,int flow=0){ e[cnt]=Edge{u,v,cost,cap,flow,head[u]}; head[u]=cnt++; e[cnt]=Edge{v,u,-cost,0,flow,head[v]}; head[v]=cnt++; } bool spfa(int s,int t,int &cost,int &flow){ memset(vis,false,sizeof(vis)); for (int i=0; i<maxn; i++) dis[i]=inf; queue<int>q; f[s]=inf; dis[s]=0; q.push(s); while(!q.empty()){ int u=q.front();q.pop(); vis[u]=false; for (int i=head[u]; i!=-1; i=e[i].next){ int v=e[i].v; if(e[i].cap>e[i].flow && dis[v]>dis[u]+e[i].cost){ dis[v]=dis[u]+e[i].cost; f[v]=min(f[u],e[i].cap-e[i].flow); pre[v]=i; if(!vis[v]){ vis[v]=1; q.push(v); } } } } // cout<<dis[t]<<endl; if(dis[t]==inf) return 0; cost+=dis[t]*f[t]; flow+=f[t]; for (int u=t; u!=s; u=e[pre[u]].u){ e[pre[u]].flow+=f[t]; e[pre[u]^1].flow-=f[t]; } return 1; } int minCost(int s,int t){ int cost=0,flow=0; while(spfa(s,t,cost,flow)) ; return cost; } }p; struct node{ int s,t,w,id; }mov[maxn]; int main(){ for (scanf("%d",&_); _; _--){ p.init(); scanf("%d%d%d%d",&n,&m,&k,&w); for (int i=1; i<=m; i++) scanf("%d%d%d%d",&mov[i].s,&mov[i].t,&mov[i].w,&mov[i].id); int s=0,t=2*m+2; p.add(0,1+2*m,0,k); for (int i=1; i<=m; i++){ p.add(m+m+1,i,0,1); p.add(i,i+m,-mov[i].w,1); p.add(i+m,t,0,1); for (int j=1; j<=m; j++){ if(i==j) continue; if(mov[i].t<=mov[j].s){ if(mov[i].id==mov[j].id) p.add(i+m,j,w,1); else p.add(i+m,j,0,1); } } } printf("%d\n",-p.minCost(s,t)); } return 0; }