Problem 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

题意:有 n 个人,每个人有一个价值,现在这 n 个人中有 m 个关系 x y,表示 y 是 x 的下级,现在要进行裁员,裁掉一个员工后,也会将他的下级裁掉,现在要求裁掉若干人,使得获得的价值最大,问裁掉的人数与价值

思路:最大权闭合子图裸题

首先记录整个图中所有正点权之和,然后建图

设一个超级源点 S 与一个超级汇点 T,从源点 S 向每个正权点连一条容量为权值的边,每个负权点向汇点 T 连一条容量为权值的绝对值的边,原图中的边容量均设为 INF

在建完图后,利用 Dinic 求最小割,最终人数是最小割中的元素个数,价值是正权值之和-最小割的值

注意使用 long long

Source Program

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<bitset>
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LL long long
#define Pair pair<LL,LL>
LL quickPow(LL a,LL b){ LL res=1; while(b){if(b&1)res*=a; a*=a; b>>=1;} return res; }
LL multMod(LL a,LL b,LL mod){ a%=mod; b%=mod; LL res=0; while(b){if(b&1)res=(res+a)%mod; a=(a<<=1)%mod; b>>=1; } return res%mod;}
LL quickPowMod(LL a, LL b,LL mod){ LL res=1,k=a; while(b){if((b&1))res=multMod(res,k,mod)%mod; k=multMod(k,k,mod)%mod; b>>=1;} return res%mod;}
LL getInv(LL a,LL mod){ return quickPowMod(a,mod-2,mod); }
LL GCD(LL x,LL y){ return !y?x:GCD(y,x%y); }
LL LCM(LL x,LL y){ return x/GCD(x,y)*y; }
const double EPS = 1E-10;
const int MOD = 998244353;
const int N = 200000+5;
const int dx[] = {-1,1,0,0,1,-1,1,1};
const int dy[] = {0,0,-1,1,-1,1,-1,1};
using namespace std;struct Edge{LL from,to;LL cap,flow;Edge(){}Edge(LL from,LL to,LL cap,LL flow):from(from),to(to),cap(cap),flow(flow){}
};
LL n,m;             //结点数,边数(含反向弧)
LL S,T;             //源点、汇点
vector<Edge> edges;  //边表,edges[e]和edges[e^1]互为反向弧
vector<LL> G[N];    //邻接表,G[i][j]表示结点i的第j条边在e数组中的序号
bool vis[N];         //BFS使用,标记一个节点是否被遍历过
LL dis[N];          //dis[i]表从起点s到i点的距离(层次)
LL cur[N];          //cur[i]表当前正访问i节点的第cur[i]条弧
set<LL> cutSet;     //最小割
bool flag;           //是否求最小割
void addEdge(LL from,LL to,LL cap){edges.push_back( Edge(from,to,cap,0) );edges.push_back( Edge(to,from,0,0) );LL m=edges.size();G[from].push_back(m-2);G[to].push_back(m-1);
}
bool BFS(){//构建层次网络memset(vis,0,sizeof(vis));dis[S]=0;vis[S]=true;//将超级源点加入最小割// if(flag)//     cutSet.insert(S);queue<LL> Q;//用来保存节点编号Q.push(S);while(!Q.empty()){LL x=Q.front();Q.pop();for(LL y=0;y<G[x].size();y++){Edge& e=edges[G[x][y]];if(!vis[e.to] && e.cap>e.flow){vis[e.to]=true;dis[e.to]=dis[x]+1;Q.push(e.to);if(flag)//记录最小割元素cutSet.insert(e.to);}}}return vis[T];
}LL DFS(LL x,LL cp){//cp表示从s到x目前为止所有弧的最小残量if(x==T || cp==0)return cp;LL flow=0,newFlow;//flow用来记录从x到t的最小残量for(LL &y=cur[x];y<G[x].size();y++){Edge &e=edges[G[x][y]];if(dis[x]+1==dis[e.to]){LL minn=min(cp,e.cap-e.flow);newFlow=DFS(e.to,minn);if(newFlow>0){e.flow+=newFlow;edges[G[x][y]^1].flow-=newFlow;flow+=newFlow;cp-=newFlow;if(cp==0)break;}}}return flow;
}
LL Dinic(){LL flow=0;while(BFS()){memset(cur,0,sizeof(cur));flow+=DFS(S,INF);}return flow;
}
int main(){scanf("%lld%lld",&n,&m);S=0,T=n+1;//超级源、汇LL sum=0;//正权值之和for(LL i=1;i<=n;i++){LL x;scanf("%lld",&x);if(x>0){addEdge(S,i,x);sum+=x;}elseaddEdge(i,T,-x);}for(LL i=1;i<=m;i++){LL x,y;scanf("%lld%lld",&x,&y);addEdge(x,y,INF);}flag=false;//不统计最小割所属元素LL minCut=Dinic();//计算最小割的值flag=true;//统计最小割所属元素BFS();//构建层次网络printf("%d ",cutSet.size());//最小割个数printf("%lld\n",sum-minCut);//最大权闭合子图的权值return 0;
}

