当前位置: 首页 > news >正文

[swust1745] 餐巾计划问题(费用流)

题目链接:https://www.oj.swust.edu.cn/problem/show/1745

这题比较抽象,信息量也比较大。但是仔细想一下就会发现,按照天来作为节点,可以有三种操作:

当天使用的毛巾可以:1买,2从前几天中的洗完获得,洗有两种,快洗和慢洗。

当天使用完的毛巾可以:1留到后一天处理,2洗,洗有两种,同上。

将日期的节点拆成两个,一个用来表示脏的(不能直接用的)毛巾总量状态,一个表示干净的(能使用)毛巾总量状态。

建图方式如图,可以知道能量是守恒的,因为毛巾总量是一定的,并且符合要求。

 

毛巾需求量是一定的,所以从源点到脏的集合里建边,容量为需求量,费用为0。

同理汇点。

洗毛巾可以快洗慢洗(花费是总价,而不是单价,所以很方便),那么洗完了就是干净的,所以从脏的点集里延期n天或者m天连接到干净的点集里,容量为inf,费用为n或m。

脏毛巾可以屯一天到后面,不过直接相邻的传递就行,因为下一天会仍然有一条边传递到下下天,所以在脏的点集里建边,容量为inf,费用为0,

新买的是干净的,所以直接插到干净的点集里,从源点到干净点集建边,容量为inf,费用为p。

 

  1 #include <bits/stdc++.h>
  2 using namespace std;
  3 
  4 typedef long long LL;
  5 typedef struct Node {
  6     int u, v, next;
  7     LL c, w;
  8 }Node;
  9 const int maxn = 10010;
 10 const int maxm = maxn << 5;
 11 const LL mod = 0x3f3f3f3fLL;
 12 const LL inf = (1LL<<55);
 13 int tot, head[maxn];
 14 LL dist[maxn];
 15 LL cost, flow;
 16 Node e[maxm];
 17 int pre[maxn];
 18 bool visit[maxn];
 19 queue<int> Q;
 20 int S, T, N;
 21 
 22 void init() {
 23     S = T = N = 0;
 24     memset(head, -1, sizeof(head));
 25     tot = 0;
 26 }
 27 
 28 void adde(int u, int v, LL c, LL w) {
 29     e[tot].u = u; e[tot].v = v; e[tot].c = c; e[tot].w = w; e[tot].next = head[u]; head[u] = tot++;
 30     e[tot].u = v; e[tot].v = u; e[tot].c = 0; e[tot].w = -w; e[tot].next = head[v]; head[v] = tot++;
 31 }
 32 bool spfa(int s, int t, int n) {
 33     int i;
 34     for(i = 0; i <= n; i++) {
 35         dist[i] = inf;
 36         visit[i] = 0;
 37         pre[i] = -1;
 38     }
 39     while(!Q.empty()) Q.pop();
 40     Q.push(s);
 41     visit[s] = true;
 42     dist[s] = 0;
 43     pre[s] = -1;
 44     while(!Q.empty()) {
 45         int u = Q.front();
 46         visit[u] = false;
 47         Q.pop();
 48         for(int j = head[u]; j != -1; j = e[j].next) {
 49             if(e[j].c > 0 && dist[u] + e[j].w < dist[e[j].v]) {
 50                 dist[e[j].v] = dist[u] + e[j].w;
 51                 pre[e[j].v] = j;
 52                 if(!visit[e[j].v]) {
 53                     Q.push(e[j].v);
 54                     visit[e[j].v] = true;
 55                 }
 56             }
 57         }
 58     }
 59     if(dist[t] == inf) return false;
 60     else return true;
 61 }
 62 LL ChangeFlow(int t) {
 63     LL det = mod;
 64     int u = t;
 65     while(~pre[u]) {
 66         u = pre[u];
 67         det = min(det, e[u].c);
 68         u = e[u].u;
 69     }
 70     u = t;
 71     while(~pre[u]) {
 72         u = pre[u];
 73         e[u].c -= det;
 74         e[u ^ 1].c += det;
 75         u = e[u].u;
 76     }
 77     return det;
 78 }
 79 LL MinCostFlow(int s, int t, int n) {
 80     LL mincost, maxflow;
 81     mincost = maxflow = 0;
 82     while(spfa(s, t, n)) {
 83         LL det = ChangeFlow(t);
 84         mincost += det * dist[t];
 85         maxflow += det;
 86     }
 87     cost = mincost;
 88     flow = maxflow;
 89     return mincost;
 90 }
 91 
 92 int day,p,m,f,n,s;
 93 int j, need[maxn];
 94 
 95 int main() {
 96     // freopen("in", "r", stdin);
 97     while(~scanf("%d%d%d%d%d%d",&day,&p,&m,&f,&n,&s)) {
 98         init();
 99         S = 0, T = day * 2 + 1, N = T + 1;
100         for(int i = 1; i <= day; i++) scanf("%d", &need[i]);
101 
102         for(int i = 1; i < day; i++) adde(i, i+1, inf, 0LL);
103 
104         for(int i = 1; i <= day; i++) adde(S, i, (LL)need[i], 0LL);
105         for(int i = 1; i <= day; i++) adde(day+i, T, (LL)need[i], 0LL);
106 
107         for(int i = 1; i <= day; i++) adde(S, day+i, inf, (LL)p);
108 
109         for(int i = 1; i <= day; i++) {
110             j = i + m; if(j <= day) adde(i, day+j, inf, (LL)f);
111             j = i + n; if(j <= day) adde(i, day+j, inf, (LL)s);
112         }
113 
114         cout << MinCostFlow(S, T, N) << endl;
115     }
116     return 0;
117 }

 

