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个视频,k个人,一个人只能看一个视频,每个视频有开始时间和结束时间以及种类A和种类B,不能同时看两个视频。每个人看视频将得到一个快乐值val,如果交替看不同种类的视频如ABABA那么不会减小快乐值,否则看一次与之前重复的视频就减少W的快乐值。如AABBAAA则减少4次快乐值。即4W。问最大化的快乐值是多少。

MLS一开始就想到了用网络流建图。而且建的非常神奇。

首先有一个超级源点和超级汇点,连到起点k的容量,汇点到超级汇点也是如此。因为一个视频有开始时间和结束时间,为了表示时间对看视频先后顺序的限制,将一个视频拆成两个节点,即开始时间一个节点,结束时间一个节点,该边费用为获得的快乐值。容量为1,表示每个人能看一个视频。

接着判断两两视频间的关系,首先是视频 i 结束时间小于等于视频 j 的开始时间才能连一条有向边,接着判断两视频的种类,如果种类相同则边权为-W,否则为0。

最后直接跑一个最大费用最大流即可。即最小费用最大流的相反数,把所有边权取相反数,结果取相反数即可。

更新:超级源点和超级汇点只是为了限制人数K,如果没有K的限制,那么答案只在人数小于等于视频数量的时候是对的,否则视频数量多于人数的时候就会跑超人数。再者。超级源点超级汇点只用存在一个即可,都能限制人数,因此不必要同时存在。

