题目传送门

  传送门

题目大意

  (经典题还不知道题意?)

  容易想到需要把未使用的餐巾和已经使用的餐巾分开。

  设$X_i$表示第$i$天已经的使用餐巾的点,设$Y_i$表示第$i$天还未使用的餐巾的点

  我们知道使用过的餐巾数量 = 洗出来的餐巾数量 + 购买的餐巾数量(一个餐巾被多次洗出来算多次)。

  右边是啥,我们不清楚,但是我们清楚每一天新增的使用过的餐巾的数量,所以源点向$X_i$连一条容量为$r_i$,费用为0的边。

  接下来还有几种连边:

  • $X_i$向$X_{i + 1}$连一条容量为$\infty$,费用为0的边(使用过的餐巾还是使用过的,$i < n$)。
  • $Y_i$向$Y_{i + 1}$连一条容量为$\infty$,费用为0的边
  • $X_i$向$Y_{i + M}$连一条容量为$\infty$,费用为$F$的边
  • $X_i$向$Y_{i + N}$连一条容量为$\infty$,费用为$S$的边

  对于第$i$天会用掉$r_i$的餐巾,所以$Y_i$向汇点连一条容量为$r_i$,费用为0的边。

  还有一个购买餐巾,它直接会变成这一天的未使用过的餐巾,所以原点向$Y_i$连一条容量为$\infty$,费用为$p$的边。

Code

 /**
* loj
* Problem#6008
* Accepted
* Time: 701ms
* Memory: 636k
*/
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <queue>
using namespace std;
typedef bool boolean; template <typename T>
void pfill(T* pst, const T* ped, T val) {
for ( ; pst != ped; *(pst++) = val);
} typedef class Edge {
public:
int ed, nx, r, c; Edge(int ed = , int nx = , int r = , int c = ) : ed(ed), nx(nx), r(r), c(c) { }
} Edge; typedef class MapManager {
public:
int* h;
vector<Edge> es; MapManager() { }
MapManager(int n) {
h = new int[(n + )];
pfill(h, h + n + , -);
} void addEdge(int u, int v, int r, int c) {
es.push_back(Edge(v, h[u], r, c));
h[u] = (signed) es.size() - ;
} void addArc(int u, int v, int cap, int c) {
addEdge(u, v, cap, c);
addEdge(v, u, , -c);
} Edge& operator [] (int p) {
return es[p];
}
} MapManager; const signed int inf = (signed) (~0u >> ); class Graph {
public:
int S, T;
MapManager g; int *le;
int *f, *mf;
boolean *vis; // be sure T is the last node
void set(int S, int T) {
this->S = S;
this->T = T;
f = new int[(T + )];
le = new int[(T + )];
mf = new int[(T + )];
vis = new boolean[(T + )];
pfill(vis, vis + T, false);
} int spfa() {
queue<int> que;
pfill(f, f + T + , inf);
que.push(S);
f[S] = , le[S] = -, mf[S] = inf;
while (!que.empty()) {
int e = que.front();
que.pop();
vis[e] = false;
for (int i = g.h[e], eu, w; ~i; i = g[i].nx) {
if (!g[i].r)
continue;
eu = g[i].ed, w = f[e] + g[i].c;
if (w < f[eu]) {
f[eu] = w, le[eu] = i, mf[eu] = min(mf[e], g[i].r);
if (!vis[eu]) {
vis[eu] = true;
que.push(eu);
}
}
}
}
if (f[T] == inf)
return inf;
int rt = ;
for (int p = T, e; ~le[p]; p = g[e ^ ].ed) {
e = le[p];
g[e].r -= mf[T];
g[e ^ ].r += mf[T];
rt += mf[T] * g[e].c;
}
return rt;
} int min_cost() {
int rt = , delta;
while ((delta = spfa()) != inf) {
rt += delta;
// cerr << delta << '\n';
}
return rt;
}
} Graph; int n;
int P, M, F, N, S;
int *require;
MapManager &g = Graph.g; inline void init() {
scanf("%d", &n);
scanf("%d%d%d%d%d", &P, &M, &F, &N, &S);
require = new int[(n + )];
g = MapManager(n << | );
for (int i = ; i <= n; i++) {
scanf("%d", require + i);
}
} inline void solve() {
int T = n << | ;
Graph.set(, T);
for (int i = ; i <= n; i++) {
g.addArc(, i, require[i], );
g.addArc(, i + n, inf, P);
if (i < n) {
g.addArc(i, i + , inf, );
g.addArc(i + n, i + n + , inf, );
}
if (i + M <= n)
g.addArc(i, i + n + M, inf, F);
if (i + N <= n)
g.addArc(i, i + n + N, inf, S);
g.addArc(i + n, T, require[i], );
}
printf("%d\n", Graph.min_cost());
} int main() {
init();
solve();
return ;
}

