1. /*以核心1为源点,以核心2为汇点建图,跑一遍最大流*/
  2. #include<stdio.h>
  3. #include<string.h>
  4. #include<queue>
  5. using namespace std;
  6. #define N 21000
  7. #define inf 999999999
  8. struct node {
  9. int u,v,w,next;
  10. }bian[N*40];
  11. int head[N],cur[N],gap[N],stac[N],top,n,sink,source,yong,start,dis[N];
  12. void init() {
  13. memset(head,-1,sizeof(head));
  14. top=0;yong=0;
  15. }
  16. void addedge(int u,int v,int w){
  17. bian[yong].u=u;
  18. bian[yong].v=v;
  19. bian[yong].w=w;
  20. bian[yong].next=head[u];
  21. head[u]=yong++;
  22. }
  23. void bfs() {
  24. queue<int>q;
  25. memset(dis,-1,sizeof(dis));
  26. q.push(sink);
  27. dis[sink]=0;
  28. while(!q.empty()) {
  29. int c=q.front();
  30. int i;
  31. q.pop();
  32. for(i=head[c];i!=-1;i=bian[i].next) {
  33. int v=bian[i].v;
  34. if(dis[v]==-1) {
  35. dis[v]=dis[c]+1;
  36. q.push(v);
  37. }
  38. }
  39. }
  40. }
  41. int ISAP() {
  42. int i,sum=0,k;
  43. bfs();
  44. memset(gap,0,sizeof(gap));
  45. for(i=1;i<=n;i++) {
  46. gap[dis[i]]++;
  47. cur[i]=head[i];
  48. }
  49. k=source;
  50. while(dis[source]<n) {
  51. if(k==sink) {
  52. int mi=inf,tep;
  53. for(i=0;i<top;i++){
  54. int e=stac[i];
  55. if(mi>bian[e].w) {
  56. mi=bian[e].w;
  57. tep=i;
  58. }
  59. }
  60. for(i=0;i<top;i++) {
  61. int e=stac[i];
  62. bian[e].w-=mi;
  63. bian[e^1].w+=mi;
  64. }
  65. sum+=mi;
  66. top=tep;
  67. k=bian[stac[top]].u;
  68. }
  69. for(i=cur[k];i!=-1;i=bian[i].next) {
  70. int v=bian[i].v;
  71. if(bian[i].w&&dis[k]==dis[v]+1) {
  72. cur[k]=i;
  73. k=v;
  74. stac[top++]=i;
  75. break;
  76. }
  77. }
  78. if(i==-1) {
  79. int m=n,i;
  80. for(i=head[k];i!=-1;i=bian[i].next) {
  81. int v=bian[i].v;
  82. if(m>dis[v]&&bian[i].w) {
  83. m=dis[v];
  84. cur[k]=i;
  85. }
  86. }
  87. if(--gap[dis[k]]==0)break;
  88. gap[dis[k]=m+1]++;
  89. if(k!=source)
  90. k=bian[stac[--top]].u;
  91. }
  92. }
  93. return sum;
  94. }
  95. int main() {
  96. int i,a,b,c,m;
  97. while(scanf("%d%d",&n,&m)!=EOF) {
  98. source=n+1;
  99. sink=n+2;
  100. init();
  101. for(i=1;i<=n;i++) {
  102. scanf("%d%d",&a,&b);
  103. addedge(source,i,a);
  104. addedge(i,source,0);
  105. addedge(i,sink,b);
  106. addedge(sink,i,0);
  107. }
  108. while(m--) {
  109. scanf("%d%d%d",&a,&b,&c);
  110. addedge(a,b,c);
  111. addedge(b,a,c);
  112. }
  113. n+=2;
  114. printf("%d\n",ISAP());
  115. }
  116. return 0;
  117. }

