zoukankan      html  css  js  c++  java
  • Videos

    Videos

    时间限制: 1 Sec  内存限制: 128 MB
    提交: 17  解决: 7
    [提交] [状态] [讨论版] [命题人:admin]

    题目描述

    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.

    输入

    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
     

    输出

    Your output should include T lines, for each line, output the maximum happiness for the corresponding datum.

    样例输入

    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
    

    样例输出

    2000
    1990

    思路:
      最小费用最大流,人尽可能被利用才能使结果最大化!
      建立负权边,因为要使总幸福值的绝对值最大!
      源点与次源点之间建一条流量为K,费用为0的边;
      所有影片自己的起点与终点建一条流量为1,费用为-w的边;
      次源点与所有影片的起点建一条流量为1,费用为0的边;
      所有影片的终点与汇点建一条流量为1,费用为0的边;
      所有影片自己的终点与其他所有满足继承条件的影片的起点建一条流量为1,费用为0或者W的边(类型相同则为W);
    AC代码:
    #include <bits/stdc++.h>
    using namespace std;
    const int maxn=1000;
    const int inf=0x3f3f3f3f;
    struct Edge
    {
        int u,v,cost,cap,flow,next;
    }e[maxn*maxn];
    int T,n,m,k,w;
    struct Mcmf
    {
        int head[maxn],cnt;
        int dis[maxn],f[maxn],pre[maxn];
        bool vis[maxn];
        inline void init()
        {
            memset(head,-1,sizeof(head));
            cnt=0;
        }
        inline void add(int u,int v,int cost,int cap,int flow=0)
        {
            e[cnt]=Edge{u,v,cost,cap,flow,head[u]};
            head[u]=cnt++;
            e[cnt]=Edge{v,u,-cost,0,flow,head[v]};
            head[v]=cnt++;
        }
        bool spfa(int s,int t,int &cost,int &flow)
        {
            memset(vis,false,sizeof(vis));
            for (int i=0; i<maxn; i++)
                dis[i]=inf;
            queue<int>q;
            f[s]=inf;
            dis[s]=0;
            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].v;
                    if(e[i].cap>e[i].flow && dis[v]>dis[u]+e[i].cost)
                    {
                        dis[v]=dis[u]+e[i].cost;
                        f[v]=min(f[u],e[i].cap-e[i].flow);
                        pre[v]=i;
                        if(!vis[v])
                        {
                            vis[v]=1;
                            q.push(v);
                        }
                    }
                }
            }
            if(dis[t]==inf) return 0;
            cost+=dis[t]*f[t];
            flow+=f[t];
            for(int u=t; u!=s; u=e[pre[u]].u)
            {
                e[pre[u]].flow+=f[t];
                e[pre[u]^1].flow-=f[t];
            }
            return 1;
        }
        int minCost(int s,int t)
        {
            int cost=0,flow=0;
            while(spfa(s,t,cost,flow)) ;
            return cost;
        }
    } p;
    struct record
    {
        int s,t,w,id;
    } mov[maxn];
    
    int main()
    {
        //scanf("%d",&T);
        for(scanf("%d",&T);T;T--)
        {
            p.init();
            scanf("%d %d %d %d",&n,&m,&k,&w);
            for (int i=1; i<=m; i++)
            {
                scanf("%d %d %d %d",&mov[i].s,&mov[i].t,&mov[i].w,&mov[i].id);
            }
            int s=0,t=2*m+2;
            p.add(0,2*m+1,0,k);
            for(int i=1; i<=m; i++)
            {
                p.add(m+m+1,i,0,1);
                p.add(i,m+i,-mov[i].w,1);
                p.add(m+i,t,0,1);
                for(int j=1; j<=m; j++)
                {
                    if(i==j) continue;
                    if(mov[i].t<=mov[j].s)
                    {
                        if(mov[i].id==mov[j].id) p.add(i+m,j,w,1);
                        else p.add(i+m,j,0,1);
                    }
                }
            }
            printf("%d
    ",-p.minCost(s,t));
        }
        return 0;
    }
    View Code

    不知道为什么s,t不能定义为全局变量!

     
  • 相关阅读:
    quartz定时器的使用
    中间件weblogic的下载,安装,集成eclipse,以及项目部署
    权限管理(RBAC)在项目中的具体应用
    项目后台管理之权限管理(RBAC)
    办公自动化技巧专题开坑!!
    真&#183;从零开始的Ubuntu+Apache+MySQL+PHP+phpstorm+xdebug下的debug环境搭建(纯小白向)
    个人收藏-未整理
    Excel VBA 连接各种数据库(三) VBA连接SQL Server数据库
    从零开始的 phpstorm+wamp 组合下的debug环境搭建(纯小白向)
    Excel VBA ——字典实用技巧
  • 原文地址:https://www.cnblogs.com/lglh/p/9524880.html
Copyright © 2011-2022 走看看