hdu6437 Videos 费用流

Posted mountaink

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hdu6437 Videos 费用流相关的知识,希望对你有一定的参考价值。

题目传送门

题目大意:

   给出n,每天有n个小时。有m种电影,每个电影有开始时间和结束时间,和01两种种类,k个人,每一部电影只能被一个人看,会获得一个快乐值wi,如果一个人连续看两部相同种类的电影,快乐值会消耗W,(先加上wi,再减去W)。如果两部电影的开始时间和结束时间是重合的,则可以连续看。题目保证所有的wi都大于等于W。

思路:

   拆点,将每一部电影拆成 i 和 i+m   两个点,建立一条费用为 -wi,流量为1的边,如果两部电影可以连着看,则建边,同种类的费用为W,不同种类的费用为0,建立一个源点连向所有电影,一个汇点也连接所有电影,一个超级源点,流量为k,连着源点。然后跑最小费用最大流,答案取负就可以了。  

   为什么这样就可以呢,因为题目保证了所有wi都大于W,也就是说,一部电影带来的收益肯定是正的,如果这个收益取负,那么我们就要最小的收益,而尽可能多的人就可以看尽可能多的电影,所以流量就要尽量大,这样就满足费用流的性质,然后就是建边稍微注意下,模板套一套就可以了。

  

#include<bits/stdc++.h>
#define CLR(a,b) memset(a,b,sizeof(a))
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f3f;
const int maxn=410;
struct edge{
    int v,f,w,Next;
    edge(){}
    edge(int a,int b,int c,int d){v = a,f=b,w=c,Next=d;}
}e[maxn*maxn];
int pree[maxn*2],prevv[maxn*2];
struct node{
    int beg,en,val,op;
}video[maxn];
int tot=1,head[maxn*2],src,sink,dist[maxn*2];
bool inque[maxn*2];
inline void addv(int u,int v,int c,int w){
    e[++tot]={v,c,w,head[u]};
    head[u]=tot;
    e[++tot]={u,0,-w,head[v]};
    head[v]=tot;;
}
inline void init(){
    CLR(pree,0);
    CLR(prevv,0);
    tot=1,CLR(head,0);
}
int n,m,k,W;
inline bool findpath(){
    queue<int >q;
    q.push(src);
    //printf("src:%d
",src);
    CLR(dist,inf);
    CLR(inque,0);
    dist[src]=0;
    inque[src]=1;
    while(!q.empty())
    {
        int u=q.front();
        q.pop();
        inque[u]=0;
        for(int i=head[u]; i ;i=e[i].Next)
        {
            if(e[i].f >0 && dist[u]+e[i].w < dist[e[i].v]){
                dist[e[i].v]=dist[u]+e[i].w;
                prevv[e[i].v]=u;
                pree[e[i].v]=i;
                if(!inque[e[i].v]){
                    inque[e[i].v]=1;
                    q.push(e[i].v);
                }
            }

        }
       // printf("%d
",u);
    }
    if(dist[sink]<inf)return true;
    return false;
}
inline int augment(){
    int u=sink;
    int delta = inf ;
    while(u!=src)
    {
       // printf("%d
",u);
        if(e[pree[u]].f<delta)delta = e[pree[u]].f;
        u = prevv[u];
    }
    u= sink;
    while(u!=src){
        e[pree[u]].f -= delta;
        e[pree[u] ^ 1].f +=delta;
        u = prevv[u];
    }
    return dist[sink]*delta;
}

inline int mincostflow(){
    int cur=0,ans=0;
    while(findpath()){


        cur += augment();
        if(cur<ans)ans=cur;
    }
    return ans;
}
int main(){
    int T;
    cin>>T;
    while(T--)
    {
        init();

        scanf("%d%d%d%d",&n,&m,&k,&W);
        src=2*m+1,sink=2*m+3;
        for(int i=1;i<=m;i++)
        {
            scanf("%d%d%d%d",&video[i].beg,&video[i].en,&video[i].val,&video[i].op);
        }
        for(int i=1;i<=m;i++){
            addv(i,i+m,1,-video[i].val);
            addv(src,i,1,0);
            addv(i+m,sink,1,0);
            for(int j=1;j<=m;j++)
            {
                if(i==j)continue;
                if(video[i].en<=video[j].beg){
                    if(video[i].op!=video[j].op)
                        {
                            addv(i+m,j,1,0);
                        }
                    else
                        {
                            addv(i+m,j,1,W);
                        }

                }
            }
        }
        src++;
        addv(src,src-1,k,0);
        printf("%d
",-mincostflow());

    }
}

Problem L.Videos

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 957    Accepted Submission(s): 474


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



















以上是关于hdu6437 Videos 费用流的主要内容,如果未能解决你的问题,请参考以下文章

hdu 6437 /// 最小费用最大流 负花费 SPFA模板

杂题总汇HDU多校赛第十场 Videos

LA 6437 Power Plant (prim最小生成树)

loj6437 PKUSC2018 PKUSC 计算几何

MYSQL 按顺序排列

html embeding-的YouTube,videos.html