题目链接:点击查看

题目大意:某公司想要裁员,裁员的标准是如果某人被裁,那么其下属也会被裁,依此类推,每一个人都有一个贡献度,问怎样裁员才能使得最后的贡献度最大并且裁掉人数最少

题目分析:最大权闭合图的模板题,先插个眼:

大佬的博客

简单背一下结论吧:

最大权闭合图 :原边改为正无穷后,添加S和T,S向所有点连正权,所有点向T连负权的绝对值.求最小割,用所有权值为正的点相加之和减掉最小割得到最大权闭合图。

还是需要多理解理解啊,理解懂了就是一个模板题了

最后需要注意的一个小细节就是题目中给出的是有环有向图,所以最后dfs遍历的时候要用vis数组标记一下,不能用v!=fa这样的条件来判断,会RE

代码:

#include<iostream>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<climits>
#include<cmath>
#include<cctype>
#include<stack>
#include<queue>
#include<list>
#include<vector>
#include<set>
#include<map>
#include<sstream>
using namespace std;typedef long long LL;const int inf=0x3f3f3f3f;const int N=5e3+100;struct Edge
{int to,w,next;
}edge[N*N];//边数int head[N],cnt,tot;bool vis[N];void addedge(int u,int v,int w)
{edge[cnt].to=v;edge[cnt].w=w;edge[cnt].next=head[u];head[u]=cnt++;edge[cnt].to=u;edge[cnt].w=0;//反向边边权设置为0edge[cnt].next=head[v];head[v]=cnt++;
}int d[N],now[N];//深度 当前弧优化bool bfs(int s,int t)//寻找增广路
{memset(d,0,sizeof(d));queue<int>q;q.push(s);now[s]=head[s];d[s]=1;while(!q.empty()){int u=q.front();q.pop();for(int i=head[u];i!=-1;i=edge[i].next){int v=edge[i].to;int w=edge[i].w;if(d[v])continue;if(!w)continue;d[v]=d[u]+1;now[v]=head[v];q.push(v);if(v==t)return true;}}return false;
}int dinic(int x,int t,int flow)//更新答案
{if(x==t)return flow;int rest=flow,i;for(i=now[x];i!=-1&&rest;i=edge[i].next){int v=edge[i].to;int w=edge[i].w;if(w&&d[v]==d[x]+1){int k=dinic(v,t,min(rest,w));if(!k)d[v]=0;edge[i].w-=k;edge[i^1].w+=k;rest-=k;}}now[x]=i;return flow-rest;
}void init()
{memset(vis,false,sizeof(vis));memset(head,-1,sizeof(head));cnt=tot=0;
}LL solve(int st,int ed)
{LL ans=0,flow;while(bfs(st,ed))while(flow=dinic(st,ed,inf))ans+=flow;return ans;
}void dfs(int u)
{tot++;vis[u]=true;for(int i=head[u];i!=-1;i=edge[i].next){int v=edge[i].to;if(edge[i].w>0&&!vis[v])dfs(v);}
}int main()
{
//  freopen("input.txt","r",stdin);
//  ios::sync_with_stdio(false);int n,m;while(scanf("%d%d",&n,&m)!=EOF){init();int st=N-1,ed=st-1;LL sum=0;for(int i=1;i<=n;i++){int val;scanf("%d",&val);if(val>0){sum+=val;addedge(st,i,val);}else{addedge(i,ed,-val);}}while(m--){int u,v;scanf("%d%d",&u,&v);addedge(u,v,inf);}LL ans=sum-solve(st,ed);dfs(st);printf("%d %lld\n",tot-1,ans);}return 0;
}

POJ - 2987 Firing(最大权闭合图)相关推荐

  1. poj 2987 Firing (最大权 闭合 图)

    http://poj.org/problem?id=2987 题意: 公司要由于经济 问题 要 裁员工,已知,要采取某个 员工,那么 他的下属也将被 裁去,给出  裁出 n  个员公的 所获的利益 ( ...

  2. POJ 2987 Firing 最大权闭合图

    Description You've finally got mad at "the world's most stupid" employees of yours and dec ...

  3. POJ2987 Firing 最大权闭合图

    详情请参考http://www.cnblogs.com/kane0526/archive/2013/04/05/3001557.html 值得注意的地方,割边会把图分成两部分,一部分和起点相连,另一部 ...

  4. POJ 2987 Firing(最大权闭合图)

    [题目链接] http://poj.org/problem?id=2987 [题目大意] 为了使得公司效率最高,因此需要进行裁员, 裁去不同的人员有不同的效率提升效果,当然也有可能是负的效果, 如果裁 ...

  5. 【POJ - 2987】Firing(最大权闭合图,网络流最小割,输出方案最小,放大权值法tricks)

    题干: You've finally got mad at "the world's most stupid" employees of yours and decided to ...

  6. 【POJ - 2987 】Firing 【最大权闭合图有唯一的 势 】

    You've finally got mad at "the world's most stupid" employees of yours and decided to do s ...

  7. POJ2987-Firing(最大权闭合图)

    Firing Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 10904   Accepted: 3290 Descript ...

  8. luogu P3410 拍照(最大权闭合图转最小割)

    luogu P3410 拍照 最大权闭合图转最小割 要得到最大收益,我们可以用总可能收益减去最小花费,也就是最小割. #include<cstdio> #include<cstrin ...

  9. 【网络流24题】B、太空飞行计划问题(最大权闭合图转最小割、最小割方案输出)

    整理的算法模板合集: ACM模板 B.太空飞行计划问题(最大权闭合图转最小割.最小割方案输出)[省选/NOI- ] P2762 太空飞行计划问题 [问题分析] 最大权闭合图问题,可以转化成最小割问题, ...

最新文章

  1. Python3中raise用法
  2. Django模型层Meta内部类详解
  3. easyui下拉选项多怎么解决_头屑多、头皮痒到底该怎么办?这4个方法帮你解决...
  4. Nginx的rewrite案例之防盗链
  5. 【HTTP】get 和 post 两种基本请求方法
  6. 线性代数 —— 线性基与前缀线性基
  7. 人工智障学习笔记——机器学习(11)PCA降维
  8. 创建用于图像大小调整和裁剪器保持纵横比的ASP.NET控件
  9. 凸优化第七章统计估计 7.5实验设计
  10. 北京市行政边界划分矢量图
  11. 中文字体的FontMetrics解析
  12. sp3485调试:sp3485-TTL转max485模块-TTL转usb模块电脑pc
  13. stm32毕业设计 太空游戏机设计与实现
  14. 解决阿里云redis监听6379,配置规则也将6379端口开放,但是外网仍无法连接6379的问题。
  15. php swoole 教程,PHP Swoole 基本使用
  16. 王峻涛:大萧条中的机会
  17. 在线pdf转换成word转换器怎么样
  18. 打听别人工资的7个话题,让你薪水更高
  19. 网络对抗技术---实验一
  20. 给图像增加一种噪声,构造并利用至少两种低通或高通滤波器实现频率域的滤波,并且显示滤波后的图像

热门文章

  1. Redis中的Cluster总结
  2. Stream流的常见生成方式
  3. 缓存-分布式锁-Redisson简介整合
  4. MapReduce-流量统计求和-排序-JobMain代码和测试运行
  5. 实现根据id查询房源数据的dubbo服务
  6. 缘起 Dubbo ,讲讲 Spring XML Schema 扩展机制
  7. 特朗普承诺改革H-1B签证 留住高技能外国人
  8. Android 应用 之路 百度地图API使用(3)
  9. 《Linux Device Drivers》第十五章 内存映射和DMA——note
  10. CentOS配置Tair