poj 3469 最小割模板sap+gap+弧优化的更多相关文章

  1. POJ 3469 最小割 Dual Core CPU

    题意: 一个双核CPU上运行N个模块,每个模块在两个核上运行的费用分别为Ai和Bi. 同时,有M对模块需要进行数据交换,如果这两个模块不在同一个核上运行需要额外花费. 求运行N个模块的最小费用. 分析 ...

  2. 网络流SAP+gap+弧优化算法

    poj1273 Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 54962   Accept ...

  3. 网络流 最大流—最小割 之SAP算法 详解

    首先引入几个新名词: 1.距离标号: 所谓距离标号 ,就是某个点到汇点的最少的弧的数量(即边权值为1时某个点到汇点的最短路径长度). 设点i的标号为level[i],那么如果将满足level[i]=l ...

  4. poj 2125(最小割)

    题目链接:http://poj.org/problem?id=2125 思路:将最小点权覆盖转化为最小割模型,于是拆点建图,将点i拆成i,i+n,其中vs与i相连,边容量为w[i]-,i+n与vt相连 ...

  5. 最大流/最小割模板(isap) POJ1273

    isap模板核心代码: //d[]为距离标号数组,d[i]表示节点i到汇点的距离 //gap[]为GAP优化数组,gap[i]表示到汇点距离为i的节点个数 int dfs(int k,int flow ...

  6. poj2914无向图的最小割模板

    题意:给出无向图的点,边,权值.求最小割. 思路:根据题目规模,最大流算法会超时. 网上参考的模板代码. 代码: /*最小割集◎Stoer-Wagner算法:一个无向连通网络,去掉一个边集可以使其变成 ...

  7. poj 3204(最小割--关键割边)

    Ikki's Story I - Road Reconstruction Time Limit: 2000MS   Memory Limit: 131072K Total Submissions: 7 ...

  8. ISAP 最大流 最小割 模板

    虽然这道题用最小割没有做出来,但是这个板子还是很棒: #include<stdio.h> #include<math.h> #include<string.h> # ...

  9. poj2914 Minimum Cut 全局最小割模板题

    Minimum Cut Time Limit: 10000MS   Memory Limit: 65536K Total Submissions: 8324   Accepted: 3488 Case ...

随机推荐

  1. bzoj 1053 [ HAOI 2007 ] 反素数ant ——暴搜

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1053 试图打表找规律,但无果... 看TJ了,暴搜: 注意参数 w 是 long long. ...

  2. vmware centos7 没有网络设备

    vmware centos7 没有网络设备 选择VMware 虚拟机模拟器为CentOS 64 即可;

  3. openStack 主机流量计运行状态 随笔记录

    root@ruiy-controller:~# ifconfigeth0      Link encap:Ethernet  HWaddr 0c:c4:7a:0d:97:2c          ine ...

  4. HDU3085 Nightmare Ⅱ

    题目: Last night, little erriyue had a horrible nightmare. He dreamed that he and his girl friend were ...

  5. Visual Studio q启动卡顿

    在开发人员CMD下面执行 Devenv.exe /ResetSettings ,然后顺利打开 总发现vs2015经常把cpu给占满了,导致电脑卡的不要不要的.这是CodeLens引起的,因为装了VAs ...

  6. POJ 1160 DP

    题目: poj 1160 题意: 给你n个村庄和它的坐标,现在要在其中一些村庄建m个邮局,想要村庄到最近的邮局距离之和最近. 分析: 这道题.很经典的dp dp[i][j]表示建第i个邮局,覆盖到第j ...

  7. .net core2.0 自定义中间件

    一.中间件(Middleware) 中间件是被组装成一个应用程序管道来处理请求和响应的软件组件. 二.编写SimpleMiddleware using Microsoft.AspNetCore.Htt ...

  8. elasticsearch模板 template

    https://elasticsearch.cn/article/335 elasticsearch模板 template 可以考虑的学习点: mapping的 _default_类型 动态模板:dy ...

  9. windows 装XP系统

    笔记本型号:HPCQ40-506AX 1.在BIOS中更改启动顺序:将USB设为第一启动项2.插入装有PE系统的USB设备3.开机后一直按F124.到达选择系统界面,目前我的HPCQ40用其他系统进去 ...

  10. 利用 html+css 画同心圆(concentric circles)——绝对布局与相对布局

    一.css 绘制圆 #circle { width: 300px; height: 300px; background-color: #000000; border-radius: 300px; } ...