【费用流】 COGS 461. [网络流24题] 餐巾

很简单的费用流。。。。拆点就行

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;

const int maxn = 405;
const int maxm = 400005;
const int INF = 0x3f3f3f3f;

struct Edge
{
	int v, c, w, next;
	Edge () {}
	Edge(int v, int c, int w, int next) : v(v), c(c), w(w), next(next) {}
}E[maxm];

set<int> ss[maxn];
queue<int> q;
int H[maxn], cntE;
int visit[maxn];
int cap[maxn];
int vis[maxn];
int dis[maxn];
int cur[maxn];
int flow, cost, s, t, T;

void addedges(int u, int v, int c, int w)
{
	E[cntE] = Edge(v, c, w, H[u]);
	H[u] = cntE++;
	E[cntE] = Edge(u, 0, -w, H[v]);
	H[v] = cntE++;
}

bool spfa()
{
	memset(dis, INF, sizeof dis);
	cur[s] = -1;
	vis[s] = ++T;
	cap[s] = INF;
	dis[s] = 0;
	q.push(s);
	while(!q.empty()) {
		int u = q.front();
		q.pop();
		vis[u] = T - 1;
		for(int e = H[u]; ~e; e = E[e].next) {
			int v = E[e].v, c = E[e].c, w = E[e].w;
			if(c && dis[v] > dis[u] + w) {
				dis[v] = dis[u] + w;
				cap[v] = min(cap[u], c);
				cur[v] = e;
				if(vis[v] != T) {
					vis[v] = T;
					q.push(v);
				}
			}
		}
	}
	if(dis[t] == INF) return false;
	cost += cap[t] * dis[t];
	flow += cap[t];
	for(int e = cur[t]; ~e; e = cur[E[e ^ 1].v]) {
		E[e].c -= cap[t];
		E[e ^ 1].c += cap[t];
	}
	return true;
}

int mfmc()
{
	flow = cost = 0;
	while(spfa());
	return cost;
}

void init()
{
	cntE = T = 0;
	memset(H, -1, sizeof H);
	memset(vis, 0, sizeof vis);
}

int a[maxn];

void work(int N)
{
	int p, n, m, ss, f;
	s = 0, t = 2 * N + 1;
	for(int i = 1; i <= N; i++) scanf("%d", &a[i]);
	scanf("%d%d%d%d%d", &p, &m, &f, &n, &ss);
	
	for(int i = 1; i <= N; i++) {
		addedges(s, i, a[i], 0);
		addedges(s, i + N, a[i], p);
		addedges(i + N, t, a[i], 0);
	}
	
	for(int i = 1; i <= N; i++) {
		for(int j = i + m; j <= N; j++)
			addedges(i, j + N, a[i], f);
		for(int j = i + n; j <= N; j++)
			addedges(i, j + N, a[i], ss);
	}
	printf("%d\n", mfmc());
}

int main()
{
	freopen("napkin.in", "r", stdin);
	freopen("napkin.out", "w", stdout);
	int N;
	while(scanf("%d", &N) != EOF) {
		init();
		work(N);
	}
	
	
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值