一道裸的最小割的题,写一下只是练练手。

表示被卡M,RE不开心。一道裸题至于吗?

再次复习一下最大权闭合子图:

1.每一个点若为正权,与源点连一条容量为绝对值权值的边。否则连向汇点一条容量为绝对值权值的边

2.如果有选了A点才能选B点的约束条件,且违背这个约束条件有C的代价,则从A点向B点连一条容量为C的边(如果不能违背,则连一条容量为INF的边)

3.设源点出度为C,最大流的值为D。答案为C-D。

  1. #include<cstdio>
  2. #include<queue>
  3. #include<algorithm>
  4. using namespace std ;
  5.  
  6. namespace DINIC {
  7.  
  8. const int MAXVn = * + ;
  9. const int MAXEn = * + ;
  10. int Vn ;
  11.  
  12. struct edge {
  13. int p ;
  14. int c ;
  15. edge * nxt ;
  16. edge * brother ;
  17. } ;
  18.  
  19. edge E [ MAXEn * ] ;
  20. namespace E_SET { edge * T = E ; } ;
  21. edge * V [ MAXVn ] ;
  22.  
  23. int S , T ;
  24.  
  25. void add_edge ( const int a , const int b , const int f ) {
  26. using E_SET :: T ;
  27. T -> p = b ; T -> c = f ; T -> nxt = V [ a ] ; V [ a ] = T ++ ;
  28. T -> p = a ; T -> c = ; T -> nxt = V [ b ] ; V [ b ] = T ++ ;
  29. V [ a ] -> brother = V [ b ] ; V [ b ] -> brother = V [ a ] ;
  30. }
  31.  
  32. edge * cur [ MAXVn ] ;
  33. int dis [ MAXVn ] ;
  34.  
  35. bool bfs ( ) {
  36. queue < int > q ;
  37. fill ( dis , dis + Vn , - ) ;
  38. copy ( V , V + Vn , cur ) ;
  39. q . push ( S ) ; dis [ S ] = ;
  40. while ( ! q . empty () ) {
  41. const int o = q . front () ; q . pop () ;
  42. for ( edge * v = V [ o ] ; v != ; v = v -> nxt )
  43. if ( v -> c != && dis [ v -> p ] == - ) {
  44. dis [ v -> p ] = dis [ o ] + ;
  45. q . push ( v -> p ) ;
  46. }
  47. }
  48. return dis [ T ] != - ;
  49. }
  50.  
  51. int dfs ( const int o , int flow ) {
  52. if ( o == T || flow == ) return flow ;
  53. int f , ans = ;
  54. for ( edge * & v = cur [ o ] ; v != ; v = v -> nxt )
  55. if ( dis [ o ] + == dis [ v -> p ] &&
  56. ( f = dfs ( v -> p , min ( flow , v -> c ) ) ) != ) {
  57. v -> c -= f ; v -> brother -> c += f ;
  58. ans += f ; flow -= f ;
  59. if ( flow == ) break ;
  60. }
  61. return ans ;
  62. }
  63.  
  64. int dinic ( ) {
  65. int ans = ;
  66. while ( bfs ( ) ) ans += dfs ( S , << ) ;
  67. return ans ;
  68. }
  69.  
  70. }
  71.  
  72. int M , N ;
  73. int sum ;
  74.  
  75. int main () {
  76.  
  77. using namespace DINIC ;
  78. scanf ( "%d%d" , & N , & M ) ;
  79. Vn = M + N + ; //in && out && S && T
  80. S = ;
  81. T = ;
  82.  
  83. #define WORK(a) ((a)+2)
  84. #define MACHINE(a) ((a)+N+2)
  85.  
  86. for ( int i = ; i < N ; ++ i ) {
  87. int value_of_w , num_of_w ; scanf ( "%d%d" , & value_of_w , & num_of_w ) ;
  88. sum += value_of_w ;
  89. add_edge ( S , WORK(i) , value_of_w ) ;
  90. while ( num_of_w -- ) {
  91. int n , pay ;
  92. scanf ( "%d%d" , & n , & pay ) ;
  93. n -= ;
  94. add_edge ( WORK(i) , MACHINE(n) , pay ) ;
  95. }
  96. }
  97. for ( int i = ; i < M ; ++ i ) {
  98. int pay ; scanf ( "%d" , & pay ) ;
  99. add_edge ( MACHINE(i) , T , pay ) ;
  100. }
  101.  
  102. #undef WORK
  103. #undef MACHINE
  104.  
  105. printf ( "%d\n" , sum - dinic () ) ;
  106.  
  107. return ;
  108.  
  109. }

