[CEOI2008]order BZOJ1391 网络流

题目描述

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

输入输出格式

输入格式:

第一行给出 N,M(1<=N<=1200,1<=M<=1200) 下面将有N组数据。

每组数据第一行给出完成这个任务能赚到的钱(其在[1,5000])及有多少道工序

接下来若干行每行两个数,分别描述完成工序所需要的机器编号及租用它的费用(其在[1,20000]) 最后M行,每行给出购买机器的费用(其在[1,20000])

输出格式:

最大利润

输入输出样例

输入样例#1: 复制
2 3
100 2
1 30
2 20
100 2
1 40
3 80
50
80
110
输出样例#1: 复制
50

建立源点st与汇点ed;
类比于 最大权闭合子图;
st 与任务连边,权值为所能赚的钱;
ed 与机器相连,表示购买的花费;
但还有一个限制就是可以租用机器;
在最大权闭合子图中,如果没有该限制,其连边应该为inf的容量;
那么考虑租用,将任务与机器的连边inf改为租金即可;
租用机器的操作只与该任务有关,而与其他无关,所以改成 moneyRent即可;

luogu上面我加了O2优化以及快读才过,不知道我这个dinic为啥会T(已加了当前弧优化还是T)

测试点信息

#1 AC 22ms/24208KB
#2 AC 14ms/26064KB
#3 AC 24ms/24096KB
#4 AC 21ms/24200KB
#5 AC 14ms/25788KB
#6 AC 24ms/24224KB
#7 AC 21ms/24184KB
#8 AC 11ms/24120KB
#9 AC 24ms/24192KB
#10 AC 24ms/24212KB
#11 AC 14ms/26096KB
#12 AC 24ms/24336KB
#13 AC 20ms/24104KB
#14 AC 12ms/24964KB
#15 AC 747ms/39924KB
#16 AC 489ms/33560KB
#17 AC 853ms/64272KB
#18 AC 716ms/39828KB
#19 AC 872ms/69156KB
#20 AC 703ms/69504KB
由几个点都接近900ms了,orz;
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<bitset>
#include<ctime>
#include<deque>
#include<stack>
#include<functional>
#include<sstream>
#pragma GCC optimize(2)
//#include<cctype>
//#pragma GCC optimize("O3")
using namespace std;
#define maxn 3000005
#define inf 0x3f3f3f3f
#define INF 9999999999
#define rdint(x) scanf("%d",&x)
#define rdllt(x) scanf("%lld",&x)
#define rdult(x) scanf("%lu",&x)
#define rdlf(x) scanf("%lf",&x)
#define rdstr(x) scanf("%s",x)
typedef long long  ll;
typedef unsigned long long ull;
typedef unsigned int U;
#define ms(x) memset((x),0,sizeof(x))
const long long int mod = 1e9 + 7;
#define Mod 1000000000
#define sq(x) (x)*(x)
#define eps 1e-3
typedef pair<int, int> pii;
#define pi acos(-1.0)
//const int N = 1005;
#define REP(i,n) for(int i=0;i<(n);i++)

inline ll rd() {
	ll x = 0;
	char c = getchar();
	bool f = false;
	while (!isdigit(c)) {
		if (c == '-') f = true;
		c = getchar();
	}
	while (isdigit(c)) {
		x = (x << 1) + (x << 3) + (c ^ 48);
		c = getchar();
	}
	return f ? -x : x;
}

ll gcd(ll a, ll b) {
	return b == 0 ? a : gcd(b, a%b);
}
ll sqr(ll x) { return x * x; }

/*ll ans;
ll exgcd(ll a, ll b, ll &x, ll &y) {
	if (!b) {
		x = 1; y = 0; return a;
	}
	ans = exgcd(b, a%b, x, y);
	ll t = x; x = y; y = t - a / b * y;
	return ans;
}
*/



ll qpow(ll a, ll b, ll c) {
	ll ans = 1;
	a = a % c;
	while (b) {
		if (b % 2)ans = ans * a%c;
		b /= 2; a = a * a%c;
	}
	return ans;
}

int n, m;
int st, ed;
struct node {
	int u, v, nxt, w;
}edge[maxn<<1];

int head[maxn], cnt;

void addedge(int u, int v, int w) {
	edge[cnt].u = u; edge[cnt].v = v; edge[cnt].w = w;
	edge[cnt].nxt = head[u]; head[u] = cnt++;
}

int rk[maxn];

int bfs() {
	queue<int>q;
	ms(rk);
	rk[st] = 1; q.push(st);
	while (!q.empty()) {
		int tmp = q.front(); q.pop();
		for (int i = head[tmp]; i != -1; i = edge[i].nxt) {
			int to = edge[i].v;
			if (rk[to] || edge[i].w <= 0)continue;
			rk[to] = rk[tmp] + 1; q.push(to);
		}
	}
	return rk[ed];
}
int dfs(int u, int flow) {
	if (u == ed)return flow;
	int add = 0;
	for (int i = head[u]; i != -1 && add < flow; i = edge[i].nxt) {
		int v = edge[i].v;
		if (rk[v] != rk[u] + 1 || !edge[i].w)continue;
		int tmpadd = dfs(v, min(edge[i].w, flow - add));
		if (!tmpadd) { rk[v] = -1; continue; }
		edge[i].w -= tmpadd; edge[i ^ 1].w += tmpadd; add += tmpadd;
	}
	return add;
}
ll ans;
void dinic() {
	while (bfs())ans += dfs(st, inf);
}



int main()
{
	//ios::sync_with_stdio(0);
	memset(head, -1, sizeof(head));
	rdint(n); rdint(m);
	st = 0; ed = n + m + 1;
	int sum = 0;
	for (int i = 1; i <= n; i++) {
		int moy, num;
		moy = rd(); sum += moy;
		addedge(st, i, moy); addedge(i, st, 0);
		//rdint(num);
		num = rd();
		for (int j = 0; j < num; j++) {
			int ID; ID = rd(); moy = rd();
			//rdint(ID); rdint(moy);
			addedge(i, n + ID, moy); addedge(n + ID, i, 0);
		}
	}
	for (int i = 1; i <= m; i++) {
		int moy;// rdint(moy);
		moy = rd();
		addedge(n + i, ed, moy); addedge(ed, n + i, 0);
	}
	dinic();
	printf("%d\n", sum - ans);
    return 0;
}

 


posted @ 2018-12-02 16:22  NKDEWSM  阅读(153)  评论(0编辑  收藏  举报