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_queuetypedef 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<<17; const ll mos = 0x7FFFFFFF; //2147483647 const ll nmos = 0x80000000; //-2147483648 const int inf = 0x3f3f3f3f; const ll inff = 0x3f3f3f3f3f3f3f3f; //18 const int mod = 1e9+7; const double esp = 1e-8; 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=0;int f=0;char ch=getchar();while (ch<'0'||ch>'9') f|=(ch=='-'),ch=getchar();while (ch>='0'&&ch<='9') x=x*10+ch-'0',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 = 2009;struct E{int v,w,cost;int nxt;}edge[10*maxn*maxn];int n,gtot = 0;int head[10*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 = 0;edge[gtot].cost = -1*cost;edge[gtot].nxt = head[v];head[v] = gtot++;}int vis[maxn*10],dis[maxn*10],pre[maxn*10],path[maxn*10];bool spfa(int s,int t){memset(vis, 0, sizeof(vis));memset(dis, inf, sizeof(dis));memset(pre, -1, sizeof(pre));queue<int>que; que.push(s); vis[s] = 1;dis[s] = 0;while(!que.empty()){int u = que.front(); que.pop(); vis[u] = 0;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 > 0 && dis[v] > dis[u] + cost){dis[v] = dis[u] + cost;pre[v] = u; path[v] = i;if(vis[v] == 0){que.push(v);vis[v] = 1;}}}}return pre[t] != -1;}ll solve(int s,int t){ll flow = 0, cost = 0;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] ^ 1].w += f;}}return cost;}int main(){memset(head, -1, sizeof(head));scanf("%d", &n);int s = 0, t = n+n+1;for(int i=1; i<=n; i++) {int x;scanf("%d", &x);addedge(s, i+n, x, 0);addedge(i, t, x, 0);}int p,m,ff,nn,ss;scanf("%d%d%d%d%d", &p, &m, &ff, &nn, &ss);for(int i=1; i<=n; i++) addedge(s, i, inf, p);for(int i=1; i + m <=n; i++) addedge(i+n, i+m, inf, ff);for(int i=1; i + nn<=n; i++) addedge(i+n, i+nn, inf, ss);for(int i=1; i<n; i++) addedge(i+n, i+n+1, inf, 0);printf("%lld\n", solve(s, t));return 0; }
View Code
转载于:https://www.cnblogs.com/ckxkexing/p/10372291.html
P1251 餐巾计划问题 费用流相关推荐
- 洛谷P1251 餐巾计划问题 无汇源最小费用流
题目描述 一个餐厅在相继的 NN 天里,每天需用的餐巾数不尽相同.假设第 ii 天需要 r_iri块餐巾( i=1,2,...,N).餐厅可以购买新的餐巾,每块餐巾的费用为 pp 分;或者把旧餐巾送 ...
- 费用流 ---- P1251 餐巾计划问题[拆点](网络流24题)
题目链接 题目大意: 解题思路 将脏餐巾以f元/条送到快洗部,过m天后,干净餐巾送回来使用 将脏餐巾以s元/条送到慢洗部,过n天后,干净餐巾送回来使用 延期送洗(可能会出现之后餐巾需求过少,并不需要所 ...
- 洛谷 - P1251 餐巾计划问题(最小费用最大流+思维建边)
题目链接:点击查看 题目大意:给出n天每天所需要的新餐巾的数量,现在有多种方式可以获得新餐巾,问如何运营能使花费最少: 直接购买,花费为cost 将脏餐巾送到快洗部,需要洗t1天,花费为c1 将脏餐巾 ...
- jzoj4802-[GDOI2017模拟9.24]探险计划【费用流,拆点】
正题 题目大意 一个nnn行的不完全矩阵第iii行有m+i−1m+i-1m+i−1个格子,然后每个格子有危险度. 每次可以从(i,j)(i,j)(i,j)走到(i−1,j)(i-1,j)(i−1,j) ...
- 【图论】【网络流】费用流模型
费用流模型 费用流板子 费用流直接应用 运输问题 负载平衡问题 二分图最大匹配 分配问题 最大权不相交路径 数字梯形问题 费用流网格图模型 K取方格数 深海机器人问题 费用流拆点 餐巾计划问题 费用流 ...
- 餐巾计划问题 线性规划与网络流24题之10 费用流
相关知识:最小费用(最大)流 问题描述: 一个餐厅在相继的N 天里, 每天需用的餐巾数不尽相同. 假设第i天需要ri块餐巾(i=1, 2,-,N).餐厅可以购买新的餐巾,每块餐巾的费用为p分:或者把旧 ...
- 初识费用流 模板(spfa+slf优化) 餐巾计划问题
今天学习了最小费用最大流,是网络流算法之一.可以对于一个每条边有一个容量和一个费用(即每单位流的消耗)的图指定一个源点和汇点,求在从源点到汇点的流量最大的前提下的最小费用. 这里讲一种最基础也是最好掌 ...
- 【网络流24题】餐巾计划问题(最小费用最大流)
[网络流24题]餐巾计划问题(最小费用最大流) 题面 COGS 洛谷上的数据范围更大,而且要开longlong 题解 餐巾的来源分为两种: ①新买的 ②旧的拿去洗 所以,两种情况分别建图 先考虑第一种 ...
- P1251-餐巾计划问题【费用流】
正题 题目链接:https://www.luogu.com.cn/problem/P1251 题目大意 NNN天,第iii天需要aia_iai个餐巾. 每个餐巾价格为ppp,使用完后有两种清洗方法 ...
最新文章
- 解决packet tracer不能复制CLI内容的问题
- os.urandom(n)函数解释Python
- ERROR 2002 (HY000): Can‘t connect to local MySQL server through socket ‘/var/run/mysqld/mysqld.sock
- Unable to locate Attribute with the the given name [] on this ManagedType[com.XXX]
- OpenCV—形态学运算定义与实现
- Java 01背包【动态规划·蓝桥杯练习题】(相信杨超越,相信锦鲤,默默努力,其它的看天意)
- spring mvc静态资源访问的配置
- WPF实现截屏(仿微信)
- BZOJ3040:最短路——题解
- 计算机网络子网划分_子网划分和超网| 计算机网络
- mesh和wifi中继的区别_深度解读Mesh路由和无线中继的差异,谁才是性价比之选?...
- 简单粗暴入门java之数组概述
- CISA 发布关于 Treck TCP/IP 栈中新漏洞的 ICS 安全公告
- 惠普暗影精灵3清灰_惠普暗影精灵15评测:速度超快,价格适中|但问题却不少...
- Chrome、FireFox浏览器新标签页打开搜索和书签
- matlab中各种数据类型及转换
- 爬取豆瓣评论之——后来的我们
- 中国一共有多少个神仙?
- 国企计算机技术岗都干什么,大家听说的国企技术岗都是什么样子的?
- 在VC中用GDI+绘制角度可变的颜色渐变效果-.NET教程,VB.Net语言[转]
热门文章
- 中国矿业大学python期末考试_中国矿业大学一体化网络校园网——CUMTDDNet-DrCOM网页版认证教程...
- 【PYTHON笔记】:文件打开和关闭
- ajax post提交数据_第三十五天JavaScript中的ajax
- linux登录指令 pgsql_一句一例解读20条Linux常用指令,学会了你就入门了
- android api接口封装,android-apidesigner是一个网络接口封装工具
- JSP中9个隐含对象
- 【CCCC】L3-004 肿瘤诊断 (30分),三维BFS
- 【PAT乙】1001 害死人不偿命的(3n+1)猜想 (15分) 模拟,水水更健康
- 计算机系统及原理pdf,计算机系统组成及工作原理题目.pdf
- linux deploy下载地址,Linux部署 Linux Deploy