「网络流 24 题」餐巾计划

这道题目很关键的一点就是,要明白用的餐巾和洗的餐巾是两个系统。

所以我们把每一天所用餐巾直接用一个源点和汇点连接。解决第一个系统。

此时可能会洗餐巾,所以我们另起一个点,从源点每一天引出脏的餐巾,数目依然是当天所用的餐巾数量,然后从这个点连接到m天后的第一系统的点和n天后第一系统的点,费用分别为f和s,流量为无穷大。当天的餐巾就算不洗也可以留下来,所以我们还需要把这些餐巾连接起来,当天不洗的餐巾连一条线到下一天不洗的餐巾处,流量为无穷大,费用为0.第二个系统就是辅助第一个系统的。

代码如下:

#include<iostream>
#include<cstdio>
#include<vector>
#include<queue>
#include<utility>
#include<stack>
#include<algorithm>
#include<cstring>
#include<string>
#include<cmath>
#include<set>
#include<map>
using namespace std;
const int INF = 0x3f3f3f3f;
const int maxn = 2500 + 5;
const int maxm = 10000 + 5;
int N;
int head[maxn], to[maxm], front[maxm], flow[maxm], cost[maxm], ppp;
int dis[maxn], minflow[maxn];
bool flag[maxn];
pair<int, int> par[maxn];

struct MIN_COST_MAX_FLOW{
	
	void init() {
		memset(head, -1, sizeof(head));
		ppp = 0;
	}
	
	bool spfa(int s, int t) {
		int u, v;
		fill(dis, dis + t + 1, INF);
		memset(flag, 0, sizeof(flag));
		dis[s] = 0;
		minflow[s] = INF;
		queue <int> q;
		q.push(s);
		while(!q.empty()) {
			u = q.front();
			q.pop();
			flag[u] = 0;
			for(int i = head[u]; ~i; i = front[i]) {
				v = to[i];
				if(flow[i] && dis[v] > dis[u] + cost[i]) {
					dis[v] = dis[u] + cost[i];
					par[v] = (make_pair(u, i));
					minflow[v] = min(minflow[u], flow[i]);
					if(!flag[v]) {
						flag[v] = 1;
						q.push(v);
					}
				}
			}
		}
		if(dis[t] == INF)
			return 0;
		return 1;
	}
	
	int slove(int s, int t) {
		int ans = 0, p;
		while(spfa(s, t)) {
			p = t;
			while(p != s) {
				flow[par[p].second] -= minflow[t];
				flow[par[p].second^1] += minflow[t];
				p = par[p].first;
			}
			ans += dis[t] * minflow[t];
		}
		return ans;
	}
	
	void add_edge(int u, int v, int f, int c) {
		to[ppp] = v, front[ppp] = head[u], flow[ppp] = f, cost[ppp] = c, head[u] = ppp++;
		to[ppp] = u, front[ppp] = head[v], flow[ppp] = 0, cost[ppp] = -c, head[v] = ppp++;
	}
}mcmf;

int main() {
#ifndef ONLINE_JUDGE
	freopen("in.txt", "r", stdin);
//    freopen("out.txt", "w", stdout);
#endif
	int p, m, f, n, s;
	mcmf.init();
	scanf("%d%d%d%d%d%d", &N, &p, &m, &f, &n, &s);
	int start = 2 * N, tank = start + 1;
	for(int i = 0; i < N; i++) {
		int tmp, a = i * 2, b = a + 1;
		scanf("%d", &tmp);
		mcmf.add_edge(start, a, tmp, p);
		mcmf.add_edge(a, b, tmp, 0);
		mcmf.add_edge(b, tank, tmp, 0);
		if(i + m < N)
			mcmf.add_edge(b, (i + m) * 2, INF, f);
		if(i + n < N)
			mcmf.add_edge(b, (i + n) * 2, INF, s);
		if(i + 1 < N)
			mcmf.add_edge(a, a + 2, INF, 0);
	}
	int ans = mcmf.slove(start, tank);
	printf("%d\n", ans);
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值