#include<bits/stdc++.h>///最大费用最大流,将边权变成相反数即可
#define LL long long
#define M(a,b) memset(a,b,sizeof a)
#define pb(x) push_back(x)
using namespace std;
const int maxn=1e5+7;
const int INF=0x3f3f3f3f;
struct edge
{int to,next,cap,flow,cost;edge() {}edge(int a,int b,int c,int d,int f){to=a;cap=b;flow=c;cost=d;next=f;}
} mp[maxn<<1];
int pre[maxn],dis[maxn];
bool vis[maxn];
int head[maxn];
int n,m,k,w,cnt,N;
void init(int n)
{N=n;cnt=0;M(head,-1);
}
void addedge(int from,int to,int cap,int cost)
{mp[cnt]=edge(to,cap,0,cost,head[from]);head[from]=cnt++;mp[cnt]=edge(from,0,0,-cost,head[to]);head[to]=cnt++;
}
bool spfa(int s,int t)
{queue<int>q;for(int i = 0; i <=N; i++){dis[i] = INF;vis[i] = false;pre[i] = -1;}dis[s] = 0;vis[s] = true;q.push(s);while(!q.empty()){int u = q.front();q.pop();vis[u] = false;for(int i = head[u]; i != -1; i = mp[i].next){int v = mp[i].to;if(mp[i].cap > mp[i].flow && dis[v] > dis[u] + mp[i].cost ){dis[v] = dis[u] + mp[i].cost;pre[v] = i;if(!vis[v]){vis[v] = true;q.push(v);}}}}if(pre[t] == -1)return false;else return true;
}
int minCostMaxflow(int s,int t,int &cost)
{int flow = 0;cost = 0;while(spfa(s,t)){int Min = INF;for(int i = pre[t]; i != -1; i = pre[mp[i^1].to]){if(Min > mp[i].cap - mp[i].flow)Min = mp[i].cap - mp[i].flow;}for(int i = pre[t]; i != -1; i = pre[mp[i^1].to]){mp[i].flow += Min;mp[i^1].flow -= Min;cost += mp[i].cost * Min;}flow += Min;}return flow;
}
int main()
{int t;scanf("%d",&t);while(t--){scanf("%d%d%d%d",&n,&m,&k,&w);init(m<<1+4);int from[maxn],to[maxn],val,flag[maxn];int st=0,ed=(m<<1)+1,S=(m<<1)+2,T=(m<<1)+3;///因此每个视频被拆成两个节点,因此总结点数达到了m*2,再加上超级源点和超级汇点addedge(st,S,k,0);addedge(T,ed,k,0);for(int i=1; i<=m; i++){scanf("%d%d%d%d",&from[i],&to[i],&val,&flag[i]);addedge(S,i,1,0);addedge(i,i+m,1,-val);///因为有时间的限制,因此将一个视频拆成开始时间和结束时间addedge(i+m,T,1,0);for(int j=1; j<i; j++)///在输入的同时比较其他视频的开始时间,将结束时间小于开始时间的建一条有向边,容量为1,一个人只能看一个视频if(to[i]<=from[j])///再判断是否是同种视频,是同种则边权为负,否则为0addedge(i+m,j,1,flag[i]==flag[j]?w:0);else if(to[j]<=from[i])addedge(j+m,i,1,flag[i]==flag[j]?w:0);}int ans=0;minCostMaxflow(st,ed,ans);printf("%d\n",-ans);}
}

HDU-6437 Problem L.Videos(最大费用最大流拆点)相关推荐

  1. HDU 6343.Problem L. Graph Theory Homework-数学 (2018 Multi-University Training Contest 4 1012)

    6343.Problem L. Graph Theory Homework 官方题解: 一篇写的很好的博客: HDU 6343 - Problem L. Graph Theory Homework - ...

  2. LightOJ - 1071 Baker Vai(最大费用最大流+拆点)

    题目链接:点击查看 题目大意:给出一个n*m的矩阵,每个点都有一个权值,现在问要从点(1,1)到点(n,m),每次只能向下或向右走,到达点(n,m)后再回到点(1,1),这次每次只能向上或向左走,除了 ...

  3. 洛谷 - P3356 火星探险问题(最大费用最大流+拆点+路径打印)

    题目链接:点击查看 题目大意:给出一个n*m的矩阵,每个点都有一个数字: 0:平坦无障碍 1:障碍 2:石块 现在在点(1,1)处有k个探测车,他们都要去往点(n,m)处,探测车只能向下或向右行驶,现 ...

  4. BZOJ-2324 营救皮卡丘 最小费用可行流+拆下界+Floyd预处理

    准备一周多的期末,各种爆炸,回来后状态下滑巨快...调了一晚上+80%下午 2324: [ZJOI2011]营救皮卡丘 Time Limit: 10 Sec Memory Limit: 256 MB ...

  5. POJ - 3422 Kaka's Matrix Travels(网络流-最大费用最大流+拆点法)

    题目链接:点击查看 题目大意:K取方格数,是在一个N*N的矩形网格中,每个格子里都写着一个整数.可以从左上角到右下角安排K条路线,每一步只能往下或往右,沿途经过的格子中的整数会被取走.若多条路线重复经 ...

  6. [费用流]2018 Multi-University Contest 10 L.Videos

    原题面 题目描述 C-bacteria takes charge of two kinds of videos: 'The Collection of Silly Games' and 'The Co ...

  7. Going Home HDU - 1533 (最小费用最大流)

    题目链接:https://cn.vjudge.net/problem/HDU-1533 题意:给你n个房子n个人  使得所有人都有一座房子的最小花费 思路:把所有的人与房子建边,最后,源点与所有的人建 ...

  8. hdu 5687 Problem C 字典树

    传送门:hdu 5687 Problem C 中文题目就不做过多的解释 解题思路 定义一个结构体,里面有26个字母,就像下面这样: struct Node{int next[26];int sum;v ...

  9. HDU 2282 Chocolate (最小费用最大流)

    HDU  2282 Chocolate (最小费用最大流) #include <iostream> #include <cstdio> #include <queue&g ...

  10. HDU 1853 HDU 3488【有向环最小权值覆盖问题 】最小费用最大流

    HDU 1853 & HDU 3488[有向环最小权值覆盖问题 ]带权二分图匹配 KM算法 In the kingdom of Henryy, there are N (2 <= N & ...

最新文章

  1. oracle顺序读等待,Oracle Study之--Oracle等待事件(4)
  2. RSA加密算法详解以及RSA在laravel中的应用
  3. Windows 10 开发环境搭建
  4. 数据结构-编程实现一个单链表的测长
  5. 【文件格式问题】文件格式 Windows、Unix/Linux、Mac 导致的问题及处理(idea 或 notepad++ 档案格式转换方法)
  6. python如何输入和输出_输入和输出
  7. mint-ui之toast使用(messagebox,indicator同理)
  8. 广州的11个辖区_重庆前三季度GDP反超广州,这对两城到底意味着什么?
  9. 在Jenkins远程链接Linux系统,然后执行shell命令-亲测可用【转】
  10. MATLAB:简单GUI的设计流程
  11. 如何快速统计考勤(bushi)
  12. 台式计算机如何上无线网络,台式电脑如何实现无线上网
  13. Kubernetes(K8s)基本概念:Volume(存储卷)、Persistent Volume
  14. 阴阳师手游初始式神推荐
  15. 高效数据同步工具DataX的使用
  16. 计算机毕业设计Java“臻宝”书画竞拍系统(源码+系统+mysql数据库+lw文档)
  17. php 模拟登录qq空间,PHP模拟登录QQ空间的例子
  18. STM32基础(11)光敏传感
  19. 虚拟机如何使用共享文件夹传文件
  20. Transform 被废弃,ASM 如何适配?

热门文章

  1. C# CAD批量转换为图片
  2. 10.前端基础--CSS盒子浮动
  3. (超全)全面手动清理c盘的的步骤
  4. 共用计算机如何加密,局域网共享文件,教您局域网共享文件怎么加密
  5. vue项目引入阿里云图标的4种方式
  6. COMSOL中的基础概念
  7. 【SSH网上商城项目实战26】完成订单支付后的短信发送功能
  8. Vimium、CrxMouse配置信息
  9. 阿里巴巴“牛逼”了,申请“行政干预”区块链专利
  10. mac安装应用提示已损坏的解决方法