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
Source
2018 Multi-University Training Contest 10
思路:老干爹刚输了决赛,很难受,不想多说,这题就是最小费用最大流,上代码。
代码:
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+10;
const int inf=0x3f3f3f3f;
int N;
struct edge{
int to,next,cap,flow,cost;
}e[maxn];
int head[maxn];
int tol;
int pre[maxn];
int dis[maxn];
bool vis[maxn];
struct nod{
int l;
int r;
int w;
int op;
}v[maxn];
void init(int n){
N=n;
tol=0;
memset(head,-1,sizeof(head));
}
void add(int u,int v,int cap,int cost){
e[tol].to=v;
e[tol].cap=cap;
e[tol].cost=cost;
e[tol].flow=0;
e[tol].next=head[u];
head[u]=tol++;
e[tol].to=u;
e[tol].cap=0;
e[tol].cost=-cost;
e[tol].flow=0;
e[tol].next=head[v];
head[v]=tol++;
}
bool spfa(int s,int t){
queue<int>q;
for(int i=0;i<N;i++){
dis[i]=inf;
vis[i]=false;
pre[i]=-1;
}
dis[s]=0;
vis[s]=true;
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].to;
if(e[i].cap>e[i].flow&&dis[v]>dis[u]+e[i].cost){
dis[v]=dis[u]+e[i].cost;
pre[v]=i;
if(!vis[v]){
vis[v]=true;
q.push(v);
}
}
}
}
if(pre[t]==-1) return false;
else return true;
}
int mincostmaxflow(int s,int t,int &cost){
int flow=0;
cost =0;
while(spfa(s,t)){
int mi=inf;
for(int i=pre[t];i!=-1;i=pre[e[i^1].to]){
if(mi>e[i].cap-e[i].flow) mi=e[i].cap-e[i].flow;
}
for(int i=pre[t];i!=-1;i=pre[e[i^1].to]){
e[i].flow+=mi;
e[i^1].flow-=mi;
cost+=e[i].cost*mi;
}
flow+=mi;
}
return flow;
}
int main(){
int t;
scanf("%d",&t);
while(t--){
int n,m,k,w;
scanf("%d%d%d%d",&n,&m,&k,&w);
for(int i=1;i<=m;i++) scanf("%d%d%d%d",&v[i].l,&v[i].r,&v[i].w,&v[i].op);
init((m<<1)+10);///初始
int S=(m<<1)+1;
int T=(m<<1)+2;///超级源点和汇点
int st=0;
add(S,st,k,0);
for(int i=1;i<=m;i++){
add(st,i,1,0);
add(i+m,T,1,0);
add(i,i+m,1,-v[i].w);
for(int j=1;j<=m;j++){
if(i==j) continue;
if(v[i].r<=v[j].l){
add(i+m,j,1,v[i].op!=v[j].op?0:w);
}
}
}
int ans=0;
mincostmaxflow(S,T,ans);
printf("%d\n",-ans);
}
}