POJ2987--最大权闭合图

Description

You’ve finally got mad at “the world’s most stupid” employees of yours and decided to do some firings. You’re now simply too mad to give response to questions like “Don’t you think it is an even more stupid decision to have signed them?”, yet calm enough to consider the potential profit and loss from firing a good portion of them. While getting rid of an employee will save your wage and bonus expenditure on him, termination of a contract before expiration costs you funds for compensation. If you fire an employee, you also fire all his underlings and the underlings of his underlings and those underlings’ underlings’ underlings… An employee may serve in several departments and his (direct or indirect) underlings in one department may be his boss in another department. Is your firing plan ready now?

Input

The input starts with two integers n (0 < n ≤ 5000) and m (0 ≤ m ≤ 60000) on the same line. Next follows n + m lines. The first n lines of these give the net profit/loss from firing the i-th employee individually bi (|bi| ≤ 107, 1 ≤ i ≤ n). The remaining m lines each contain two integers i and j (1 ≤ ij ≤ n) meaning the i-th employee has the j-th employee as his direct underling.

Output

Output two integers separated by a single space: the minimum number of employees to fire to achieve the maximum profit, and the maximum profit.

Sample Input

5 5
8
-9
-20
12
-10
1 2
2 5
1 4
3 4
4 5

Sample Output

2 2
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
#define maxn 5080
#define edge 120080
#define inf 0x3f3f3f3f3f3f3f3fll
#define LL long long int
int first[maxn],dis[maxn],num[maxn];
bool vis[maxn];
int vv[edge];LL ww[edge];int nxt[edge];
int e,NN,n,m;
void addEdge(int u,int v,LL w)
{
	vv[e] = v;	ww[e] = w;		nxt[e] = first[u];	first[u] = e++;
	vv[e] = u;	ww[e] = 0;		nxt[e] = first[v];		first[v] = e++;
}
inline LL min(LL a,LL b)
{
	return a>b?b:a;
}
LL dfs(int u,int s,int d,LL cost)
{
	if(u == d)	return cost;
	LL ans = 0;
	int _min = NN;
	for(int i = first[u];i != -1;i = nxt[i])
	{
		int v = vv[i];
		if(ww[i])
		{
			if(dis[v] + 1 == dis[u])
			{
				int t = dfs(v,s,d,min(ww[i],cost));
				ww[i] -= t;
				ww[i^1] += t;
				ans += t;
				cost -= t;
				if(dis[s] == NN)		return ans;
				if(!cost)	break;
			}
			if(_min > dis[v])		_min = dis[v];
		}
	}
	if(!ans)
	{
		if(--num[dis[u]] == 0)	dis[s] = NN;
		dis[u] = _min + 1;
		num[dis[u]]++;
	}
	return ans;
}
LL isap(int s,int d)
{
	memset(dis,0,sizeof(dis));
	memset(num,0,sizeof(num));
	num[0] = NN;
	LL ans = 0;
	while(dis[s] < NN)	ans += dfs(s,s,d,inf);
	return ans;
}
void dfs(int u)
{
	if(u == n+1)	return;
	vis[u] = 1;
	for(int i = first[u];i != -1;i = nxt[i])
	{
		int v =  vv[i];
		if(ww[i] > 0 && !vis[v])	dfs(v);
	}
}
int main()
{
	//freopen("in.txt","r",stdin);
	int s,t;
	while(scanf("%d%d",&n,&m)==2)
	{
		e = 0;
		LL sum = 0;
		memset(first,-1,sizeof(first));
		memset(vis,0,sizeof(vis));
		NN = n+2;
		for(int i = 1;i <= n;i++)
		{
			LL key;	scanf("%lld",&key);
			if(key >= 0)	
			{
				sum += key;
				addEdge(0,i,key);
			}
			else 
			{
				addEdge(i,n+1,-key);
			}
		}
		for(int i = 1;i <= m;i++)
		{
			int u,v;
			scanf("%d%d",&u,&v);
			addEdge(u,v,inf);
		}
		LL ans = isap(0,n+1);
		int ans2 = 0;
		dfs(0);
		for(int i = 1;i <= n;i++)
		{
			if(vis[i])	ans2++;
		}
		printf("%d %lld\n",ans2,sum - ans);
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值