Problem L.Videos

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

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

Sample Output
2000
1990

解析   很容易看出来 这是一道费用流的题  首先数量级很小 然后很多个电影 每个电影只能看一遍   可以得到一个快乐值 (容量,费用)

   关键有k个人,按照一个人一个人来贪心,策略貌似不对,所以试一下网络流。

建图

  1 把每个电影拆成俩个点v,v' 有向边 v->v' 的边权为观看的该电影的快乐值的相反数  容量为1(只能观看一次)

  2 然后源点s到次源点s'连一条有向边s->s' 权值为0  容量为 k

  3  次源点s' 到每个电影的 v 点都连一条s'->v的有向边 权值为0  容量为1

  4  每个电影的v'点到汇点 t 连一条有向边 v'-> t   权值为0 容量为1

  5 电影与电影之间 根据开始 结束时间是否相交,判断是否可以跳转观看。若可以,连一条有向边 u'->v ,再判断两个电影类型是否相同,相同权值为w,不相同权值为0,容量为1

然后跑一边最小费用最大流  答案就是费用取反

AC代码

#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define X first
#define Y second
#define all(a) (a).begin(), (a).end()
#define fillchar(a, x) memset(a, x, sizeof(a))
#define huan printf("\n");
#define debug(a,b) cout<<a<<" "<<b<<" ";
const int maxn=1e3+,mod=1e9+,inf=0x3f3f3f3f;
typedef long long ll;
struct MCMF {
struct Edge {
int from, to, cap, cost;
Edge(int u, int v, int w, int c): from(u), to(v), cap(w), cost(c) {}
};
int n, s, t;
vector<Edge> edges;
vector<int> G[maxn];
int inq[maxn], d[maxn], p[maxn], a[maxn]; void init(int n) {
this->n = n;
for (int i = ; i <= n; i ++) G[i].clear();
edges.clear();
}
void addedge(int from, int to, int cap, int cost) {
edges.push_back(Edge(from, to, cap, cost));
edges.push_back(Edge(to, from, , -cost));
int m = edges.size();
G[from].push_back(m - );
G[to].push_back(m - );
}
bool BellmanFord(int s, int t, int &flow, int &cost) {
for (int i = ; i <= n; i ++) d[i] = inf;
memset(inq, , sizeof(inq));
d[s] = ; inq[s] = ; p[s] = ; a[s] = inf; queue<int> Q;
Q.push(s);
while (!Q.empty()) {
int u = Q.front(); Q.pop();
inq[u] = ;
for (int i = ; i < G[u].size(); i ++) {
Edge &e = edges[G[u][i]];
if (e.cap && d[e.to] > d[u] + e.cost) {
d[e.to] = d[u] + e.cost;
p[e.to] = G[u][i];
a[e.to] = min(a[u], e.cap);
if (!inq[e.to]) {
Q.push(e.to);
inq[e.to] = ;
}
}
}
}
if (d[t] == inf) return false;
flow += a[t];
cost += d[t] * a[t];
int u = t;
while (u != s) {
edges[p[u]].cap -= a[t];
edges[p[u] ^ ].cap += a[t];
u = edges[p[u]].from;
}
return true;
}
int solve(int s, int t) {
int flow = , cost = ;
while (BellmanFord(s, t, flow, cost));
return cost;
}
}solver;
struct node
{
int l,r,w,type,id;
}a[maxn];
void build(int n,int m,int k,int w)
{
solver.init(*m+);
for(int i=;i<m;i++)
{
solver.addedge(a[i].id,a[i].id^,,-a[i].w);
solver.addedge(*m+,a[i].id,,);
solver.addedge(a[i].id^,*m+,,);
}
for(int i=;i<m;i++)
{
for(int j=i+;j<m;j++)
{
if(a[i].r<=a[j].l)
{
if(a[i].type==a[j].type)
solver.addedge(a[i].id^,a[j].id,,w);
else
solver.addedge(a[i].id^,a[j].id,,);
}
else if(a[i].l>=a[j].r)
{
if(a[i].type==a[j].type)
solver.addedge(a[j].id^,a[i].id,,w);
else
solver.addedge(a[j].id^,a[i].id,,);
}
}
}
solver.addedge(*m,*m+,k,);
}
int main()
{
int n,m,t,w,k;
scanf("%d",&t);
while(t--)
{
scanf("%d%d%d%d",&n,&m,&k,&w);
for(int i=;i<m;i++)
{
scanf("%d%d%d%d",&a[i].l,&a[i].r,&a[i].w,&a[i].type);
a[i].id=i*;
}
build(n,m,k,w);
int maxflow; // 汇点 2m+2 源点 2m 次源点2m+1
maxflow=solver.solve(*m,*m+);
printf("%d\n",-maxflow);
}
return ;
}

