Problem Statement

Snuke runs a bakery.
He is planning for the next $N$ days.
Let us call these days Day $1,2,\cdots,N$.

At the moment, the bakery hires nobody.
There are $M$ bakers that Snuke can hire, numbered $1$ to $M$.

$C_i$ yen must be paid to hire Baker $i$.
If Baker $i$ is hired, they will work on Day $L_i, L_i+1, \cdots, R_i$, baking one loaf of bread each day.

For each $j$ ($1 \leq j \leq N$), there is a limit $A_j$ to the number of loaves of bread that can be sold on Day $j$. If $x_j$ loaves of bread are baked on Day $j$, $\min(x_j, A_j)$ loaves will be sold on that day.

Each loaf of bread sold will yield a profit of $D$ yen.

Snuke wants to choose the set of bakers to hire to maximize the final profit: $($The total number of loaves sold$) \times D - ($The total cost of hiring bakers$)$.
Find the maximum possible value of this.

Constraints

  • $1 \leq N \leq 2000$
  • $1 \leq M \leq 2000$
  • $1 \leq D \leq 10^9$
  • $1 \leq A_j \leq M$
  • $1 \leq L_i \leq R_i \leq N$
  • $1 \leq C_i \leq 10^9$
  • All values in input are integers.

网络流中一条流量为 $f$,费用为 $w$ 的边在下文表示为 $(f,w)$

考虑把每天的限制在网络流的边上考虑。

那么 \(i->i+1\) 的点表示这天的情况,连一条 \((a_i,d)\) 的边和一条 \((\infin,0)\) 的边。

然后对于一个面包师,从 \(R_i+1\) 到 \(L_i\) 连一条 \(-C_i\) 的边。

现在就要求他的最大费用循环流就行了。

全部取负,求最小费用循环流。

#include<bits/stdc++.h>
using namespace std;
const int N=2005,T=2004,INF=1e9;
int hd[N],e_num=1,n,m,d,v[N],p[N],g[N],a[N],l[N],r[N],c[N];
priority_queue<pair<long long,int>,vector<pair<long long,int> >,greater<pair<long long,int> > >q;
long long ans,h[N],dis[N];
struct edge{
int u,v,nxt,f,w;
}e[N*10];
void add_edge(int u,int v,int f,int w)
{
e[++e_num]=(edge){u,v,hd[u],f,w};
hd[u]=e_num;
e[++e_num]=(edge){v,u,hd[v],0,-w};
hd[v]=e_num;
}
void spfa()
{
static int l,r,q[N];
memset(h,0x3f,sizeof(h));
v[q[l=r=1]=0]=1,h[0]=0;
while(l<=r)
{
for(int i=hd[q[l%N]];i;i=e[i].nxt)
{
if(e[i].f&&h[e[i].v]>h[q[l%N]]+e[i].w)
{
h[e[i].v]=h[q[l%N]]+e[i].w;
if(!v[e[i].v])
v[q[(++r)%N]=e[i].v]=1;
}
}
v[q[(l++)%N]]=0;
}
}
int dijkstra()
{
memset(dis,0x3f,sizeof(dis));
memset(v,0,sizeof(v));
q.push(make_pair(0,0));
dis[0]=0;
while(!q.empty())
{
int k=q.top().second;
q.pop();
if(v[k])
continue;
v[k]=1;
for(int i=hd[k];i;i=e[i].nxt)
{
if(e[i].f&&dis[k]+e[i].w+h[k]-h[e[i].v]<dis[e[i].v])
{
assert(e[i].w+h[k]-h[e[i].v]>=0);
dis[e[i].v]=dis[k]+e[i].w+h[k]-h[e[i].v];
p[e[i].v]=i;
q.push(make_pair(dis[e[i].v],e[i].v));
}
}
}
return v[T];
}
int main()
{
scanf("%d%d%d",&n,&m,&d);
for(int i=1;i<=n;i++)
scanf("%d",a+i);
for(int i=1;i<=m;i++)
scanf("%d%d%d",l+i,r+i,c+i),g[l[i]]++,g[r[i]+1]--;
for(int i=1;i<=n;i++)
{
g[i]+=g[i-1];
if(g[i]<=a[i])
add_edge(i,i+1,g[i],d);
else
add_edge(i,i+1,a[i],d),add_edge(i,i+1,g[i]-a[i],0);
ans+=min(a[i],g[i])*1LL*d;
}
for(int i=1;i<=m;i++)
{
add_edge(0,l[i],1,0);
add_edge(l[i],r[i]+1,1,c[i]);
add_edge(r[i]+1,T,1,0);
}
spfa();
int ret=0;
while(dijkstra())
{
for(int i=0;i<=n+1;i++)
h[i]=h[i]+dis[i];
h[T]+=dis[T];
int mnf=INF;
for(int i=T;i;i=e[p[i]].u)
mnf=min(mnf,e[p[i]].f);
for(int i=T;i;i=e[p[i]].u)
e[p[i]].f-=mnf,e[p[i]^1].f+=mnf;
ret+=mnf;
ans-=mnf*h[T];
}
printf("%lld",ans);
}