loj 6008 餐巾计划 - 费用流的更多相关文章

  1. [2019多校联考(Round 6 T3)]脱单计划 (费用流)

    [2019多校联考(Round 6 T3)]脱单计划 (费用流) 题面 你是一家相亲机构的策划总监,在一次相亲活动中,有 n 个小区的若干男士和 n个小区的若干女士报名了这次活动,你需要将这些参与者两 ...

  2. hdu 6118度度熊的交易计划(费用流)

    度度熊的交易计划 Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  3. 餐巾计划问题 zwk费用流解法

    «问题描述:一个餐厅在相继的N 天里,每天需用的餐巾数不尽相同.假设第i天需要ri块餐巾(i=1,2,…,N).餐厅可以购买新的餐巾,每块餐巾的费用为p分:或者把旧餐巾送到快洗部,洗一块需m天,其费用 ...

  4. LibreOJ #6008. 「网络流 24 题」餐巾计划 最小费用最大流 建图

    #6008. 「网络流 24 题」餐巾计划 内存限制:256 MiB时间限制:1000 ms标准输入输出 题目类型:传统评测方式:文本比较 上传者: 匿名 提交提交记录统计讨论测试数据   题目描述 ...

  5. Libre 6008 「网络流 24 题」餐巾计划 (网络流,最小费用最大流)

    Libre 6008 「网络流 24 题」餐巾计划 (网络流,最小费用最大流) Description 一个餐厅在相继的N天里,第i天需要Ri块餐巾(i=l,2,-,N).餐厅可以从三种途径获得餐巾. ...

  6. [luogu_P1251][LOJ#6008]「网络流 24 题」餐巾计划

    [luogu_P1251][LOJ#6008]「网络流 24 题」餐巾计划 试题描述 一个餐厅在相继的 \(N\) 天里,第 \(i\) 天需要 \(R_i\) 块餐巾 \((i=l,2,-,N)\) ...

  7. 【网络流24题】 No.10 餐巾计划问题 (线性规划网络优化 最小费用最大流)

    [题意] 一个餐厅在相继的 N 天里, 每天需用的餐巾数不尽相同. 假设第 i 天需要 ri 块餐巾(i=1,2,-, N). 餐厅可以购买新的餐巾,每块餐巾的费用为 p 分:或者把旧餐巾送到快洗部, ...

  8. 网络流之最小费用最大流 P1251 餐巾计划问题

    题目描述 一个餐厅在相继的 NN 天里,每天需用的餐巾数不尽相同.假设第 ii 天需要 r_iri​块餐巾( i=1,2,...,N).餐厅可以购买新的餐巾,每块餐巾的费用为 pp 分;或者把旧餐巾送 ...

  9. 【刷题】LOJ 6008 「网络流 24 题」餐巾计划

    题目描述 一个餐厅在相继的 \(n\) 天里,每天需用的餐巾数不尽相同.假设第 \(i\) 天需要 \(r_i\) 块餐巾.餐厅可以购买新的餐巾,每块餐巾的费用为 \(P\) 分:或者把旧餐巾送到快洗 ...

随机推荐

  1. js常用正则表达式判断

    1.判断IP:端口 <html> <head> </head> <body> ip:port<input type="" na ...

  2. spring mvc 总结

    依赖包 <!-- spring依赖 --> <dependency> <groupId>org.springframework</groupId> &l ...

  3. boot跳转到app后,中断不起作用的原因

    boot跳转到app后 osKernelStart()之前,中断有问题,不起作用 原因是因为boot跳转之前__disable_irq(); 跳转到APP后,并不是一切从头开始,__disable_i ...

  4. Android的几种Manager

    电话管理器TelephoneManager 第一个实例是获取网络和SIM卡信息:界面是一个列表,这里省略,Java代码如下: public class MainActivity extends Act ...

  5. RoR - Action Pack

    什么是Action Pack: Rest and Rails: Rest if all about resources rake routes:  print out all routes

  6. Linux-004-解决 Tomcat 启动时提示 Insufficient space for shared memory file

    今天在帮同事定位问题时,定位服务发现有服务无法访问,发现在 Linux 启动 tomcat 时,提示如下信息: 即服务提示共享内存空间不足,可以使用 -Djava.io.tmpdir 参数指定期本地临 ...

  7. spring 数据库多数据源路由

    项目中需要根据不同业务进行分库,首先是将业务不同业务映射到不同过的数据库( biz --> db,可能存在多对一情况), 查看springjdbc源码发现AbstractRoutingDataS ...

  8. Delphi窗体显示Echarts图表

    笨办法,先保存用着 unit Unit1; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Varian ...

  9. T-SQL语言基础(1)之理论背景

    从学校就开始接触和使用 SQL 了,但一直没有怎么细细去了解它,最近入职的公司比较重 T-SQL 部分,所以就准备系统的学习一下. 买了一本<Microsoft SQL Server 2008 ...

  10. [ROS]激光驱动安装

    参考资料: https://blog.csdn.net/hongliang2009/article/details/73302986 https://blog.csdn.net/bohaijun_12 ...