2018 Multi-University Training Contest 10 Problem L.Videos

Problem L.Videos

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


 

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个人在n个小时内看m个视频,每个视频有开始和终止时间,一个人可以连续看视频当且仅当视频时间不交叉,有两种视频,当一个人每看一个种类和上一次相同的视频是失去W快乐值,没看一个视频获得w快乐值,每个视频只能被看一次,求所有人获得的快乐值之和的最大值
 

数据很小,第一时间想到费用流,所有源点向每个人建边。为了保证每个点的权值只被记一次,将视频点拆成两个点x1和x2,x1向x2建边,费用为w,x2向汇点建边,所有人向每个视频的x1建边,如果种类相同,费用为W,否则为0。每个视频点的x2向可以连续看的下个视频点的x1建边,所有边费用为1,所有费用取反。跑一次最小费用最大流即可。

#include<stdio.h>
#include<queue>
#include<string.h>
#include<algorithm>
using namespace std;
const int maxm = 500005;
const int INF = 1e9 + 7;
struct videos
{
    int a, b, w, op;
    bool operator<(const videos &r)const
    {
        if (a == b) return b < r.b;
        return a < r.a;
    }
}p[maxm];
struct node
{
    int u, v, flow, cost, next;
}edge[maxm];
int n, m, s, t, cnt, k;
int head[maxm], vis[maxm], dis[maxm], cur[maxm], pre[maxm];
void init()
{
    cnt = 0, s = 0, t = 2 * m*k + 1;
    memset(head, -1, sizeof(head));
}
void add(int u, int v, int flow, int cost)
{
    edge[cnt].u = u, edge[cnt].v = v;
    edge[cnt].flow = flow, edge[cnt].cost = cost;
    edge[cnt].next = head[u], head[u] = cnt++;
    edge[cnt].u = v, edge[cnt].v = u;
    edge[cnt].flow = 0, edge[cnt].cost = -cost;
    edge[cnt].next = head[v], head[v] = cnt++;
}
int bfs()
{
    queue<int>q;
    for (int i = 0;i <= t;i++) dis[i] = INF;
    for (int i = 0;i <= cnt;i++) pre[i] = -1;
    dis[s] = 0, q.push(s);
    while (!q.empty())
    {
        int u = q.front();q.pop();
        //printf("%d\n", u);
        for (int i = head[u];i != -1;i = edge[i].next)
        {
            int v = edge[i].v;
            if (dis[v] > dis[u] + edge[i].cost&&edge[i].flow)
            {
                dis[v] = dis[u] + edge[i].cost;
                pre[v] = i, q.push(v);
            }
        }
    }
    if (dis[t] == INF) return 0;
    return 1;
}
int MCMF()
{
    int ans = 0, minflow;
    while (bfs())
    {
        minflow = INF;
        for (int i = pre[t];i != -1;i = pre[edge[i].u])
            minflow = min(minflow, edge[i].flow);
        for (int i = pre[t];i != -1;i = pre[edge[i].u])
            edge[i].flow -= minflow, edge[i ^ 1].flow += minflow;
        ans += dis[t] * minflow;
    }
    return ans;
}
int main()
{
    int i, j, T, w;
    scanf("%d", &T);
    while (T--)
    {
        scanf("%d%d%d%d", &n, &m, &k, &w);
        init();
        for (i = 1;i <= m;i++)
            scanf("%d%d%d%d", &p[i].a, &p[i].b, &p[i].w, &p[i].op);
        for (i = 1;i <= k;i++)
            add(s, i, 1, 0);
        sort(p + 1, p + 1 + m);
        for (i = 1;i <= m;i++)
        {
            for (j = 1;j <= k;j++)
                add(j, k + i, 1, 0);
            add(k + i, k + m + i, 1, -p[i].w);
            add(k + m + i, t, 1, 0);
            for (j = i + 1;j <= m;j++)
            {
                if (j == i) continue;
                if (p[i].b <= p[j].a)
                    add(k + m + i, k + j, 1, p[i].op == p[j].op ? w : 0);
            }
        }
        printf("%d\n", -MCMF());
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值