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标准输入输出 题目类型:传统评测方式:文本比较 上传者: 匿名 提交提交记录统计讨论测试数据 题目描述 ...
随机推荐
- python3键盘输入
1.脚本 # -*- coding: utf-8 -*- print("今年是哪一年?"),year = input("年份:")print ("ji ...
- 这 3 个 Set 集合的实现有点简单,那来做个总结吧
Set 接口是 Java Collections Framework 中的一员,它的特点是:不能包含重复的元素,允许且最多只有一个 null 元素.Java 中有三个常用的 Set 实现类: Hash ...
- SQL注入详解及技巧
Tip小技巧 :在白盒测试的过程中,在sql语句的下一句加上 echo $sql. '<br>'; 可以在页面中输出完整的sql语句 效果图 :
- 8天入门docker系列 —— 第八天 让程序跑在swarm集群上
真正的落地部署都是希望程序跑在集群下,而不是单机版下测测玩玩,所以这篇就来聊一下怎么使用docker swarm进行部署,因为是swarm是docker自带的, 所以部署起来还是非常简单的. 一:前置 ...
- spring 的权限控制:security
下面我们将实现关于Spring Security3的一系列教程. 最终的目标是整合Spring Security + Spring3MVC 完成类似于SpringSide3中mini-web的功能. ...
- python3 编译安装
前言: Linux下大部分系统默认自带python2.x的版本,最常见的是python2.6或python2.7版本,默认的python被系统很多程序所依赖,比如centos下的yum就是python ...
- Flask+APScheduler定时任务
1.安装依赖 pip install flask_apscheduler 2.使用定时任务 ```python from flask import Flask from flask_apschedul ...
- 浅谈单例模式及其java实现
单例模式是23种设计模式中比较简单的一种,在此聊一下单例模式. 1.什么是设计模式? 对于没有接触过设计模式的人来说,一听到设计模式这四个字就觉得这个东西很高深莫测,一下子就对这个东西产生了恐惧感,其 ...
- Extjs4 combobox autoLoad: true 时,加载两次
问题是这样的,combobox 远程加载数据时,当我们把 store 设置为 autoLoad: tue, 时,这样页面加载时,store 会load 一次,但是我们在第一次点击 下来框时,他还会 ...
- springMVC(一) --前端控制器(DispatcherServlet)的作用
SpringMVC是Spring中的模块,它实现了mvc设计模式的web框架,首先用户发出请求,请求到达SpringMVC的前端控制器(DispatcherServlet),前端控制器根据用户 ...