hdu6437 Problem L.Videos(网络流)

 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

题目大意:一天有N个小时,有m个节目(每种节目都有类型),有k个人,连续看相同类型的节目会扣w快乐值

每一种节目有都一个播放区间[l,r]。每个人同一时间只能看一个节目,看完可以获得快乐值。问最多可以获得多少快乐?

我们拆点 用于限制两个相连节目的费用

然后次源点用于限制k个人

#include<bits/stdc++.h>
#define FIN freopen("input.txt","r",stdin)
#define ll long long
#define mod 1000000007
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define inf 0x3f3f3f3f
#define INF 0x3f3f3f3f3f3f3f3f
const int maxn = 100005;
using namespace std;
int head[maxn],Next[maxn*200],to[maxn*200];
int flow[maxn*200],dis[maxn],pre[maxn],vis[maxn],id[maxn],ct[maxn];
int cnt,s,t,n,m,k;
inline void add(int u,int v,int w,int cost){
    Next[cnt]=head[u];
    head[u]=cnt;
    to[cnt]=v;
    flow[cnt]=w;
    ct[cnt++]=cost;

    Next[cnt]=head[v];
    head[v]=cnt;
    to[cnt]=u;
    flow[cnt]=0;
    ct[cnt++]=-cost;
}
bool spfa(){
    memset(vis,0,sizeof(vis));
    memset(dis,inf,sizeof(dis));
    memset(pre,-1,sizeof(pre));
    dis[s]=0;
    queue<int> q;
    q.push(s);
    while(!q.empty()){
        int u=q.front();
        q.pop();
        vis[u]=0;
        for(int i=head[u];i!=-1;i=Next[i]){
            int v=to[i];
            int d=ct[i];
            if(dis[v]>dis[u]+d&&flow[i]>0){
                dis[v]=dis[u]+d;    
                pre[v]=u;
                id[v]=i;
                if(!vis[v]){
                    vis[v]=1;
                    q.push(v);
                }
            }
        }
    }
   // cout<<dis[t]<<endl;
    return dis[t]<inf;
}
int Maxflow(){
    int ans=0;
    while(spfa()){ 
        int Min=inf;
        for(int i=t;i!=s;i=pre[i]){
            Min=min(flow[id[i]],Min);
        }
        for(int i=t;i!=s;i=pre[i]){
            flow[id[i]]-=Min;
            flow[id[i]^1]+=Min;
        }
        ans+=dis[t]*Min;
    }
    return ans;
}
void init(){
    memset(head,-1,sizeof(head));
    cnt=0;
}
struct node{
    int l,r,op,val;
}p[205];
int main(){
    int T,w;
    scanf("%d",&T);
    while(T--){
        init();
        scanf("%d %d %d %d",&n,&m,&k,&w);
        s=0;
        int ss=m+m+1;
        t=m+m+2;
        for(int i=1;i<=m;i++){
            scanf("%d %d %d %d",&p[i].l,&p[i].r,&p[i].val,&p[i].op);
        }
        for(int i=1;i<=m;i++){
            for(int j=1;j<=m;j++){
                if(i==j) continue;
                if(p[j].l>=p[i].r){
                    add(i+m,j,1,p[i].op==p[j].op?w:0);
                   // cout<<i+m<<" "<<j<<" "<<(p[i].op==p[j].op?-w:0)<<endl;
                }
            }
        }
        add(s,ss,k,0);
       /* cout<<s<<" "<<ss<<" 0
";*/
        for(int i=1;i<=m;i++){
            add(ss,i,1,0);
            add(i+m,t,1,0);
            add(i,i+m,1,-p[i].val);
           /* cout<<ss<<" "<<i<<" 0
";
            cout<<i+m<<" "<<t<<" 0
";
            cout<<i<<" "<<i+m<<" "<<-p[i].val<<endl;*/
        }
        printf("%d
",-Maxflow());
    }
    return 0;
}
View Code
原文地址:https://www.cnblogs.com/MengX/p/11370478.html