BZOJ1391: [Ceoi2008]order

1391: [Ceoi2008]order

Time Limit: 10 Sec Memory Limit: 64 MB
Submit: 2053 Solved: 619
[Submit][Status][Discuss]

Description

有N个工作,M种机器,每种机器你可以租或者买过来. 每个工作包括若干道工序,每道工序需要某种机器来完成,你可以通过购买或租用机器来完成。 现在给出这些参数,求最大利润

Input

第一行给出 N,M(1<=N<=1200,1<=M<=1200) 下面将有N块数据,每块数据第一行给出完成这个任务能赚到的钱(其在[1,5000])及有多少道工序 接下来若干行每行两个数,分别描述完成工序所需要的机器编号及租用它的费用(其在[1,20000]) 最后M行,每行给出购买机器的费用(其在[1,20000])

Output

最大利润

Sample Input

2 3

100 2

1 30

2 20

100 2

1 40

3 80

50

80

110

Sample Output

50

题解

本来想的费用流。
网上题解全是最小割。
看了看数据范围。。。
。。。
裸的最小割
dinic当前弧优化,试一试
经过一番卡常后终于过了

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <map>
#include <cmath>
inline int max(int a, int b){return a > b ? a : b;}
inline int min(int a, int b){return a < b ? a : b;}
inline int abs(int x){return x < 0 ? -x : x;}
inline void swap(int &x, int &y){int tmp = x;x = y;y = tmp;}
inline void read(int &x)
{
    x = 0;char ch = getchar(), c = ch;
    while(ch < '0' || ch > '9') c = ch, ch = getchar();
    while(ch <= '9' && ch >= '0') x = x * 10 + ch - '0', ch = getchar();
    if(c == '-') x = -x;
}
const int INF = 0x3f3f3f3f;
struct Edge
{
	int u,v,w,nxt;
	Edge(int _u, int _v, int _w, int _nxt){u = _u;v = _v;w = _w;nxt = _nxt;}
	Edge(){}
}edge[3000010];
int head[2510], cnt = 1, S, T, cur[2510], q[2510], he, ta, h[2510], ans;
inline void insert(int a, int b, int c)
{
	edge[++ cnt] = Edge(a, b, c, head[a]), head[a] = cnt;
	edge[++ cnt] = Edge(b, a, 0, head[b]), head[b] = cnt;
}
bool bfs()
{
	memset(h, -1, sizeof(h)), h[S] = 0, he = ta = 0, q[ta ++] = S;
	while(he < ta)
	{
		register int now = q[he ++];
		for(register int pos = head[now];pos;pos = edge[pos].nxt)
		{
			register int v = edge[pos].v;
			if(edge[pos].w && h[v] == -1)
				h[v] = h[now] + 1, q[ta ++] = v;
		}
	}
	return h[T] != -1;
}
int dfs(int x, int f)
{
	if(x == T || f == 0) return f;
	int used = 0, w;
	for(register int& pos = cur[x];pos;pos = edge[pos].nxt)
	{
		register int v = edge[pos].v;
		if(h[v] == h[x] + 1 && edge[pos].w > 0)
		{
			w = dfs(v, min(edge[pos].w, f - used));
			if(w > 0)
				edge[pos].w -= w, edge[pos ^ 1].w += w, used += w;
			if(used == f) return f;
		}
	}
	if(!used) h[x] = -1;
	return used;
}
void dinic()
{
	while(bfs()) 
	{
		memcpy(cur, head, sizeof(head));
		ans += dfs(S, INF);
	}
}
int n, m, sum;
int main()
{
	read(n), read(m);S = n + m + 1, T = S + 1;
	register int tmp1, tmp2, tmp3;
	for(register int i = 1;i <= n;++ i)
	{
		read(tmp1), sum += tmp1, insert(S, i, tmp1);
		read(tmp1);
		for(register int j = 1;j <= tmp1;++ j)
		{
			read(tmp2), read(tmp3);
			insert(i, n + tmp2, tmp3);
		}
	}
	for(register int i = 1;i <= m;++ i)
		read(tmp1), insert(n + i, T, tmp1);
	dinic();
	printf("%d", sum - ans);
    return 0;
}
posted @ 2018-03-08 11:25  嘒彼小星  阅读(139)  评论(0编辑  收藏  举报