| Time Limit: 5000MS | Memory Limit: 131072K | |
| Total Submissions: 6666 | Accepted: 2020 |
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
题目大意是要裁员,给出裁掉对应员工需要的花费(可正可负,正说明裁掉这个员工是赚的,负说明是亏的),同时还给出员工之间的关系,如果A是B的上司,则裁掉A的话B也必须裁掉。
这题是很裸的最大权闭合图问题,既答案为 最大权闭包 = 正权和-最小割。又最小割 = 最大流。故求出最大流即可。构图时,构造一个源点,将源点与值为正的点相连,容量为该点的值,将值为负的点与汇点相连,容量为该点的值的绝对值。最后根据给出的员工关系连边,容量为无穷。
题目同时还要求出裁掉的员工的最小个数,其实就是在残留网络中找到源点可达的点(非满流说明可达),最后统计有多少个这样的点。
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #define SIZE 5005 #define inf 0x7fffffff using namespace std; typedef __int64 Int; struct node { int to,next; Int val; }edge[SIZE*505]; int N,M,sc,sk,pt; int head[SIZE],idx; int gap[SIZE],dis[SIZE]; Int sum,ans,num; bool vis[SIZE]; void addnode(int from,int to,Int val) { edge[idx].to = to; edge[idx].val = val; edge[idx].next = head[from]; head[from] = idx ++; edge[idx].to = from; edge[idx].val = 0; edge[idx].next = head[to]; head[to] = idx ++; } Int dfs(int cur,Int cval) { if(cur == sk) return cval; int mindis = pt - 1; Int tval = cval; for(int i=head[cur]; i!=-1; i=edge[i].next) { int to = edge[i].to; if(edge[i].val > 0) { if(dis[to] + 1 == dis[cur]) { Int val = dfs(to,min(edge[i].val,tval)); tval -= val; edge[i].val -= val; edge[i^1].val += val; if(dis[sc] >= pt) return cval - tval; if(tval == 0) break; } if(dis[to] < mindis) mindis = dis[to]; } } if(cval == tval) { --gap[dis[cur]]; if(!gap[dis[cur]]) dis[sc] = pt; dis[cur] = mindis + 1; ++gap[dis[cur]]; } return cval - tval; } Int sap() { memset(gap,0,sizeof(gap)); memset(dis,0,sizeof(dis)); Int ret = 0; gap[sc] = pt; while(dis[sc] < pt) ret += dfs(sc,inf); return sum - ret; } void read() { sum = ans = num = 0; idx = 0; memset(head,-1,sizeof(head)); memset(vis,0,sizeof(vis)); sc = 0, sk = N+1, pt = sk+1; Int tem; int s,e; for(int i=1; i<=N; i++) { scanf("%I64d",&tem); if(tem >= 0) { sum += tem; addnode(sc,i,tem); } else { tem = -tem; addnode(i,sk,tem); } } for(int i=1; i<=M; i++) { scanf("%d%d",&s,&e); addnode(s,e,inf); } } void find(int v) { if(v == pt) return; vis[v] = true; for(int i=head[v]; i!=-1; i=edge[i].next) if(edge[i].val > 0 && !vis[edge[i].to]) find(edge[i].to); } int main() { while(~scanf("%d%d",&N,&M)) { read(); Int ans = sap(); find(0); for(int i=1; i<=N; i++) if(vis[i]) num ++; printf("%I64d %I64d\n",num,ans); } return 0; }
947




被折叠的 条评论
为什么被折叠?



