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 餐巾计划问题 费用流的更多相关文章

  1. LuoguP1251 餐巾计划问题(费用流)

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

  2. 洛谷.1251.餐巾计划问题(费用流SPFA)

    题目链接 /* 每一天的餐巾需求相当于必须遍历某些点若干次 设q[i]为Dayi需求量 (x,y)表示边x容y费 将每个点i拆成i,i',由i'->T连(q[i],0)的边,表示求最大流的话一定 ...

  3. 洛谷 P1251 餐巾计划问题(线性规划网络优化)【费用流】

    (题外话:心塞...大部分时间都在debug,拆点忘记加N,总边数算错,数据类型标错,字母写错......) 题目链接:https://www.luogu.org/problemnew/show/P1 ...

  4. P1251 餐巾计划问题

    P1251 餐巾计划问题 题目描述 一个餐厅在相继的 N 天里,每天需用的餐巾数不尽相同.假设第 iii 天需要 rir_iri​块餐巾( i=1,2,...,N).餐厅可以购买新的餐巾,每块餐巾的费 ...

  5. P1251 餐巾计划问题 网络流

    P1251 餐巾计划问题 #include <bits/stdc++.h> using namespace std; typedef long long ll; , inf = 0x3f3 ...

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

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

  7. 【Luogu】P1251餐巾计划(上下界费用流)

    题目链接 学了一下上下界费用流,似乎很nb.但是我说得不好,所以这里给出博客链接. 某dalao的博客 然后这道题的解法就是先用上下界费用流的建图方式连早上和晚上之间的那条边,保证当天一定会有r条或以 ...

  8. 洛谷P1251 餐巾计划问题(最小费用最大流)

    题意 一家餐厅,第$i$天需要$r_i$块餐巾,每天获取餐巾有三种途径 1.以$p$的费用买 2.以$f$的费用送到快洗部,并在$m$天后取出 3.以$s$的费用送到慢洗部,并在$n$天后取出 问满足 ...

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

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

随机推荐

  1. 深入学习 Intellij IDEA 调试技巧

    程序员的日常工作除了写代码之外,很大一部分时间将会在查找 BUG,解决问题.查找 BUG,离不开在 IDE 中调试代码.熟练的掌握调试技巧,可以帮助我们减少查找时间,快速定位问题. 在 IDEA 中调 ...

  2. Hive映射HBase表的几种方式

    1.Hive内部表,语句如下 CREATE TABLE ods.s01_buyer_calllogs_info_ts( key string comment "hbase rowkey&qu ...

  3. 计算机网络中IP地址和MAC地址

    计算机 网络中的网络地址有I P 地址和物理地址之分,对 于主机间的通信时,它们的作用也不一样 . l   I P 地址 为 了保证 I n t e r n e t 网上主机通信时能够相互识别 ,不引 ...

  4. 反应式微服务框架Flower

    Flower是一个构建在Akka上的反应式微服务框架,开发者只需要针对每一个细粒度的业务功能开发一个Service服务,并将这些Service按照业务流程进行可视化编排,即可得到一个反应式系统. 即时 ...

  5. spark shuffle写操作三部曲之UnsafeShuffleWriter

    前言 在前两篇文章 spark shuffle的写操作之准备工作 中引出了spark shuffle的三种实现,spark shuffle写操作三部曲之BypassMergeSortShuffleWr ...

  6. 从JavaScript到Python之异常

    不少前端工程师看到这个标题可能会产生质问: 我js用得好好的,能后端能APP,为什么还要学习Python? 至少有下面两个理由: 学习曲线.ES6之后的JavaScript(TypeScript)的在 ...

  7. 【Java笔记】【Java核心技术卷1】chapter3 D3数据类型

    package chapter3; public class D3数据类型 { public static void main(String[] arg) { //Java 整型(字节数不会随硬件变化 ...

  8. Ubuntu 16.04 硬盘安装

    自己的宏碁4741G 笔记本,已经用了6年了,最近感觉越来越慢,晚上突然想起装个Linux 玩玩,说干就干,选择了用户比较多的Ubuntu,网上下载16.04的版本,结合网上搜索到的安装教程,看似简单 ...

  9. 1.1Django简介和虚拟环境配置

    MVC 大部分开发语言中都有MVC框架 MVC框架的核心思想是:解耦 降低各功能模块之间的耦合性,方便变更,更容易重构代码,最大程度上实现代码的重用 m表示model,主要用于对数据库层的封装 v表示 ...

  10. javaScript基础-04 对象

    一.对象的基本概念 对象是JS的基本数据类型,对象是一种复合值,它将很多值(原始值或者对象)聚合在一起,可通过名字访问这些值,对象也可看做是属性的无序集合,每个属性都是一个名/值对.对象不仅仅是字符串 ...