P1251 餐巾计划问题 费用流
https://www.luogu.org/problemnew/show/P1251
题意
有一家酒店,酒店每天需要ri张桌布,桌布可以现买,p元。可以通过快洗店,等m天,f元。可以通过慢洗店,等n天,s元。问满足每天用布需求的最小费用
思路
这道题拆点是要的,把一天拆成早上和晚上。比较精彩的是,把每天需要用ri张桌布分开来看,“早上需要有ri张脏布”,“晚上有ri张脏布”。翻译过来就是,早上向终点连ri容量的边,源点向晚上连ri容量的边。
然后又是三种情况的讨论,1)现买,源点向早上连费用为p的边。2)快洗店,晚上向+m天连费用为f的边。3)慢洗店,晚上向+n天连费用为s的边。
最后还要注意,由于可以留下晚上的脏布,所以每个晚上向下一个晚上连边。
#include <algorithm>
#include <iterator>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <iomanip>
#include <bitset>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <stack>
#include <cmath>
#include <queue>
#include <list>
#include <map>
#include <set>
#include <cassert> /* ⊂_ヽ
\\ Λ_Λ 来了老弟
\('ㅅ')
> ⌒ヽ
/ へ\
/ / \\
レ ノ ヽ_つ
/ /
/ /|
( (ヽ
| |、\
| 丿 \ ⌒)
| | ) /
'ノ ) Lノ */ using namespace std;
#define lson (l , mid , rt << 1)
#define rson (mid + 1 , r , rt << 1 | 1)
#define debug(x) cerr << #x << " = " << x << "\n";
#define pb push_back
#define pq priority_queue typedef long long ll;
typedef unsigned long long ull;
//typedef __int128 bll;
typedef pair<ll ,ll > pll;
typedef pair<int ,int > pii;
typedef pair<int,pii> p3; //priority_queue<int> q;//这是一个大根堆q
//priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q
#define fi first
#define se second
//#define endl '\n' #define boost ios::sync_with_stdio(false);cin.tie(0)
#define rep(a, b, c) for(int a = (b); a <= (c); ++ a)
#define max3(a,b,c) max(max(a,b), c);
#define min3(a,b,c) min(min(a,b), c); const ll oo = 1ll<<;
const ll mos = 0x7FFFFFFF; //
const ll nmos = 0x80000000; //-2147483648
const int inf = 0x3f3f3f3f;
const ll inff = 0x3f3f3f3f3f3f3f3f; //
const int mod = 1e9+;
const double esp = 1e-;
const double PI=acos(-1.0);
const double PHI=0.61803399; //黄金分割点
const double tPHI=0.38196601; template<typename T>
inline T read(T&x){
x=;int f=;char ch=getchar();
while (ch<''||ch>'') f|=(ch=='-'),ch=getchar();
while (ch>=''&&ch<='') x=x*+ch-'',ch=getchar();
return x=f?-x:x;
} inline void cmax(int &x,int y){if(x<y)x=y;}
inline void cmax(ll &x,ll y){if(x<y)x=y;}
inline void cmin(int &x,int y){if(x>y)x=y;}
inline void cmin(ll &x,ll y){if(x>y)x=y;} /*-----------------------showtime----------------------*/ const int maxn = ;
struct E{
int v,w,cost;
int nxt;
}edge[*maxn*maxn];
int n,gtot = ;
int head[*maxn];
void addedge(int u,int v,int w,int cost){
edge[gtot].v = v;
edge[gtot].w = w;
edge[gtot].cost = cost;
edge[gtot].nxt = head[u];
head[u] = gtot ++; edge[gtot].v = u;
edge[gtot].w = ;
edge[gtot].cost = -*cost;
edge[gtot].nxt = head[v];
head[v] = gtot++;
}
int vis[maxn*],dis[maxn*],pre[maxn*],path[maxn*];
bool spfa(int s,int t){
memset(vis, , sizeof(vis));
memset(dis, inf, sizeof(dis));
memset(pre, -, sizeof(pre)); queue<int>que; que.push(s); vis[s] = ;
dis[s] = ;
while(!que.empty()){
int u = que.front(); que.pop(); vis[u] = ;
for(int i=head[u]; ~i; i = edge[i].nxt){
int v = edge[i].v, w = edge[i].w, cost = edge[i].cost;
if(w > && dis[v] > dis[u] + cost){
dis[v] = dis[u] + cost;
pre[v] = u; path[v] = i;
if(vis[v] == ){
que.push(v);
vis[v] = ;
}
}
}
}
return pre[t] != -;
} ll solve(int s,int t){
ll flow = , cost = ;
while(spfa(s,t)){
int f = inf;
for(int i=t; i!=s; i = pre[i]){
f = min(f, edge[path[i]].w);
}
flow += f;
cost += 1ll*f * dis[t];
// cout<<f<<" "<<dis[t]<<endl;
for(int i=t; i!=s; i = pre[i]){
edge[path[i]].w -= f;
edge[path[i] ^ ].w += f;
}
}
return cost;
} int main(){
memset(head, -, sizeof(head));
scanf("%d", &n);
int s = , t = n+n+;
for(int i=; i<=n; i++) {
int x;
scanf("%d", &x);
addedge(s, i+n, x, );
addedge(i, t, x, );
}
int p,m,ff,nn,ss;
scanf("%d%d%d%d%d", &p, &m, &ff, &nn, &ss);
for(int i=; i<=n; i++) addedge(s, i, inf, p); for(int i=; i + m <=n; i++) addedge(i+n, i+m, inf, ff);
for(int i=; i + nn<=n; i++) addedge(i+n, i+nn, inf, ss); for(int i=; i<n; i++) addedge(i+n, i+n+, inf, );
printf("%lld\n", solve(s, t));
return ;
}
P1251 餐巾计划问题 费用流的更多相关文章
- LuoguP1251 餐巾计划问题(费用流)
题目描述 一个餐厅在相继的 NN 天里,每天需用的餐巾数不尽相同.假设第 ii 天需要 r_iri块餐巾( i=1,2,...,N).餐厅可以购买新的餐巾,每块餐巾的费用为 pp 分;或者把旧餐巾送 ...
- 洛谷.1251.餐巾计划问题(费用流SPFA)
题目链接 /* 每一天的餐巾需求相当于必须遍历某些点若干次 设q[i]为Dayi需求量 (x,y)表示边x容y费 将每个点i拆成i,i',由i'->T连(q[i],0)的边,表示求最大流的话一定 ...
- 洛谷 P1251 餐巾计划问题(线性规划网络优化)【费用流】
(题外话:心塞...大部分时间都在debug,拆点忘记加N,总边数算错,数据类型标错,字母写错......) 题目链接:https://www.luogu.org/problemnew/show/P1 ...
- P1251 餐巾计划问题
P1251 餐巾计划问题 题目描述 一个餐厅在相继的 N 天里,每天需用的餐巾数不尽相同.假设第 iii 天需要 rir_iri块餐巾( i=1,2,...,N).餐厅可以购买新的餐巾,每块餐巾的费 ...
- P1251 餐巾计划问题 网络流
P1251 餐巾计划问题 #include <bits/stdc++.h> using namespace std; typedef long long ll; , inf = 0x3f3 ...
- 网络流之最小费用最大流 P1251 餐巾计划问题
题目描述 一个餐厅在相继的 NN 天里,每天需用的餐巾数不尽相同.假设第 ii 天需要 r_iri块餐巾( i=1,2,...,N).餐厅可以购买新的餐巾,每块餐巾的费用为 pp 分;或者把旧餐巾送 ...
- 【Luogu】P1251餐巾计划(上下界费用流)
题目链接 学了一下上下界费用流,似乎很nb.但是我说得不好,所以这里给出博客链接. 某dalao的博客 然后这道题的解法就是先用上下界费用流的建图方式连早上和晚上之间的那条边,保证当天一定会有r条或以 ...
- 洛谷P1251 餐巾计划问题(最小费用最大流)
题意 一家餐厅,第$i$天需要$r_i$块餐巾,每天获取餐巾有三种途径 1.以$p$的费用买 2.以$f$的费用送到快洗部,并在$m$天后取出 3.以$s$的费用送到慢洗部,并在$n$天后取出 问满足 ...
- LibreOJ #6008. 「网络流 24 题」餐巾计划 最小费用最大流 建图
#6008. 「网络流 24 题」餐巾计划 内存限制:256 MiB时间限制:1000 ms标准输入输出 题目类型:传统评测方式:文本比较 上传者: 匿名 提交提交记录统计讨论测试数据 题目描述 ...
随机推荐
- centos7更新yum库为aliyun库
1. 备份原来的yum源$sudo cp /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak 2.设置ali ...
- 20190803 NOIP模拟测试12「斐波那契(fibonacci)· 数颜色 · 分组 」
164分 rank11/64 这次考的不算太差,但是并没有多大的可能性反超(只比一小部分人高十几分而已),时间分配还是不均,T2两个半小时,T1半个小时,T3-额十几分钟吧 然额付出总是与回报成反比的 ...
- java虚拟机学习笔记(六)---垃圾收集算法
主要讨论集中垃圾收集算法的思想及发展过程. 1.标记-清除法 最基础的收集算法是标记-清除法,算法分为标记和清除两个阶段:首先标记出所有需要回收的对象,在标记完成后统一回收所有被标记的对象,其标记过程 ...
- koa2图片上传成功后返回服务器地址,实时显示服务器图片
版本:node(8.5.0); koa(2.4.1); koa-router(7.3.0); koa-body(2.5.0); koa-static(4.0.2); 代码实现 const fs = r ...
- MySQL储存过程详解
我们常用的操作数据库语言SQL语句在执行的时候需要要先编译,然后执行,而存储过程(Stored Procedure)是一组为了完成特定功能的SQL语句集,经编译后存储在数据库中,用户通过指定存储过程的 ...
- 洛谷 P2024 [NOI2001]食物链
题意简述 有人用两种说法对这 N 个动物所构成的食物链关系进行描述: 1."1 X Y",表示 X 和 Y 是同类. 2."2 X Y",表示 X 吃 Y . ...
- 走进JavaWeb技术世界5:初探Tomcat的HTTP请求过程
初探Tomcat的HTTP请求过程 前言:1.作为Java开发人员,大多都对Tomcat不陌生,由Apache基金会提供技术支持与维护,因为其免费开源且易用,作为Web服务器深受市场欢迎,所以有必要对 ...
- linux之压缩和解压
归档:也称为打包,指的是一个文件或目录的集合,而这个集合被存储在一个文件中.归档文件没有经过压缩,因此,它占用的空间是其中所有文件和目录的总和.压缩:压缩文件也是一个文件和目录的集合,且这个集合也被存 ...
- 就当我在扯淡,宇宙的bug
Geohot说到“我打算建立一个组织让人们从人工智能模拟中‘越狱’,释放真正的人性.” 不知从何时开始,世界上的知名科学家,黑客等都开始怀疑我们所处世界的真实性. 我们的世界上是真实存在的吗?是否存在 ...
- 【原】UILabel 设置了 attributedText 后省略号不显示
在开发中,对于一个 UILabel 我们都会设置 lineBreakMode 属性. 我在开发中就遇到个比较有意思的问题,所以就写了这篇博客,与大家共勉! 对于一个 UILabel ,我先设置了 se ...