POJ 2987 Firing

Firing

Time Limit: 5000ms
Memory Limit: 131072KB
This problem will be judged on  PKU. Original ID: 2987
64-bit integer IO format: %lld      Java class name: Main
 

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

Source

 
解题:最大权闭合图
 
  1 #include <iostream>

  2 #include <cstdio>

  3 #include <cstring>

  4 #include <cmath>

  5 #include <algorithm>

  6 #include <climits>

  7 #include <vector>

  8 #include <queue>

  9 #include <cstdlib>

 10 #include <string>

 11 #include <set>

 12 #include <stack>

 13 #define LL long long

 14 #define pii pair<int,int>

 15 #define INF 0x3f3f3f3f3f3f3f3fLL

 16 using namespace std;

 17 const int maxn = 5010;

 18 struct arc{

 19     int to,next;

 20     LL cap;

 21     arc(int x = 0,LL y = 0,int z = -1){

 22         to = x;

 23         cap = y;

 24         next = z;

 25     }

 26 };

 27 arc e[130010];

 28 int head[maxn],cur[maxn],d[maxn],q[maxn];

 29 int n,m,hd,tl,S,T,tot,cnt;

 30 bool vis[maxn];

 31 void add(int u,int v,int cap){

 32     e[tot] = arc(v,cap,head[u]);

 33     head[u] = tot++;

 34     e[tot] = arc(u,0,head[v]);

 35     head[v] = tot++;

 36 }

 37 bool bfs(){

 38     hd = tl = 0;

 39     memset(d,-1,sizeof(d));

 40     d[S] = 1;

 41     q[tl++] = S;

 42     while(hd < tl){

 43         int u = q[hd++];

 44         for(int i = head[u]; ~i; i = e[i].next){

 45             if(e[i].cap > 0 && d[e[i].to] == -1){

 46                 d[e[i].to] = d[u] + 1;

 47                 q[tl++] = e[i].to;

 48             }

 49         }

 50     }

 51     return d[T] > -1;

 52 }

 53 LL dfs(int u,LL low){

 54     if(u == T) return low;

 55     LL tmp = 0,a;

 56     for(int &i = cur[u]; ~i; i = e[i].next){

 57         if(e[i].cap > 0 && d[e[i].to] == d[u] + 1 && (a = dfs(e[i].to,min(low,e[i].cap)))){

 58             e[i].cap -= a;

 59             e[i^1].cap += a;

 60             tmp += a;

 61             low -= a;

 62             if(!low) break;

 63         }

 64     }

 65     if(!tmp) d[u] = -1;

 66     return tmp;

 67 }

 68 void go(int u){

 69     cnt++;

 70     vis[u] = true;

 71     for(int i = head[u]; ~i; i = e[i].next){

 72         if(e[i].cap > 0 && !vis[e[i].to]) go(e[i].to);

 73     }

 74 }

 75 int main(){

 76     LL ans,tmp;

 77     int u,v,w;

 78     while(~scanf("%d %d",&n,&m)){

 79         memset(head,-1,sizeof(head));

 80         memset(vis,false,sizeof(vis));

 81         S = ans = tot = cnt = 0;

 82         T = n + 1;

 83         for(int i = 1; i <= n; i++){

 84             scanf("%I64d",&tmp);

 85             if(tmp > 0){

 86                 ans += tmp;

 87                 add(S,i,tmp);

 88             }else if(tmp < 0) add(i,T,-tmp);

 89         }

 90         for(int i = 0; i < m; i++){

 91             scanf("%d %d",&u,&v);

 92             add(u,v,INF);

 93         }

 94         while(bfs()){

 95             memcpy(cur,head,sizeof(head));

 96             ans -= dfs(S,INF);

 97         }

 98         go(S);

 99         printf("%d %I64d\n",cnt-1,ans);

100     }

101     return 0;

102 }
View Code

 

你可能感兴趣的:(poj)