Firing(POJ-2987)相关推荐

  1. Bailian2734 十进制到八进制【入门】(POJ NOI0113-45)

    问题链接:POJ NOI0113-45十进制到八进制 2734:十进制到八进制 总时间限制: 1000ms 内存限制: 65536kB 描述 把一个十进制正整数转化成八进制. 输入 一行,仅含一个十进 ...

  2. Bailian2676 整数的个数【入门】(POJ NOI0105-11)

    问题链接:POJ NOI0105-11 整数的个数 2676:整数的个数 总时间限制: 1000ms 内存限制: 65536kB 描述 给定k(1 < k < 100)个正整数,其中每个数 ...

  3. Bailian4029 数字反转【进制】(POJ NOI0105-29)

    问题链接:POJ NOI0105-29 数字反转 4029:数字反转 总时间限制: 1000ms 内存限制: 65535kB 描述 给定一个整数,请将该数各个位上数字反转得到一个新数.新数也应满足整数 ...

  4. Bailian2735 八进制到十进制【入门】(POJ NOI0113-46)

    问题链接:POJ NOI0113-46 八进制到十进制 2735:八进制到十进制 总时间限制: 1000ms 内存限制: 65536kB 描述 把一个八进制正整数转化成十进制. 输入 一行,仅含一个八 ...

  5. Silver Cow Party (POJ - 3268 )

    Silver Cow Party (POJ - 3268 ) 这道题是我做的最短路专题里的一道题,但我还没做这个,结果比赛就出了,真是.......... 题目: One cow from each ...

  6. 吴昊品游戏核心算法 Round 7 —— 熄灯游戏AI(有人性的Brute Force)(POJ 2811)

    暴力分为两种,一种属于毫无人性的暴力,一种属于有人性 的暴力.前面一种就不说了,对于后面一种情况,我们可以只对其中的部分问题进行枚举,而通过这些子问题而推导到整个的问题中.我称之为有人性的Brute ...

  7. 【二分】Best Cow Fences(poj 2018)

    Best Cow Fences poj 2018 题目大意: 给出一个正整数数列,要你求平均数最大,长度不小于M的字串,结果乘1000取整 输入样例 10 6 6 4 2 10 3 8 5 9 4 1 ...

  8. 昂贵的聘礼(poj 1062)

    Description 年轻的探险家来到了一个印第安部落里.在那里他和酋长的女儿相爱了,于是便向酋长去求亲.酋长要他用10000个金币作为聘礼才答应把女儿嫁给他.探险家拿不出这么多金币,便请求酋长降低 ...

  9. 主席树学习小结(POJ 2104)

    在高中的时候就听到过主席树了,感觉非常高端,在寒假的时候 winter homework中有一题是查找区间第K大的树,当时就开始百度这种网上的博客,发现主席树看不懂,因为那个root[i],还有tx[ ...

  10. A - TOYS(POJ - 2318) 计算几何的一道基础题

    Calculate the number of toys that land in each bin of a partitioned toy box. 计算每一个玩具箱里面玩具的数量 Mom and ...

最新文章

  1. 记录一次生产环境中Redis内存增长异常排查全流程!
  2. Xamarin 2017.9.13发布更新
  3. 面试后总是没有结果的7大原因
  4. 字符串匹配算法Java_如何简单理解字符串匹配算法?
  5. java八种排序算法---直接插入排序
  6. Java防止Xss注入json_XSS的两种攻击方式及五种防御方式
  7. 【计算机组成原理】零碎知识归纳总结
  8. 美国商务部发布软件物料清单 (SBOM) 的最小元素(下)
  9. 2017省夏令营Day6
  10. 怎么用xmind整理我们获取的杂乱的信息
  11. 开心网程炳皓:早期创业公司应该做一根针
  12. ctfshow 做题 MISC入门 模块1-10
  13. Python tkinter库窗口化爬虫
  14. Python可视化-WordCloud生成云词图片
  15. 2016苹果开发者账号注册申请流程链接
  16. 任正非女儿孟晚舟成华为轮值董事长 公司年利润1137亿
  17. JS 如何将 HTML 页面导出为 PDF
  18. C++——NOIP提高组——飞扬的小鸟
  19. 哈佛大学开放课程:《公正:该如何做是好?》1
  20. 13岁男孩偷开公交车 连撞12车撞断电线杆

热门文章

  1. 在pocket pc 2003上播放声音
  2. “要源码上门自取”,结果人真上门了!国内企业再惹争议
  3. 程序员必备的GitHub加速指南,真香!
  4. Java 14 发布了,再也不怕NullPointerException 了!?
  5. 再有人问你为什么MySQL用B+树做索引,就把这篇文章发给她
  6. 从程序员角度分析,到底“12306”的架构到底有多牛逼?
  7. 使用CallableStatement处理Oracle数据库的存储过程
  8. CNN发展历史【从LeNet到DenseNet】
  9. Linux用户管理案例(第二版)
  10. mysql 日期和时间类型