ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

餐巾计划问题

2021-07-29 22:32:22  阅读:196  来源: 互联网

标签:int 餐巾 inq tot 问题 计划 incf dis


题面

餐巾计划问题

题解

隐式图问题。
我们考虑建立分层图,那么每层的状态即为时间,将每天拆成两个点,分别表示早上和晚上。

  1. 从源点向表示晚上的点连流量为当天所用餐巾数 \(r_i\),费用为 \(0\) 的边,表示每天晚上得到 \(r_i\) 条脏餐巾。
  2. 从表示早上的点向汇点连流量为当天所用餐巾数 \(r_i\),费用为 \(0\) 的边,表示每天早上用了 \(r_i\) 条餐巾,流满证明够用。
  3. 从每一天晚上向第二天晚上练流量为 \(inf\), 费用为 \(0\) 的边,表示把脏餐巾留到下一天晚上,因为不能留给下一天早上,所以不能向下一天早上连。
  4. 从每一天晚上向快洗时间之后的早上连流量为 \(inf\), 费用为快洗费用的边,表示把脏餐巾送去快洗,洗完之后的那天早上就能用了。
  5. 慢洗同理。
  6. 从源点向每天早上连流量为 \(inf\), 费用为购买餐巾费用的边,表示早上购买新餐巾。
    然后跑最小费用最大流就好了,最大流保证了餐巾够用,最小费用保证了花费最少。

代码

#include<cstdio>
#include<queue>
#include<iostream>
#include<cstring>

using namespace std;

typedef long long LL;

const int N = 4e3 + 5, M = 1e5 + 5;
const LL inf = 1e17;

int head[N], nex[M], to[M], c[M], n, tot = 1;
int S, T, t1, t2, w1, w2, p; LL maxflow = 0, ans = 0, w[M];

inline void add(int u, int v, int k, LL f) {
	nex[++tot] = head[u]; to[tot] = v; w[tot] = f; c[tot] = k; head[u] = tot;
	nex[++tot] = head[v]; to[tot] = u; w[tot] = 0; c[tot] = -k; head[v] = tot;
}

namespace Edmonds_Karp {
	int pre[N]; LL dis[N], incf[N]; bool inq[N];
	queue < int > q;
	inline bool SPFA() {
		for(int i = 0; i < N; i++) dis[i] = inf;
		memset(inq, false, sizeof inq);
		dis[S] = 0; q.push(S); inq[S] = true; incf[S] = inf;
		while(!q.empty()) {
			int x = q.front(); q.pop();
			inq[x] = false;
			for(int i = head[x]; i; i = nex[i]) {
				if(!w[i]) continue;
				if(dis[to[i]] > dis[x] + c[i]) {
					dis[to[i]] = dis[x] + c[i];
					incf[to[i]] = min(incf[x], w[i]);
					pre[to[i]] = i;
					if(!inq[to[i]]) q.push(to[i]), inq[to[i]] = true;
				}
			}
		}
		return dis[T] < inf;
	}
	inline void update() {
		int x = T;
		while(x != S) {
			int i = pre[x];
			w[i] -= incf[T], w[i ^ 1] += incf[T];
			x = to[i ^ 1];
		}
		maxflow += incf[T];
		ans += dis[T] * incf[T];
	}
}

using namespace Edmonds_Karp;

int main() {
	scanf("%d", &n);
	S = 0; T = 2 * n + 1;
	for(int i = 1, x; i <= n; i++) scanf("%d", &x), add(S, i + n, 0, x), add(i, T, 0, x);
	scanf("%d%d%d%d%d", &p, &t1, &w1, &t2, &w2);
	for(int i = 1; i <= n; i++) {
		add(S, i, p, inf);
		if(i + t1 <= n) add(i + n, i + t1, w1, inf);
		if(i + t2 <= n) add(i + n, i + t2, w2, inf);
		if(i < n) add(i + n, i + 1 + n, 0, inf);
	}
	while(SPFA()) update();
	printf("%lld\n", ans);
	return 0;
}

标签:int,餐巾,inq,tot,问题,计划,incf,dis
来源: https://www.cnblogs.com/sjzyh/p/15077423.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有