POJ 2987 Firing, poj2987firing

Source: Internet
Author: User

POJ 2987 Firing, poj2987firing

Time Limit:5000 MS   Memory Limit:131072 K
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 demo-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 units 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 integersN(0 <N≤ 5000) andM(0 ≤M≤ 60000) on the same line. Next followsN+MLines. The firstNLines of these give the net profit/loss from firingI-Th employee individuallyBi(|Bi| ≤ 107, 1 ≤IN). The remainingMLines each contain two integersIAndJ(1 ≤I,JN) MeaningI-Th employee hasJ-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 58-9-2012-101 22 51 43 44 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.

Source

POJ Monthly -- 2006.08.27, frkstyc is similar in nature to the previous question. They are all the largest closed weights. the difference lies in 1. the value of m has nothing to do with the weight. In this case, we can set the side before x and y to INF. to request the number of people, we need to re-run the DFS residual network. If there is still traffic on this side, it means we need to cut down this person. For the reason, https://wenku.baidu.com/view/986baf00b52acfc789ebc9:
1. Do not enable long in full mode. 2. Do not reverse the number of outputs.
  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 }

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.