POJ 2987 最大权闭合子图

Firing
Time Limit: 5000MS Memory Limit: 131072K
Total Submissions: 7442 Accepted: 2247

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 ≤ in). The remaining m lines each contain two integers i and j (1 ≤ i, jn) 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.


最大权闭合子图经典题目,不懂概念,于是问了上交神牛。

这是别人给的解释:直观的理解就是说一堆东西有依赖关系,有正有负,选了某个物品,那么他指向的物品就必须选,那么我们先假设选了所有的物品,
这样就满足了所有的限制.然后建图,源点与正权点连,边为其权值,负权点指向汇点,边为权值的绝对值,中间依赖关系连一条边,
权值为无穷大,然后求最小割,这样子会分成与s相连的联通块和与t相连的联通块,可以看到,中间的依赖关系的边不会割开,
那么只要一个点与他的后继节点必在一个联通块内,那么拿所有的正权点的权值和减去最小割就是答案,
其意义在于与源点相连的点集就是答案,可以看到如果一个正权点在汇点联通块,它与源点间的边会被割开,而
与源点相连的负权点同样会与汇点割开,这两部分权值就是割的权值,然后最小割可以最小化这个值,自然就让总权值最大

根据上面描述指导建图,

sap代码:

/* ***********************************************
Author :rabbit
Created Time :2014/3/8 10:05:50
File Name :1718.cpp
************************************************ */
#pragma comment(linker, "/STACK:102400000,102400000")
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <string>
#include <time.h>
#include <math.h>
#include <queue>
#include <stack>
#include <set>
#include <map>
using namespace std;
#define INF 100000000000000LL
#define eps 1e-8
#define pi acos(-1.0)
typedef long long ll;
const ll maxn=10010;
const ll maxm=400010;
struct Edge{
    ll to,next,cap,flow;
    Edge(){};
    Edge(ll _next,ll _to,ll _cap,ll _flow){
        next=_next;to=_to;cap=_cap;flow=_flow;
    }
}edge[maxm];
ll head[maxn],tol,gap[maxn],dep[maxn],cur[maxn];
void addedge(ll u,ll v,ll flow){
    edge[tol]=Edge(head[u],v,flow,0);head[u]=tol++;
    edge[tol]=Edge(head[v],u,0,0);head[v]=tol++;
}
ll Q[maxn];
void bfs(ll start,ll end){
    memset(dep,-1,sizeof(dep));
    memset(gap,0,sizeof(gap));
    gap[0]++;ll front=0,rear=0;
    dep[end]=0;Q[rear++]=end;
    while(front!=rear){
        ll u=Q[front++];
        for(ll i=head[u];i!=-1;i=edge[i].next){
            ll v=edge[i].to;if(dep[v]==-1&&edge[i].cap)
                Q[rear++]=v,dep[v]=dep[u]+1,gap[dep[v]]++;
        }
    }
}
ll S[maxn];
ll sap(ll start,ll end,ll N){
    bfs(start,end);
    memcpy(cur,head,sizeof(head));
    ll top=0,u=start,ans=0;
    while(dep[start]<N){
        if(u==end){
            ll MIN=INF,id;
            for(ll i=0;i<top;i++)
                if(MIN>edge[S[i]].cap-edge[S[i]].flow)
                    MIN=edge[S[i]].cap-edge[S[i]].flow,id=i;
            for(ll i=0;i<top;i++)
                edge[S[i]].flow+=MIN,edge[S[i]^1].flow-=MIN;
            ans+=MIN,top=id,u=edge[S[top]^1].to;
            continue;
        }
        bool flag=0;ll v;
        for(ll 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=1;cur[u]=i;break;
            }
        }
        if(flag){
            S[top++]=cur[u];u=v;continue;
        }
        ll MIN=N;
        for(ll 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;
        if(--gap[dep[u]]==0)break;gap[dep[u]=MIN+1]++;
        if(u!=start)u=edge[S[--top]^1].to;
    }
    return ans;
}
ll ans,col[maxn];
void dfs1(ll u){
	col[u]=1;
	for(ll i=head[u];i!=-1;i=edge[i].next){
		ll v=edge[i].to;
		if(!col[v]&&edge[i].cap>edge[i].flow)dfs1(v);
	}
}
int main()
{
     //freopen("data.in","r",stdin);
     //freopen("data.out","w",stdout);
     ll n,m;
	 while(~scanf("%lld%lld",&n,&m)){
		 memset(head,-1,sizeof(head));tol=0;
		 ll sum=0;
		 for(ll i=1;i<=n;i++){
			 ll w;
			 scanf("%lld",&w);
			 if(w>0){
				 sum+=w;
				 addedge(0,i,w);
			 }
			 else{
				 addedge(i,n+1,-w);
			 }
		 }
		 while(m--){
			 ll u,v;
			 scanf("%lld%lld",&u,&v);
			 addedge(u,v,INF);
		 }
		 ll pp=sap(0,n+1,n+10);
		 ll ans=0;
		 memset(col,0,sizeof(col));
		 dfs1(0);
		 //for(ll i=1;i<=n;i++)cout<<col[i]<<" ";cout<<endl;
		 for(ll i=1;i<=n;i++)if(col[i])ans++;
		 printf("%lld %lld\n",ans,sum-pp);
	 }
     return 0;
}


