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. shiro 和 spring boot 的集成

    1 添加依赖 使用 shiro-spring-boot-web-starter 在 spring boot 中集成 shiro 只需要再添加一个依赖 <dependency> <gr ...

  2. STL 大法好

    #include <vector>  1.支持随机访问,但不支持在任意位置O(1)插入:    2.定义:  ```cpp      vector<int> a;  ```  ...

  3. UE4 本地化不起作用 SetCurrentCulture

    UE4 本地化 FInternationalization::Get ().SetCurrentCulture ( TEXT ( "en" ) ) FInternationaliz ...

  4. 夯实Java基础(五)——==与equals()

    1.前言 我们在学习Java的时候,看到==.equals()就认为比较简单,随便看了一眼就过了,其实你并没有深入去了解二者的区别.这个问题在面试的时候出现的频率比较高,而且据统计有85%的人理直气壮 ...

  5. SpringBoot第一天

    一,SpringBoot 介绍 1,如果使用 Spring 开发一个"HelloWorld"的 web 应用: • 创建一个 web 项目并且导入相关 jar 包.SpringMV ...

  6. 伪分布式Spark + Hive on Spark搭建

    Spark大数据平台有使用一段时间了,但大部分都是用于实验而搭建起来用的,搭建过Spark完全分布式,也搭建过用于测试的伪分布式.现在是写一遍随笔,记录一下曾经搭建过的环境,免得以后自己忘记了.也给和 ...

  7. bio,nio,aio学习

    http://qindongliang.iteye.com/blog/2018539 1 同步 指的是用户进程触发IO操作并等待或者轮询的去查看IO操作是否就绪 自己上街买衣服,自己亲自干这件事,别的 ...

  8. HelloDjango 第 07 篇:创作后台开启,请开始你的表演!

    作者:HelloGitHub-追梦人物 文中涉及的示例代码,已同步更新到 HelloGitHub-Team 仓库 在此之前我们完成了 django 博客首页视图的编写,我们希望首页展示发布的博客文章列 ...

  9. Java一个简单的文件工具集

    class FileUtils { //文件目录下文件总数目 public static int fileNumber(File dir) { int filenumber = 0; if(dir.e ...

  10. 洛谷 P1960 列队

    题意简述 有一个n × m 的矩阵,第i行第j列元素编号为(i - 1)× m +j 每次将一个数取出,其他元素依次向左,向上填补空缺,最后将取出的数放入矩阵最后一格 求每次取出数的编号 题解思路 由 ...