跳到正文

个人记录

P1251 餐巾计划问题 非投稿题解

Chiaro约 8 分钟
原文
文章信息与版本

文章信息

发布
2019年8月11日
收录
2025年12月12日
更新
2026年11月9日
篇幅
约 8 分钟

版本

  1. v2当前
  2. v1

P1251 餐巾计划问题

题目描述

请自行去看

输入输出格式

请自行去看

样例/提示/说明

请自行去看


这道题看标签 显然是一道网络流题

也显然是一道最小费用最大流的题 , 不知道费用流去看看模板吧…

既然是一道网络流题 , 那就只讲建图


这道题听qc神仙说是上下界网络流 , 当然我也不知道那是啥

正常建图经过简单尝试后发现似乎不太可做 , 所以反过来想

但是好像有正着建图的方法


很显然能想到 , 这题需要拆点 , 我一开始甚至想拆餐巾 , 但是并没有做出来 , 所以就来拆 天

一天早晨进毛巾 , 晚上出毛巾 , 那么就想当然的可以把天拆成早和晚 , 分别进毛巾和出毛巾

但是我还是不会做…

那么反过来想 , 晚上其实是本有毛巾 , 那是不是就可以从原点( 脏/净 毛巾发售商)获得每天需要的毛巾数量的脏毛巾

而从早晨出去到汇点连边 , 流量也是毛巾需求量 , 可以代表用了这么多的干净毛巾然后变成了脏毛巾进了汇点

这样是两排点了 , 当然每条边花费是0 , 但是早晨的点没有入度

每天早晨 , 餐厅可以买进无限数量的毛巾 , 那么就可以从源点连向早晨点 , 流量无限 , 花费是p

再考虑洗餐巾 , 快洗慢洗需要时间 , 所以如果第ii天洗毛巾需要k天 , 那么就可以把晚上ii点和早晨 i+ki+ k点连边 , 流量无限 , 花费也是题目给出的

又因为脏毛巾可以留给下一天等待解决 , 干净毛巾也可以考虑去留给下一天用 , 所以ii点和i+1i+1点也可以连流量无限花费0的边


这样就建完图了 , 然后一遍spfaspfa最小费用流就过了

别忘了开long longlong\ long


C++
#include<cmath>
#include<queue>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define APART puts("----------------------")
#define debug 1
#define inf 100010
#define int long long
#define ha 998244353
#define INF 0x7fffffff
#define INF_T 9223372036854775807
#define DEBUG printf("%s %d\n",__FUNCTION__,__LINE__)
using namespace std;

namespace chino{

inline int read(){
	char c = getchar(), up = c; int num = 0;
	for(; c < '0' || c > '9'; up = c, c = getchar());
	for(; c >= '0' && c <= '9';
	num = (num << 3) + (num << 1) + (c ^ '0'), c = getchar());
	return  up == '-' ? -num : num;
}

int n;
int costP;
int S, T;
int cntE = -1;
int costMin, flowMax;
int fastDay, fastCost;
int slowDay, slowCost;
int need[inf];
int head[inf];
int cost[inf];
int flow[inf];
int vis[inf];
int fat[inf];
int last[inf];
struct Edge{
	int to;
	int flow;
	int cost;
	int next;
}e[inf << 1];
queue<int>Q;

inline void AddEdge(int from, int to, int flow, int cost){
	++cntE;
	e[cntE].to = to;
	e[cntE].flow = flow;
	e[cntE].cost = cost;
	e[cntE].next = head[from];
	head[from] = cntE;
	return;
}

inline int spfa(int s, int t){
	memset(flow, 0x7f, sizeof flow);
	memset(cost, 0x7f, sizeof cost);
	memset(vis, 0, sizeof vis);
	Q.push(s);
	vis[s] = 1;
	cost[s] = 0;
	fat[t] = -1;
	while(!Q.empty()){
		int x = Q.front();
		Q.pop();
		vis[x] = 0;
		for(int i = head[x]; i ^ -1; i = e[i].next){
			int y = e[i].to;
			if(e[i].flow && cost[y] > cost[x] + e[i].cost){
				cost[y] = cost[x] + e[i].cost;
				fat[y] = x;
				last[y] = i;
				flow[y] = min(flow[x], e[i].flow);
				if(vis[y] == 0)
					vis[y] = 1,
					Q.push(y);
			}
		}
	}
	return fat[t] ^ -1;
}

inline void work(int s, int t){
	int now = t;
	flowMax += flow[t];
	costMin += cost[t] * flow[t];
	while(now ^ s){
		e[last[now]].flow -= flow[t];
		e[last[now] ^ 1].flow += flow[t];
		now = fat[now];
	}
	return;
}

inline void costDinic(int s, int t){
	while(spfa(s, t))
		work(s, t);
	return;
}

inline int main(){
	memset(head, -1, sizeof head);
	n = read();
	for(int i = 1; i <= n; i++)
		need[i] = read();
	costP = read();
	fastDay = read();
	fastCost = read();
	slowDay = read();
	slowCost = read();
	S = n << 1 | 1;
	T = S + 1;
	for(int i = 1; i <= n; i++){
		AddEdge(S, i, need[i], 0);
		AddEdge(i, S, 0, 0);

		AddEdge(i + n, T, need[i], 0);
		AddEdge(T, i + n, 0, 0);

		AddEdge(S, i + n, INF >> 1, costP);
		AddEdge(i + n, S, 0, -costP);
	}
	for(int i = 1; i < n; i++){
		AddEdge(i, i + 1, INF >> 1, 0);
		AddEdge(i + 1, i, 0, 0);

		AddEdge(i + n, i + n + 1, INF >> 1, 0);
		AddEdge(i + n + 1, i + n, 0, 0);
	}
	for(int i = 1; i <= n; i++){
		if(i + fastDay <= n){
			AddEdge(i, i + fastDay + n, INF >> 1, fastCost);
			AddEdge(i + fastDay + n, i, 0, -fastCost);
		}
		if(i + slowDay <= n){
			AddEdge(i, i + slowDay + n, INF >> 1, slowCost);
			AddEdge(i + slowDay + n, i, 0, -slowCost);
		}
	}
	costDinic(S, T);
	printf("%lld\n", costMin);
	return 0;
}

}//namespace chino

signed main(){return chino::main();}
原文