HDU 6437 最(大) 小费用最大流的更多相关文章

  1. hdu 1533 Going Home 最小费用最大流

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1533 On a grid map there are n little men and n house ...

  2. HDU 5988.Coding Contest 最小费用最大流

    Coding Contest Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)To ...

  3. hdu 3667(拆边+最小费用最大流)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3667 思路:由于花费的计算方法是a*x*x,因此必须拆边,使得最小费用流模板可用,即变成a*x的形式. ...

  4. hdu 3488(KM算法||最小费用最大流)

    Tour Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Submis ...

  5. hdu 2686 Matrix && hdu 3367 Matrix Again (最大费用最大流)

    Matrix Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Subm ...

  6. hdu 3395(KM算法||最小费用最大流(第二种超级巧妙))

    Special Fish Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  7. POJ-2135 Farm Tour---最小费用最大流模板题(构图)

    题目链接: https://vjudge.net/problem/POJ-2135 题目大意: 主人公要从1号走到第N号点,再重N号点走回1号点,同时每条路只能走一次. 这是一个无向图.输入数据第一行 ...

  8. hdu 1533 Going Home 最小费用最大流 入门题

    Going Home Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tota ...

  9. HDU–5988-Coding Contest(最小费用最大流变形)

    Coding Contest Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)To ...

随机推荐

  1. DMA简介

    直接存储器访问 直接存储器访问(Direct Memory Access,DMA)是计算机科学中的一种内存访问技术.它可以让外设可以独立地直接读写系统存储器,而不需绕道中央处理器(CPU),DMA是一 ...

  2. Centos5安装***

    最近shadowsocks挺火,看了几张帖子,感觉在手机上应该挺好用,电脑都是挂着ssh,用不到***了,下面贴出服务器安装过程: yum install build-essential autoco ...

  3. UVALive 2238 Fixed Partition Memory Management 固定分区内存管理(KM算法,变形)

    题意:目前有一部分可用内存,分为m个大小固定的区域.现有n个程序要执行,每个程序在不同大小的内存中运行所需的时间不同,要使运行完所有程序所耗时最少,问每个程序在哪块区域中从什么时间运行到什么时间,以及 ...

  4. H3C S5024P交换机 H3C AR28-31路由器命令

    H3C S5024P交换机 H3C AR28-31路由器命令 交换机命令 各个视图的切换: 注意命令要在相应的视图下执行 在用户视图下键入quit命令可以断开与交换机的连接.在其它视图中键入quit命 ...

  5. Java三大特性之封装

    .封装 1.概念:把对象的内部细节封闭起来,只提供操作对象属性的公共方法. 封装是面向对象编程语言对客观世界的模拟:如:电视机,她的内部元件就被封闭起来了,仅仅暴露电视机按钮来供人使用,这样就没有人能 ...

  6. html自己写响应式布局(说起来很高大上的样子,但是其实很简单)

    第一步,打开电脑中安装的Sublime Text3,新建demo文件夹用来存放文件,在里面新建一个HTML文件,通过Tab快捷键迅速创建一个HTML模板,并命名标题. 第二步,在Body标签里添加三个 ...

  7. 微信小程序---协同工作和发布

    (1)协同开发和发布 在中大型的公司里,人员的分工非常仔细,一般会有不同岗位角色的员工同时参与同一个小程序项目.为此,小程序平台设计了不同的权限管理使得项目管理者可以更加高效管理整个团队的协同工作. ...

  8. Hibernate初始化环境的基本封装

    public class HibernateUtils { private static SessionFactory sf; static{ sf = new Configuration().con ...

  9. 光猫&路由器网络配置

    前期准备:电脑(工业电脑).网线.光猫.路由器 1.检查连接光猫后能否正常上网:把网线两头的水晶头,一头插在光猫上的千兆口,一头插在电脑(工业电脑)的网口上,看电脑能否正常上网: 可以正常上网:说明光 ...

  10. JS数组专题1️⃣ ➖ 数组扁平化

    一.什么是数组扁平化 扁平化,顾名思义就是减少复杂性装饰,使其事物本身更简洁.简单,突出主题. 数组扁平化,对着上面意思套也知道了,就是将一个复杂的嵌套多层的数组,一层一层的转化为层级较少或者只有一层 ...