您的位置:首页 > 其它

最大权闭合子图(poj 2987 Firing)

2017-08-29 00:16 501 查看
Firing

Time Limit: 5000MS Memory Limit: 131072K
Total Submissions: 10884 Accepted: 3286
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


题意:

n个点m条边的有向图,每个点都有一个权值,要在其中找到一张子图,使得所有点权值和最大,输出点数和权值和

结论:最大流:

①如果a和b连有一条边(a->b),那么a到b连接一条流量为无穷大的边

②如果a点的权值为正,那么源点到a点连接一条容量为val[a]的边

②如果a点的权值为负,那么a点到汇点连接一条容量为-val[a]的边

最大权就是权值为正的点之和-最大流

最大权闭合子图就是残余网络中与源点相连的那一部分

这样一定不会出现非法情况,因为如果a到b有条边,且a点的权值为正b点的权值为负,假设你选了a没选b,那么相当于没有割掉S到a的边,也没有割掉b到T的边,这样S到T就是联通的了所以当前肯定不是最大流

这有一篇比较好的证明:

http://www.cnblogs.com/wuyiqi/archive/2012/03/12/2391960.html

#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<queue>
#define LL long long
using namespace std;
LL n, m, sum, cnt, S, T, val[5005], vis[5005], head[5005], h[5005], cur[5005];
typedef struct
{
LL to, next;
LL flow;
}Road;
Road G[160005];
void Add(LL u, LL v, LL flow)
{
cnt++;
G[cnt].next = head[u];
head[u] = cnt;
G[cnt].to = v;
G[cnt].flow = flow;
}
int Jud()
{
LL now, i;
queue<int> q;
memset(h, -1, sizeof(h));
q.push(S);
h[S] = 0;
while(q.empty()==0)
{
now = q.front();
q.pop();
for(i=head[now];i!=0;i=G[i].next)
{
if(G[i].flow && h[G[i].to]==-1)
{
h[G[i].to] = h[now]+1;
q.push(G[i].to);
}
}
}
if(h[T]!=-1)
return 1;
return 0;
}
LL Sech(LL x, LL flow)
{
LL w, used, i;
if(x==T)
return flow;
used = 0;
for(i=cur[x];i!=0;i=G[i].next)
{
if(h[G[i].to]==h[x]+1)
{
w = Sech(G[i].to, min(flow-used, G[i].flow));
G[i].flow -= w;
G[i^1].flow += w;
if(G[i].flow)
cur[x] = i;
used += w;
if(used==flow)
return flow;
}
}
if(used==0)
h[x] = -1;
return used;
}
LL Dinic()
{
LL i, flow = 0;
while(Jud())
{
for(i=S;i<=T;i++)
cur[i] = head[i];
flow += Sech(S, (LL)1<<50);
}
return flow;
}
void Sech(LL x)
{
int i, v;
sum++;
vis[x] = 1;
for(i=head[x];i!=0;i=G[i].next)
{
v = G[i].to;
if(G[i].flow>0 && vis[v]==0)
Sech(v);
}
}
int main(void)
{
LL n, m, i, x, y, ans;
while(scanf("%lld%lld", &n, &m)!=EOF)
{
cnt = 1, ans = 0;
S = 0, T = n+1;
memset(head, 0, sizeof(head));
for(i=1;i<=n;i++)
{
scanf("%lld", &val[i]);
if(val[i]>0)
{
ans += val[i];
Add(S, i, val[i]);
Add(i, S, 0);
}
else
{
Add(i, T, -val[i]);
Add(T, i, 0);
}
}
for(i=1;i<=m;i++)
{
scanf("%lld%lld", &x, &y);
Add(x, y, (LL)1<<50);
Add(y, x, 0);
}
memset(vis, 0, sizeof(vis));
ans -= Dinic();
sum = 0;
Sech(S);
printf("%lld %lld\n", sum-1, ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: