您的位置:首页 > 其它

POJ 2987 (最大权闭合图 最小割)

2016-03-21 22:13 417 查看
Firing

Time Limit: 5000MS Memory Limit: 131072K
Total Submissions: 9493 Accepted: 2850
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 ≤ i, j ≤ 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

Hint

As of the situation described by the sample input, firing employees 4 and 5 will produce a net profit of 2, which is maximum.
Source

POJ Monthly--2006.08.27, frkstyc
读了论文以后才知道这东西是最大权闭合图,建图方法:

源点到正权点建边边权为点权;负权点到汇点建边边权为点权的绝对值;原图中的边边权为正无穷.

那么有两个结论:

1.最大权闭合图的权值和就是正点权之和-最小割

2.残留网络上源点能够到达的点就是选择的点

注意long long

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
const long long INF = 1e13;
#define maxn 5111
#define maxm 24111111
#define eps 1e-10
typedef long long type;

int n, m, x, N;
int s, t;
struct Edge
{
int from, to,next;
type cap,flow;
void get(int u,int a,int b,type c,type d)
{
from = u; to = a; next = b; cap = c; flow = d;
}
}edge[maxm];
int tol;
int head[maxn];
int gap[maxn],dep[maxn],pre[maxn],cur[maxn];
void init()
{
tol=0;
memset(head,-1,sizeof(head));
}

void add_edge(int u,int v,type w,type rw=0)
{ //cout << u << " " << v << " " << w << endl;
edge[tol].get(u, v,head[u],w,0);head[u]=tol++;
edge[tol].get(v, u,head[v],rw,0);head[v]=tol++;
}
type sap(int start,int end,int N)
{
memset(gap,0,sizeof(gap));
memset(dep,0,sizeof(dep));
memcpy(cur,head,sizeof(head));
int u=start;
pre[u]=-1;
gap[0]=N;
type ans=0;
while(dep[start]<N)
{
if(u==end)
{
type Min=INF;
for(int i=pre[u];i!=-1;i=pre[edge[i^1].to])
if(Min>edge[i].cap-edge[i].flow)
Min=edge[i].cap-edge[i].flow;
for(int i=pre[u];i!=-1;i=pre[edge[i^1].to])
{
edge[i].flow+=Min;
edge[i^1].flow-=Min;
}
u = start;
ans+=Min;
continue;
}
bool flag=false;
int v;
for(int i=cur[u];i !=-1;i=edge[i].next)
{
v=edge[i].to;
if(edge[i].cap-edge[i].flow&&dep[v]+1==dep[u])
{
flag=true;
cur[u]=pre[v]=i;
break;
}
}
if(flag)
{
u=v;
continue;
}
int Min=N;
for(int i=head[u];i!=-1;i=edge[i].next)
if(edge[i].cap-edge[i].flow&&dep[edge[i].to]<Min)
{
Min=dep[edge[i].to];
cur[u]=i;
}
gap[dep[u]]--;
if(!gap[dep[u]]) return ans;
dep[u]=Min+1;
gap[dep[u]]++;
if(u!=start) u=edge[pre[u]^1].to;
}
return ans;
}

long long w[maxn];
int num;
bool vis[maxn];

void dfs (int u) {
vis[u] = 1;
for (int i = head[u]; i != -1; i = edge[i].next) {
int v = edge[i].to;
if (vis[v])
continue;
if (edge[i].cap > edge[i].flow) {
num++;
dfs (v);
}
}
}

int main () {
//freopen ("in.txt", "r", stdin);
while (scanf ("%d%d", &n, &m) == 2) {
init ();
s = 0, t = n+1, N = n+2;
type sum = 0;
for (int i = 1; i <= n; i++) {
scanf ("%lld", &w[i]);
if (w[i] > 0) {
sum += w[i];
add_edge (s, i, w[i]);
}
else
add_edge (i, t, -w[i]);
}
for (int i = 1; i <= m; i++) {
int u, v;
scanf ("%d%d", &u, &v);
add_edge (u, v, INF);
}
type ans = sap (s, t, N); //cout << ans << endl;
num = 0;
memset (vis, 0, sizeof vis);
dfs (s);
printf ("%d %lld\n", num, sum-ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: