1. 程式人生 > >HDU 6437 Problem L.Videos(費用流)

HDU 6437 Problem L.Videos(費用流)

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;
}