转载于:https://www.cnblogs.com/kirai/p/6831049.html

相关文章:

  • 详尽Netty(二):源码环境搭建
  • AssetBundle管理机制(上)
  • 详尽Netty(三):Channel
  • 循序渐进之Spring AOP(5) - 创建切面
  • 牛逼!阿里推出国产开源的jdk! 快来试试吧!
  • ES6 中的let 声明变量
  • 原来Java类的加载过程是这样的?
  • 淘宝数据库OceanBase SQL编译器部分 源代码阅读--生成物理查询计划
  • 聊聊jvm几种垃圾收集器
  • 搭建 webpack + React 开发环境
  • jvm垃圾回收的过程
  • 到底什么是分布式锁,进程锁,线程锁
  • 晶振參数校定
  • 这样做能让nginx新能提升10倍
  • 查看项目错误信息
  • 【跃迁之路】【519天】程序员高效学习方法论探索系列(实验阶段276-2018.07.09)...
  • Docker下部署自己的LNMP工作环境
  • express.js的介绍及使用
  • FastReport在线报表设计器工作原理
  • Netty 4.1 源代码学习:线程模型
  • 从 Android Sample ApiDemos 中学习 android.animation API 的用法
  • 官方解决所有 npm 全局安装权限问题
  • 如何邀请好友注册您的网站(模拟百度网盘)
  • 一些css基础学习笔记
  • 《TCP IP 详解卷1:协议》阅读笔记 - 第六章
  • ​queue --- 一个同步的队列类​
  • # MySQL server 层和存储引擎层是怎么交互数据的?
  • #大学#套接字
  • #考研#计算机文化知识1(局域网及网络互联)
  • (function(){})()的分步解析
  • (二)JAVA使用POI操作excel
  • (二)正点原子I.MX6ULL u-boot移植
  • (十八)devops持续集成开发——使用docker安装部署jenkins流水线服务
  • (实战)静默dbca安装创建数据库 --参数说明+举例
  • (原創) 未来三学期想要修的课 (日記)
  • (转)3D模板阴影原理
  • **python多态
  • .NET Core6.0 MVC+layui+SqlSugar 简单增删改查
  • .Net 垃圾回收机制原理(二)
  • .net 流——流的类型体系简单介绍
  • .netcore 如何获取系统中所有session_如何把百度推广中获取的线索(基木鱼,电话,百度商桥等)同步到企业微信或者企业CRM等企业营销系统中...
  • .NET框架类在ASP.NET中的使用(2) ——QA
  • .pop ----remove 删除
  • /proc/interrupts 和 /proc/stat 查看中断的情况
  • [Assignment] C++1
  • [BT]BUUCTF刷题第4天(3.22)
  • [BUG]Datax写入数据到psql报不能序列化特殊字符
  • [BZOJ4016][FJOI2014]最短路径树问题
  • [codevs] 1029 遍历问题
  • [C语言][C++][时间复杂度详解分析]二分查找——杨氏矩阵查找数字详解!!!
  • [Editor]Unity Editor类常用方法
  • [EFI]MSI GF63 Thin 9SCXR电脑 Hackintosh 黑苹果efi引导文件
  • [emacs] CUA的矩形块操作很给力啊
  • [ios] IOS文件操作的两种方式:NSFileManager操作和流操作【转】
  • [LeetCode] Binary Tree Preorder Traversal 二叉树的先序遍历