loj 6008 餐巾计划 - 费用流
容易想到需要把未使用的餐巾和已经使用的餐巾分开。
设$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 餐巾计划 - 费用流的更多相关文章
- [2019多校联考(Round 6 T3)]脱单计划 (费用流)
[2019多校联考(Round 6 T3)]脱单计划 (费用流) 题面 你是一家相亲机构的策划总监,在一次相亲活动中,有 n 个小区的若干男士和 n个小区的若干女士报名了这次活动,你需要将这些参与者两 ...
- hdu 6118度度熊的交易计划(费用流)
度度熊的交易计划 Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- 餐巾计划问题 zwk费用流解法
«问题描述:一个餐厅在相继的N 天里,每天需用的餐巾数不尽相同.假设第i天需要ri块餐巾(i=1,2,…,N).餐厅可以购买新的餐巾,每块餐巾的费用为p分:或者把旧餐巾送到快洗部,洗一块需m天,其费用 ...
- LibreOJ #6008. 「网络流 24 题」餐巾计划 最小费用最大流 建图
#6008. 「网络流 24 题」餐巾计划 内存限制:256 MiB时间限制:1000 ms标准输入输出 题目类型:传统评测方式:文本比较 上传者: 匿名 提交提交记录统计讨论测试数据 题目描述 ...
- Libre 6008 「网络流 24 题」餐巾计划 (网络流,最小费用最大流)
Libre 6008 「网络流 24 题」餐巾计划 (网络流,最小费用最大流) Description 一个餐厅在相继的N天里,第i天需要Ri块餐巾(i=l,2,-,N).餐厅可以从三种途径获得餐巾. ...
- [luogu_P1251][LOJ#6008]「网络流 24 题」餐巾计划
[luogu_P1251][LOJ#6008]「网络流 24 题」餐巾计划 试题描述 一个餐厅在相继的 \(N\) 天里,第 \(i\) 天需要 \(R_i\) 块餐巾 \((i=l,2,-,N)\) ...
- 【网络流24题】 No.10 餐巾计划问题 (线性规划网络优化 最小费用最大流)
[题意] 一个餐厅在相继的 N 天里, 每天需用的餐巾数不尽相同. 假设第 i 天需要 ri 块餐巾(i=1,2,-, N). 餐厅可以购买新的餐巾,每块餐巾的费用为 p 分:或者把旧餐巾送到快洗部, ...
- 网络流之最小费用最大流 P1251 餐巾计划问题
题目描述 一个餐厅在相继的 NN 天里,每天需用的餐巾数不尽相同.假设第 ii 天需要 r_iri块餐巾( i=1,2,...,N).餐厅可以购买新的餐巾,每块餐巾的费用为 pp 分;或者把旧餐巾送 ...
- 【刷题】LOJ 6008 「网络流 24 题」餐巾计划
题目描述 一个餐厅在相继的 \(n\) 天里,每天需用的餐巾数不尽相同.假设第 \(i\) 天需要 \(r_i\) 块餐巾.餐厅可以购买新的餐巾,每块餐巾的费用为 \(P\) 分:或者把旧餐巾送到快洗 ...
随机推荐
- Spring boot 国际化自动加载资源文件问题
Spring boot 国际化自动加载资源文件问题 最近在做基于Spring boot配置的项目.中间遇到一个国际化资源加载的问题,正常来说只要在application.properties文件中定义 ...
- npm run dev没反应
npm config set registry https://registry.npm.taobao.org npm install npm run dev
- python练习题-day21
1.三级菜单 china_ditie = { "北京": { "一号线": ["四惠", "大望路", "天安 ...
- python实现ip地址查询经纬度定位
1.此api已经关闭 https://api.map.baidu.com/highacciploc/v1?qcip=220.181.38.113&ak=你申请的AK&extension ...
- webpack(6)-模块热替代&tree shaking
模块热替换(hot module replacement 或 HMR) 模块热替换(hot module replacement 或 HMR)是 webpack 提供的最有用的功能之一.它允许在运行时 ...
- python类与对象-如何为创建大量实例节省内存
如何为创建大量实例节省内存 问题举例 在网络游戏中,定义玩家类Player(id, name, level...), 每个玩家在线将创建一个Player实例,当在线人数很多时,将产生大量实例, 如何降 ...
- Linux系统下的网络配置
一.修改配置文件,重启后设置不丢失 [Red Hat Linux/CentOS] 使用ifconfig查看使用的网口: [root@localhost /]# ifconfig 修改对应网口配置文件: ...
- Ubuntu 常见的问题及常见软件的安装_ubuntu16.04
1, 在使用 rpm 删除 rpm 包的时候的: rpm -r *** 报错: error reading information on service sfdc3: No such file or ...
- CentOS 7.5 安装 Python3.7
1.安装开发者工具 yum -y groupinstall "Development Tools" 2.安装Python编译依赖包 yum -y install openssl-d ...
- 面试 -- 关于Activity的相关知识
本篇文章就是记录关于简单的Activity中的问题 问题一:当Activity中存在两个注册的Activity都是主界面的话,应用程序会报错吗?如果不报错,会把那个Activity当做主界面执行 &l ...