Luo Gu · [24 network flow problem] napkin plan issues

Ann ~ Gugu Gu began to take so long to finally start writing network stream of it. Here is the Portal: Luo Gu P1251 napkins plan issues

answer

The problem with the Los Valley P2223 almost exactly the same, except for [a few days] this boundary distinct logical slightly.

More obvious is the minimum cost flow . First, the approximate boundaries we can build out: from S to the point of even everyday side, and from the point connected to the T side every day, and the number of capacity use are the days of paper towels. Then we would not even buy new representatives, coming from a quick wash and wash towels over from the slow side, capacity is INF, cost is its own costs.

Now we refined. Because respectively connected to be out of the S and T, and the respective points have internal flow dominance relation, we split the day into two points, the first point to send a napkin to wash, connected to S, the second point for receiving a tissue quick wash and slow wash from buy a new, connected with T. In this way we would be very clear to the establishment of a diagram -

S is connected to the first day of the i-th point, and to provide and the second point of the i + m i + n and napkins for days, even to the point of the second T. And S is connected to a napkin representing the new second point for later on day i. This looks to be lost, but you will find you a WA. Because sometimes you obviously do not have to buy one, but you buy. For example, when m = 2, the first point to wash the napkins on the third day, but not so much by the third day of napkins, so Tuen in there , that is the fifth day of the fourth day all be used this napkin . So we have a uniform edge for each i to i + 1. The second point is the first point i connected to a first point i + 1 or i is connected to the second point i + 1 are possible, represents a deferred sent wash, a wash expressed extension with.

Code on it -

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<queue>
#define maxn 5000
#define maxm 50000
using namespace std;
typedef long long ll;
const int INF = 0x3f3f3f3f;
int read() {
	int x = 0, f = 1, ch = getchar();
	while(!isdigit(ch)) {if(ch == '-') f = -1; ch = getchar();}
	while(isdigit(ch)) x = (x << 1) + (x << 3) + ch - '0', ch = getchar();
	return x * f;
}

struct edge {int to, w, f, nxt;} e[maxm];
int head[maxn], k = 0;
void add(int u, int v, int w, int f) {
	e[k] = {v, w, f, head[u]}; head[u] = k++;
	e[k] = {u, 0, -f, head[v]}; head[v] = k++;
} 

int n, A, B, fa, fb, F, r[maxn];
int S, T;
int dis[maxn], pre[maxn], incf[maxn];
bool vis[maxn];
bool spfa() {
	memset(dis, 0x3f, sizeof dis); dis[S] = 0;
	memset(vis, 0, sizeof vis); vis[S] = true;
	queue<int> q; incf[S] = INF; q.push(S);
	while(q.size()) {
		register int u = q.front(), v; q.pop(); vis[u] = false;
		for(int i = head[u]; ~i; i = e[i].nxt) {
			v = e[i].to; if(e[i].w && dis[u] + e[i].f < dis[v]) {
				dis[v] = dis[u] + e[i].f;
				incf[v] = min(incf[u], e[i].w); pre[v] = i;
				if(!vis[v]) q.push(v), vis[v] = true;
			}
		}
	}
	if(dis[T] == INF) return false; return true;
}

ll ans = 0;
void update() {
	register int u = T, i;
	while(u != S) {
		i = pre[u];
		e[i].w -= incf[T], e[i ^ 1].w += incf[T];
		u = e[i ^ 1].to;
	}
	ans += 1ll * incf[T] * dis[T];
}//上面都是最小费用流基本操作

signed main() {
	n = read(); memset(head, -1, sizeof head);
	for(int i = 1; i <= n; i++) r[i] = read();
	F = read(), A = read(), fa = read(), B = read(), fb = read();
	
	S = 0, T = n * 2 + 1;
	for(int i = 1; i <= n; i++) {
		add(S, i, r[i], 0), add(i + n, T, r[i], 0);//如题解所言建图
		add(S, i + n, INF, F);
		if(i + A <= n) add(i, i + A + n, INF, fa);
		if(i + B <= n) add(i, i + B + n, INF, fb);
		if(i < n) add(i , i + 1, INF, 0);
	}
	
	while(spfa()) update();
	printf("%lld\n", ans);
	return 0;
}

Meet the assessment
:) --End--

发布了158 篇原创文章 · 获赞 23 · 访问量 2万+

Guess you like

Origin blog.csdn.net/qq_43326267/article/details/104188773