[ARC137E] Baker的更多相关文章

  1. Mesh Baker的基本操作与功能演示

    原地址:http://www.narkii.com/club/thread-301789-1.html 如何降低游戏在系统中的消耗并带给用户最佳的体验是开发者一直追求的目标,在Unity里面对于模型与 ...

  2. Render To Texel Baker

    今天仔细研究了 Shaowgun 示例中那个金黄色雕像所使用的光照纹理烘焙工具:“Render To Texel Baker”.因为要在移动设备展现比较逼真的光照效果,但是实时使用法线贴图并大量用于场 ...

  3. Lightoj 1071 - Baker Vai (双线程DP)

    题目连接: http://lightoj.com/volume_showproblem.php?problem=1071 题目大意: 一个n*m的格子,Baker Vai要从(1,1)到(n,m)再回 ...

  4. UVa 11308 - Bankrupt Baker

    题目大意:给出一些原料和价钱和若干份菜谱,每份菜谱都标明所需的原料和数量,找出所有不超过预算的菜谱. 没什么好说的,主要是对map的运用. #include <cstdio> #inclu ...

  5. Baker Vai LightOJ - 1071 (MCMF)

    在个给出的矩阵从,从左上角走到右下角,然后再从右下角走到左上角,两次不能经过想同的点,每个点都有一个价值,问最大的价值是多少. 可以把原来的问题化简成从左上角走两条路到右下角,然后把价值加起来,然是这 ...

  6. 混沌数学之Baker模型

    相关DEMO参见:混沌数学之离散点集图形DEMO 相关代码: // http://wenku.baidu.com/view/ac9b57ea172ded630b1cb65b.html class Ba ...

  7. LightOJ 1071 Baker Vai(拆点+最大费用流)题解

    题意:给一个n*m的方格,每个格子上都有一个数字表示价值,小A在左上角,他从左上角走到右下角只能向右或向下走,然后再从右下角走上左上角,这次只能向上或向左走,这两条路绝对没有重复,问你怎样走有最大价值 ...

  8. Baker Vai LightOJ - 1071

    题意:类似传纸条 方法: 把他要求的操作(一个人来回),转化为两个人同时走,除了开始和结束位置只能走不同路,得到的分数和的最大值即可. 一开始想到要定义的状态,是两个人的x(行)和y(列)坐标.这样时 ...

  9. codewars--js--Pete, the baker

    问题描述: Pete likes to bake some cakes. He has some recipes and ingredients. Unfortunately he is not go ...

  10. 开源 iOS 项目分类索引大全 - 待整理

    开源 iOS 项目分类索引大全 GitHub 上大概600个开源 iOS 项目的分类和介绍,对于你挑选和使用开源项目应该有帮助 系统基础库 Category/Util sstoolkit 一套Cate ...

随机推荐

  1. 开机自动打开termux以及启动termux的服务

    ps:因为我们的服务是安装在平板上面的termux,客户不想维护麻烦,如果平板重启之后还需要手动启动ternux,还要开启命令启动服务,这样比较麻烦,所以研究如下操作 1.安装macroDroid 直 ...

  2. Mysql高级8-触发器

    一.触发器 触发器是与表有关的数据库对象,指在insert/update/delete之前或者之后,触发并执行触发器中定义的sql语句集合,触发器的这种特性可以协助应用在数据库端确保数据的完整性,日志 ...

  3. 一文了解Gin对Cookie的支持

    1. 引言 本文将从Web应用程序处理请求时需要用户信息,同时HTTP又是无状态协议这个矛盾点出发.从该问题出发,简单描述了解决该问题的Token 机制,进而引出Cookie的实现方案. 基于此我们将 ...

  4. linux shell根据关键字用sed注释掉整行

    一.将带有ab的行注释掉 # cat test # sed -i '/ab/s/^\(.*\)$/#\1/g' test ab是关键字 s是语法替换 ^是行首 $是行尾 \是转义符 数字1带表前述匹配 ...

  5. Hugging News #0912: Hugging Face 2 人入选时代周刊全球百大 AI 人物

    每一周,我们的同事都会向社区的成员们发布一些关于 Hugging Face 相关的更新,包括我们的产品和平台更新.社区活动.学习资源和内容更新.开源库和模型更新等,我们将其称之为「Hugging Ne ...

  6. Docker Compose V2 安装常用数据库MySQL+Mongo

    前言 书接上篇:Docker V24 及 Docker Compose V2 的安装及使用 本篇操作都在 centos8 虚拟机 devops01 中进行,并都归属网络:devopsnetwork 主 ...

  7. 在Docker下一键安装部署免费开源的问答社区!

    在Docker下一键安装部署免费开源的问答社区!   1.准备一台VPS主机,没有的话,[搞一台] 2.一键安装部署Docker wget https://raw.githubusercontent. ...

  8. nacos-mysql.sql

    https://github.com/alibaba/nacos/blob/master/distribution/conf/nacos-mysql.sql # Nacos安装 /home/nacos ...

  9. C#学习笔记---异常捕获和变量

    异常捕获 使用异常捕获可以捕获出现异常的代码块,防止因为异常抛出造成的程序卡死的情况发生. try{}catch{}finally{}结构 //异常捕获 try { string str=Consol ...

  10. 可视化-vscode使用Plotly,绘制直方图

    Plotly 是一款用来做数据分析和可视化的在线平台,功能非常强大,可以在线绘制很多图形比如条形图.散点图.饼图.直方图等等. 概述: plotly在python中绘图使用分三种:1.plotly.g ...