POJ_2987_Firing_(最大流+最大权闭合图)
描述
http://poj.org/problem?id=2987
要炒员工鱿鱼,炒了一个人,他的下属一定被炒.给出每个人被炒后公司的收益(负值表示亏损),问怎样炒公司收益最大,以及这种方法炒了几个人.(先输出人数)
Time Limit: 5000MS | Memory Limit: 131072K | |
Total Submissions: 9674 | Accepted: 2906 |
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
Source
分析
求最大收益是用最大权闭合图.要炒一个人,他的下属也要被炒,那么边由上司连向下属,求最大权闭合图即可.
求炒了几个人实际是就是求先前求出来的那个最大权闭合图中点的个数.最大权闭合图对应的是最小割,而最小割中的边都是满流的,所以从源点出发无法到达闭合图的补集(即T),并且由于是闭合图,所以图中的边都不属于割,这样闭合图中就没有流量,从源点出发可以到达闭合图中的每一个点.
注意:
1.要用long long.
ps.f[S,T]=|f|,即整个图的流等于流过割的流,这样这道题中,f=fmax=cmin,又f=f[S,T],所以f[S,T]=cmin,也就是流过最小割的流等于最小割的容量,所以满流了.
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #include<vector> 5 #include<queue> 6 #define rep(i,n) for(int i=0;i<(n);i++) 7 #define for1(i,a,n) for(int i=(a);i<=(n);i++) 8 #define CC(i,a) memset(i,a,sizeof(i)) 9 #define read(a) a=getnum() 10 #define ll long long 11 using namespace std; 12 13 const int maxn=5000+10,INF=0x7fffffff; 14 int n,m; 15 ll sumw; 16 int level[maxn],iter[maxn]; 17 bool vis[maxn]; 18 struct edge 19 { 20 int to,cap,rev; 21 edge() {} 22 edge(int a,ll b,int c) : to(a),cap(b),rev(c) {} 23 }; 24 vector <edge> g[maxn]; 25 26 inline int getnum() 27 { 28 int r=0,k=1; char c; 29 for(c=getchar();c<'0'||c>'9';c=getchar()) if(c=='-') k=-1; 30 for(;c>='0'&&c<='9';c=getchar()) r=r*10+c-'0'; 31 return r*k; 32 } 33 34 void add_edge(int from,int to,int cap) 35 { 36 g[from].push_back(edge(to,cap,g[to].size())); 37 g[to].push_back(edge(from,0,g[from].size()-1)); 38 } 39 40 void bfs(int s) 41 { 42 CC(level,-1); 43 level[s]=0; 44 queue <int> q; 45 q.push(s); 46 while(!q.empty()) 47 { 48 int t=q.front(); q.pop(); 49 rep(i,g[t].size()) 50 { 51 edge e=g[t][i]; 52 if(e.cap>0&&level[e.to]<0) 53 { 54 level[e.to]=level[t]+1; 55 q.push(e.to); 56 } 57 } 58 } 59 } 60 61 int dfs(int v,int t,int f) 62 { 63 if(v==t) return f; 64 for(int &i=iter[v];i<g[v].size();i++) 65 { 66 edge &e=g[v][i]; 67 if(e.cap>0&&level[e.to]>level[v]) 68 { 69 int d=dfs(e.to,t,min(f,e.cap)); 70 if(d>0) 71 { 72 e.cap-=d; 73 g[e.to][e.rev].cap+=d; 74 return d; 75 } 76 } 77 78 } 79 return 0; 80 } 81 82 ll max_flow(int s,int t) 83 { 84 ll flow=0; 85 bfs(s); 86 while(level[t]>0) 87 { 88 int f; 89 CC(iter,0); 90 while((f=dfs(s,t,INF))>0) flow+=f; 91 bfs(s); 92 } 93 return flow; 94 } 95 96 int DFS(int v) 97 { 98 vis[v]=true; 99 int ans=1; 100 rep(i,g[v].size()) 101 { 102 edge e=g[v][i]; 103 if(!vis[e.to]&&e.cap>0) ans+=DFS(e.to); 104 } 105 return ans; 106 } 107 108 void init() 109 { 110 read(n); read(m); 111 for1(i,1,n) 112 { 113 int w; read(w); 114 if(w>0) 115 { 116 sumw+=w; 117 add_edge(0,i,w); 118 } 119 else 120 { 121 add_edge(i,n+1,-w); 122 } 123 } 124 for1(i,1,m) 125 { 126 int a,b; read(a); read(b); 127 add_edge(a,b,INF); 128 } 129 } 130 131 int main() 132 { 133 #ifndef ONLINE_JUDGE 134 freopen("firing.in","r",stdin); 135 freopen("firing.out","w",stdout); 136 #endif 137 init(); 138 ll ans=sumw-max_flow(0,n+1); 139 printf("%d %lld\n",DFS(0)-1,ans); 140 #ifndef ONLINE_JUDGE 141 fclose(stdin); 142 fclose(stdout); 143 system("firing.out"); 144 #endif 145 return 0; 146 }