HDU 6437 Videos(费用流)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_37943488/article/details/82020727

Problem L.Videos

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


 

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

题目大意:有k个人去看电影,只要电影时间段不重叠就可以一直看,每看一次电影可以得到电影的开心值,但是连续看同样的电影开心值就会减W,问最后得到的最大的开心值是多少

其实是一个很简单的费用流,然后几天没写费用流也没看模板自己写了一发.....改了半个小时才发现spfa的地方写错了....

建图也很容易,对于每个电影,拆点相连,连一条容量为1,费用为快乐值取负的边,然后对于所有的电影,如果能看的话,就连一条容量为1,费用要看电影类型的边,电影类型相同,费用就是那个要减去的快乐值,否则就是0,然后就是最重要的地方,一开始我设了一个源点,将它与每一个电影相连,但是发现这样样例过不了。后来想想也是,这样的话会把所有的边都跑了,所以需要两个源点,一个源点连着另一个源点,容量为k,费用为0,另一个源点连所有的电影,容量为1,费用为0,汇点就正常连,就行了

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int maxn=50010;
const int maxm=5000010;
const int inf=0x3f3f3f3f;
struct Node
{
    int to;
    int capa;
    int cost;
    int next;
}edge[maxm<<2];
struct LDJ
{
    int S;
    int T;
    int w;
    int op;
}ldj[maxm];
int cnt;
int head[maxn];
int dis[maxn];
int rec[maxn];
int pre[maxn];
bool vis[maxn];
int source,sink;
void init()
{
    memset(head,-1,sizeof(head));
    memset(rec,-1,sizeof(rec));
    memset(pre,-1,sizeof(pre));
    cnt=0;
    return;
}
void add(int u,int v,int capa,int cost)
{
    edge[cnt].to=v;
    edge[cnt].capa=capa;
    edge[cnt].cost=cost;
    edge[cnt].next=head[u];
    head[u]=cnt++;
    edge[cnt].to=u;
    edge[cnt].capa=0;
    edge[cnt].cost=-cost;
    edge[cnt].next=head[v];
    head[v]=cnt++;
    return;
}
bool spfa()
{
    queue<int> que;
    que.push(source);
    memset(dis,inf,sizeof(dis));
    memset(vis,false,sizeof(vis));
    dis[source]=0;
    vis[source]=true;
    while(!que.empty())
    {
        int node=que.front();
        que.pop();
        vis[node]=false;
        for(int i=head[node];~i;i=edge[i].next)
        {
            int v=edge[i].to;
            if(edge[i].capa>0&&dis[v]>dis[node]+edge[i].cost)
            {
                dis[v]=dis[node]+edge[i].cost;
                rec[v]=i;
                pre[v]=node;
                if(!vis[v])
                {
                    vis[v]=true;    
                    que.push(v);
                }
            }
        }
    }
    return dis[sink]!=inf;
}
void mcmf()
{
    int maxflow=0;
    int mincost=0;
    while(spfa())
    {
        int node=sink;
        int flow=inf;
        while(node!=source)
        {
            flow=min(flow,edge[rec[node]].capa);
            node=pre[node];
        }
        node=sink;
        while(node!=source)
        {
            mincost+=flow*edge[rec[node]].cost;
            edge[rec[node]].capa-=flow;
            edge[rec[node]^1].capa+=flow;
            node=pre[node];
        }
    }
    printf("%d\n",-mincost);
    return;
}
int main()
{
    int test;
    scanf("%d",&test);
    while(test--)
    {
        int maxx=-1;
        init();
        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",&ldj[i].S,&ldj[i].T,&ldj[i].w,&ldj[i].op);
            add(i,i+m,1,-ldj[i].w);
        }
        for(int i=1;i<=m;i++)
        {
            for(int j=1;j<=m;j++)
            {
                if(i==j) continue;
                if(ldj[j].S>=ldj[i].T)
                {
                    if(ldj[j].op==ldj[i].op)
                    {
                        add(i+m,j,1,W);
                    }       
                    else
                    {
                        add(i+m,j,1,0);
                    }
                }
            }
        }
        source=0;
        int tmp_source=m*2+1;
        sink=m*2+2;
        add(source,tmp_source,k,0);
        for(int i=1;i<=m;i++)
        {
            add(tmp_source,i,1,0);
        }
        for(int i=1;i<=m;i++)
        {
            add(m+i,sink,1,0);
        }
        mcmf();
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_37943488/article/details/82020727