dinic一直跑不出样例,原来是模板里一个分号错误的打成逗号,又检查出模板的一个bug。

dinic代码:

/* ***********************************************
Author :rabbit
Created Time :2014/3/8 10:05:50
File Name :1718.cpp
************************************************ */
#pragma comment(linker, "/STACK:102400000,102400000")
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <string>
#include <time.h>
#include <math.h>
#include <queue>
#include <stack>
#include <set>
#include <map>
using namespace std;
#define INF 100000000000000LL
#define eps 1e-8
#define pi acos(-1.0)
typedef long long ll;
const ll maxn=6110;
const ll maxm=400010;
struct Edge{
	ll to,next,cap;
	Edge(){};
	Edge(ll _next,ll _to,ll _cap){
		next=_next;to=_to;cap=_cap;
	}
}edge[maxm];
ll head[maxn],tol,dep[maxn],cur[maxn],n;
void addedge(ll u,ll v,ll flow){
	edge[tol]=Edge(head[u],v,flow);head[u]=tol++;
	edge[tol]=Edge(head[v],u,0);head[v]=tol++;
}
ll Q[maxn];
bool bfs(ll start,ll end){
	memset(dep,-1,sizeof(dep));
	ll front=0,rear=0;
	dep[start]=0;Q[rear++]=start;
	while(front!=rear){
		ll u=Q[front++];
		for(ll i=head[u];i!=-1;i=edge[i].next){
			ll v=edge[i].to;if(dep[v]==-1&&edge[i].cap>0){
				Q[rear++]=v,dep[v]=dep[u]+1;
				if(v==end)return 1;
			}
		}
	}
	return 0;
}
ll dinic(ll s,ll t){  
     ll i,res=0,top;  
     ll S[maxn];
     while(bfs(s,t)){  
          memcpy(cur,head,sizeof(head));  
          ll u=s;top=0;  
          while(1){  
              if(u==t){  
                  ll min=INF,id;
				  for(ll i=0;i<top;i++)
					  if(edge[S[i]].cap<min)min=edge[S[i]].cap,id=i;
				  for(ll i=0;i<top;i++)edge[S[i]].cap-=min,edge[S[i]^1].cap+=min;
				  res+=min;top=id;u=edge[S[top]^1].to;
              }  
              for(ll i=cur[u];i!=-1;cur[u]=i=edge[i].next)  
              if(edge[i].cap&&dep[u]+1==dep[edge[i].to])break;  
              if(cur[u]!=-1)S[top++]=cur[u],u=edge[cur[u]].to;  
              else{  
                   if(top==0)break;  
                   dep[u]=-1;u=edge[S[--top]^1].to;  
              }  
         }  
    }  
       return res;  
} 
ll ans,col[maxn];
void dfs1(ll u){
	col[u]=1;
	for(ll i=head[u];i!=-1;i=edge[i].next){
		ll v=edge[i].to;
		if(!col[v]&&edge[i].cap>0)dfs1(v);
	}
}
int main()
{
     //freopen("data.in","r",stdin);
     //freopen("data.out","w",stdout);
     ll n,m;
	 while(~scanf("%lld%lld",&n,&m)){
		 memset(head,-1,sizeof(head));tol=0;
		 ll sum=0;
		 for(ll i=1;i<=n;i++){
			 ll w;
			 scanf("%lld",&w);
			 if(w>0){
				 sum+=w;
				 addedge(0,i,w);
			 }
			 else{
				 addedge(i,n+1,-w);
			 }
		 }
		 while(m--){
			 ll u,v;
			 scanf("%lld%lld",&u,&v);
			 addedge(u,v,INF);
		 }
		 ll pp=dinic(0,n+1);
		 ll ans=0;
		 memset(col,0,sizeof(col));
		 dfs1(0);
		 //for(ll i=1;i<=n;i++)cout<<col[i]<<" ";cout<<endl;
		 for(ll i=1;i<=n;i++)if(col[i])ans++;
		 printf("%lld %lld\n",ans,sum-pp);
	 }
     return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值