[luogu_P1251][LOJ#6008]「网络流 24 题」餐巾计划

[luogu_P1251][LOJ#6008]「网络流 24 题」餐巾计划

试题描述

一个餐厅在相继的 (N) 天里,第 (i) 天需要 (R_i) 块餐巾 ((i=l,2,…,N))。餐厅可以从三种途径获得餐巾。

(1)购买新的餐巾,每块需 (p) 分;

(2)把用过的餐巾送到快洗部,洗一块需 (m) 天,费用需 (f)((f<p))。如 (m=1) 时,第一天送到快洗部的餐巾第二天就可以使用了,送慢洗的情况也如此。

(3)把餐巾送到慢洗部,洗一块需 (n)((n>m)),费用需 (s)((s<f))

在每天结束时,餐厅必须决定多少块用过的餐巾送到快洗部,多少块送慢洗部。在每天开始时,餐厅必须决定是否购买新餐巾及多少,使洗好的和新购的餐巾之和满足当天的需求量(R_i),并使 (N) 天总的费用最小

输入

输入文件共 (3) 行,第 (1) 行为总天数;第 (2) 行为每天所需的餐巾块数;第 (3) 行为每块餐巾的新购费用 (p),快洗所需天数 (m),快洗所需费用 (f),慢洗所需天数 (n),慢洗所需费用 (s)

输出

输出文件共 (1) 行为最小的费用。

输入示例

3
3 2 4
10 1 6 2 3

输出示例

64

数据规模及约定

(N le 2000)

(R_i le 10000000)

(p,f,s le 10000)

题解

建图显然,按照题意叙述即可,跑的是边有下界最小费用可行流。

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <algorithm>
#include <queue>
using namespace std;
#define rep(i, s, t) for(int i = (s); i <= (t); i++)
#define dwn(i, s, t) for(int i = (s); i >= (t); i--)

int read() {
	int x = 0, f = 1; char c = getchar();
	while(!isdigit(c)){ if(c == '-') f = -1; c = getchar(); }
	while(isdigit(c)){ x = x * 10 + c - '0'; c = getchar(); }
	return x * f;
}

#define maxn 4010
#define maxm 32010
#define ool (1ll << 60)
#define LL long long

struct Edge {
	int from, to;
	LL flow, cost;
	Edge() {}
	Edge(int _1, int _2, LL _3, LL _4): from(_1), to(_2), flow(_3), cost(_4) {}
};
struct ZKW {
	int n, m, s, t, head[maxn], nxt[maxm];
	LL cost, ans;
	Edge es[maxm];
	LL d[maxn];
	deque <int> Q;
	bool inq[maxn];
	bool vis[maxn];
	
	void init() {
		m = 0; memset(head, -1, sizeof(head));
		return ;
	}
	void setn(int _) {
		n = _;
		return ;
	}
	
	void AddEdge(int a, int b, LL c, LL d) {
		es[m] = Edge(a, b, c, d); nxt[m] = head[a]; head[a] = m++;
		es[m] = Edge(b, a, 0, -d); nxt[m] = head[b]; head[b] = m++;
		return ;
	}
	
	bool BFS() {
		rep(i, 1, n) d[i] = ool;
		d[t] = 0;
		Q.push_back(t); inq[t] = 1;
		while(!Q.empty()) {
			int u = Q.front(); Q.pop_front(); inq[u] = 0;
			for(int i = head[u]; i != -1; i = nxt[i]) {
				Edge& e = es[i^1];
				if(d[e.from] > d[u] + e.cost && e.flow) {
					d[e.from] = d[u] + e.cost;
					if(!inq[e.from]) {
						inq[e.from] = 1;
						if(Q.empty() || d[e.from] <= d[Q.front()]) Q.push_front(e.from);
						else Q.push_back(e.from);
					}
				}
			}
		}
		if(d[s] == ool) return 0;
		cost = d[s];
		return 1;
	}
	
	LL DFS(int u, LL a) {
		if(u == t || !a) return ans += cost * a, a;
		if(vis[u]) return 0;
		vis[u] = 1;
		LL flow = 0, f;
		for(int i = head[u]; i != -1; i = nxt[i]) {
			Edge& e = es[i];
			if(d[e.to] == d[u] - e.cost && (f = DFS(e.to, min(a, e.flow)))) {
				flow += f; a -= f;
				e.flow -= f; es[i^1].flow += f;
				if(!a) return flow;
			}
		}
		return flow;
	}
	
	LL MaxFlow(int _s, int _t) {
		s = _s; t = _t;
		LL flow = 0, f;
		while(BFS())
			do {
				memset(vis, 0, sizeof(vis));
				f = DFS(s, ool);
				flow += f;
			} while(f);
		return flow;
	}
} sol;

int main() {
	int n = read(), S = (n << 1) + 1, T = (n << 1) + 2, SS = (n << 1) + 3, TT = (n << 1) + 4;
	sol.init(); sol.setn(TT);
	sol.AddEdge(T, S, ool, 0);
	rep(i, 1, n) {
		sol.AddEdge(i + n, T, ool, 0);
		int r = read();
		sol.AddEdge(i, i + n, ool - r, 0);
		sol.AddEdge(i, TT, r, 0);
		sol.AddEdge(SS, i + n, r, 0);
	}
	int price = read(), fastt = read(), fastc = read(), slowt = read(), slowc = read();
	rep(i, 1, n) {
		sol.AddEdge(S, i, ool, price);
		if(i + fastt <= n) sol.AddEdge(i + n, i + fastt, ool, fastc);
		if(i + slowt <= n) sol.AddEdge(i + n, i + slowt, ool, slowc);
		if(i < n) sol.AddEdge(i, i + 1, ool, 0);
	}
	
	sol.MaxFlow(SS, TT);
	printf("%lld
", sol.ans);
	
	return 0;
}

注意,LOJ 上输入顺序不太一样。

原文地址:https://www.cnblogs.com/xiao-ju-ruo-xjr/p/7912169.html