bzoj1391 最大权闭合子图(also最小割、网络流)的更多相关文章

  1. 【BZOJ】1497: [NOI2006]最大获利 最大权闭合子图或最小割

    [题意]给定n个点,点权为pi.m条边,边权为ci.选择一个点集的收益是在[点集中的边权和]-[点集点权和],求最大获利.n<=5000,m<=50000,0<=ci,pi<= ...

  2. LuoguP2762 太空飞行计划问题(最大权闭合子图,最小割)

    题目描述 W 教授正在为国家航天中心计划一系列的太空飞行.每次太空飞行可进行一系列商业性实验而获取利润.现已确定了一个可供选择的实验集合E={E1,E2,…,Em},和进行这些实验需要使用的全部仪器的 ...

  3. Petya and Graph/最大权闭合子图、最小割

    原题地址:https://codeforces.com/contest/1082/problem/G G. Petya and Graph time limit per test 2 seconds ...

  4. HDU 3917 最大权闭合图 求最小割

    具体参考http://blog.csdn.net/power721/article/details/6665750 TODO //#pragma comment(linker, "/STAC ...

  5. Less Time, More profit 最大权闭合子图(最大流最小割)

    The city planners plan to build N plants in the city which has M shops. Each shop needs products fro ...

  6. BZOJ 4873 [Shoi2017]寿司餐厅 | 网络流 最大权闭合子图

    链接 BZOJ 4873 题解 当年的省选题--还记得蒟蒻的我Day1 20分滚粗-- 这道题是个最大权闭合子图的套路题.严重怀疑出题人就是先画好了图然后照着图编了个3000字的题面.和我喜欢的妹子当 ...

  7. 【POJ 2987】Firing (最小割-最大权闭合子图)

    裁员 [问题描述] 在一个公司里,老板发现,手下的员工很多都不务正业,真正干事员工的没几个,于是老板决定大裁员,每开除一个人,同时要将其下属一并开除,如果该下属还有下属,照斩不误.给出每个人的贡献值和 ...

  8. BZOJ 1565 NOI2009 植物大战僵尸 topo+最小割(最大权闭合子图)

    题目链接:https://www.luogu.org/problemnew/show/P2805(bzoj那个实在是有点小小的辣眼睛...我就把洛谷的丢出来吧...) 题意概述:给出一张有向图,这张有 ...

  9. 洛谷 P4174 [NOI2006]最大获利 && 洛谷 P2762 太空飞行计划问题 (最大权闭合子图 && 最小割输出任意一组方案)

    https://www.luogu.org/problemnew/show/P4174 最大权闭合子图的模板 每个通讯站建一个点,点权为-Pi:每个用户建一个点,点权为Ci,分别向Ai和Bi对应的点连 ...

随机推荐

  1. springboot源码解析 - 构建SpringApplication

    1 package com.microservice.framework; 2 3 import org.springframework.boot.SpringApplication; 4 impor ...

  2. ubuntu启动eclipse时出错cannot open display

    由于要学习hadoop,就在ubuntu下创建了一个hadoop用户,但是eclipse是在naomi用户下装的,在root和naomi用户下都能正常启动,但是一旦切换到hadoop用户,试着启动ec ...

  3. 巧用cssText属性批量操作样式

    给一个HTML元素设置css属性,如 1 2 3 4 var head= document.getElementById("head"); head.style.width = & ...

  4. 每天一个小算法(Shell Sort2)

    希尔排序: 伪代码: input: an array a of length n with array elements numbered 0 to n − 1 inc ← round(n/2) wh ...

  5. BZOJ 2006 超级钢琴(划分树+优先队列)

    题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=2006 题意: 给出一个数列A,L,R,构造出一个新的集合,集合中的数字为A中任意连续t( ...

  6. git使用ssh协议,生成公钥和私钥,并指定私钥

    http://superuser.com/questions/232373/how-to-tell-git-which-private-key-to-use In ~/.ssh/config, add ...

  7. Sales Order Flow Statuses

    OE_ORDER_LINES_ALL.flow_status_code column values execute the below query to see the values. SELECT ...

  8. JSON 之 SuperObject(6): 方法

    SuperObject 的 JSON 对象中还可以包含 "方法", 这太有意思了; 其方法的格式是: procedure Method(const This, Params: IS ...

  9. cocos2d-x 获取图片的某像素点的RGBA颜色 -转

    cocos2d-x 获取图片的某像素点的RGBA颜色  原文:http://www.cnblogs.com/jaoye/archive/2013/02/19/2916501.html 没做过 太多的图 ...

  10. how bootstrap fit into our website design?

    在web相关技术中,HTML代表了content,structure, CSS则负责styling,决定内容是如何展示的,javascript则主要负责interactive, behaviour. ...