POJ 2987 Firing
Posted 自为
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ 2987 Firing相关的知识,希望对你有一定的参考价值。
Time Limit: 5000MS | Memory Limit: 131072K | |
Total Submissions: 10804 | Accepted: 3263 |
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
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cmath> 5 #include<queue> 6 #define lli long long int 7 using namespace std; 8 const int MAXN=2000001; 9 const int INF = 1e8; 10 inline void read(int &n) 11 { 12 char c=‘+‘;int x=0;bool flag=0; 13 while(c<‘0‘||c>‘9‘){c=getchar();if(c==‘-‘)flag=1;} 14 while(c>=‘0‘&&c<=‘9‘){x=x*10+c-48;c=getchar();} 15 n=flag==1?-x:x; 16 } 17 int n,m,s,t; 18 struct node 19 { 20 int u,v,flow,nxt; 21 }edge[MAXN]; 22 int head[MAXN]; 23 int cur[MAXN]; 24 int num=0; 25 int deep[MAXN]; 26 lli tot=0; 27 bool vis[MAXN]; 28 lli out; 29 void add_edge(int x,int y,int z) 30 { 31 edge[num].u=x; 32 edge[num].v=y; 33 edge[num].flow=z; 34 edge[num].nxt=head[x]; 35 head[x]=num++; 36 } 37 void add(int x,int y,int z) 38 { 39 add_edge(x,y,z); 40 add_edge(y,x,0); 41 } 42 bool BFS() 43 { 44 memset(deep,0,sizeof(deep)); 45 deep[s]=1; 46 queue<int>q; 47 q.push(s); 48 while(q.size()!=0) 49 { 50 int p=q.front(); 51 q.pop(); 52 for(int i=head[p];i!=-1;i=edge[i].nxt) 53 if(!deep[edge[i].v]&&edge[i].flow) 54 deep[edge[i].v]=deep[edge[i].u]+1, 55 q.push(edge[i].v); 56 } 57 return deep[t]; 58 59 } 60 lli DFS(int now,int nowflow) 61 { 62 if(now==t||nowflow<=0) 63 return nowflow; 64 lli totflow=0; 65 for(int &i=cur[now];i!=-1;i=edge[i].nxt) 66 { 67 if(deep[edge[i].v]==deep[edge[i].u]+1&&edge[i].flow) 68 { 69 int canflow=DFS(edge[i].v,min(nowflow,edge[i].flow)); 70 edge[i].flow-=canflow; 71 edge[i^1].flow+=canflow; 72 totflow+=canflow; 73 nowflow-=canflow; 74 if(nowflow<=0) 75 break; 76 } 77 78 } 79 return totflow; 80 } 81 void Dinic() 82 { 83 lli ans=0; 84 while(BFS()) 85 { 86 memcpy(cur,head,MAXN); 87 ans+=DFS(s,1e8); 88 } 89 out=tot-ans; 90 } 91 int find_pep(int now) 92 { 93 vis[now]=1; 94 for(int i=head[now];i!=-1;i=edge[i].nxt) 95 if(!vis[edge[i].v]&&edge[i].flow) 96 find_pep(edge[i].v); 97 } 98 int main() 99 { 100 //freopen("a.in","r",stdin); 101 //freopen("c.out","w",stdout); 102 while(~scanf("%d%d",&n,&m)) 103 { 104 memset(head,-1,sizeof(head)); 105 num=0; 106 s=0,t=n+1; 107 tot=0; 108 for(int i=1;i<=n;i++) 109 { 110 int a;read(a); 111 if(a>0) tot+=a,add(s,i,a); 112 if(a<0) add(i,t,-a); 113 } 114 for(int i=1;i<=m;i++) 115 { 116 int x,y;read(x);read(y); 117 add(x,y,INF); 118 } 119 Dinic(); 120 memset(vis,0,sizeof(vis)); 121 int ans=0; 122 find_pep(s); 123 for(int i=1;i<=n;i++) 124 ans+=vis[i]; 125 //cout<<ans<<" "<<out<<endl; 126 printf("%d %I64d\n",ans,out); 127 } 128 return 0; 129 }
以上是关于POJ 2987 Firing的主要内容,如果未能解决你的问题,请参考以下文章