题意:有n条线段,每条有起点,终点和一个权值

要求选取一些线段,使它们的权值和最大,并且使每一个点被覆盖不超过k次

1 ≤ K ≤ N ≤ 200

1 ≤ ai < bi ≤ 100,000, 1 ≤ wi ≤ 100,000

思路:RYZ作业

费用流(经典?)模型之一

离散化后对于线段(a[i],b[i],w[i]),从a[i]到b[i]连容量为1,费用为w[i]的边

(i,i+1)之间都连容量为K,费用为0的边,以达到限制max<=k的效果

S——>1和N——>T之间都连容量为K,费用为0的边

跑最大费用最大流

  1. var head,vet,next,len1,len2,a,b,c,d,fan:array[..]of longint;
  2. inq:array[..]of boolean;
  3. q:array[..]of longint;
  4. pre:array[..,..]of longint;
  5. dis:array[..]of longint;
  6. n,m,k,up,i,tot,ans,s,source,src,cas,v,x,y:longint;
  7.  
  8. procedure swap(var x,y:longint);
  9. var t:longint;
  10. begin
  11. t:=x; x:=y; y:=t;
  12. end;
  13.  
  14. procedure qsort(l,r:longint);
  15. var i,j,mid:longint;
  16. begin
  17. i:=l; j:=r; mid:=d[(l+r)>>];
  18. repeat
  19. while mid>d[i] do inc(i);
  20. while mid<d[j] do dec(j);
  21. if i<=j then
  22. begin
  23. swap(d[i],d[j]);
  24. inc(i); dec(j);
  25. end;
  26. until i>j;
  27. if l<j then qsort(l,j);
  28. if i<r then qsort(i,r);
  29. end;
  30.  
  31. function min(x,y:longint):longint;
  32. begin
  33. if x<y then exit(x);
  34. exit(y);
  35. end;
  36.  
  37. function hash(x:longint):longint;
  38. var l,r,mid:longint;
  39. begin
  40. l:=; r:=up;
  41. while l<=r do
  42. begin
  43. mid:=(l+r)>>;
  44. if d[mid]=x then exit(mid);
  45. if d[mid]<x then l:=mid+
  46. else r:=mid-;
  47. end;
  48. end;
  49.  
  50. procedure add(a,b,c,d:longint);
  51. begin
  52. inc(tot);
  53. next[tot]:=head[a];
  54. vet[tot]:=b;
  55. len1[tot]:=c;
  56. len2[tot]:=d;
  57. head[a]:=tot;
  58.  
  59. inc(tot);
  60. next[tot]:=head[b];
  61. vet[tot]:=a;
  62. len1[tot]:=;
  63. len2[tot]:=-d;
  64. head[b]:=tot;
  65. end;
  66.  
  67. function spfa:boolean;
  68. var u,e,v,i,top:longint;
  69. begin
  70. for i:= to s do
  71. begin
  72. dis[i]:=-(maxlongint>>);
  73. inq[i]:=false;
  74. end;
  75. top:=; q[]:=source; dis[source]:=; inq[source]:=true;
  76. while top> do
  77. begin
  78. u:=q[top]; dec(top); inq[u]:=false;
  79. e:=head[u];
  80. while e<> do
  81. begin
  82. v:=vet[e];
  83. if (len1[e]>)and(dis[u]+len2[e]>dis[v]) then
  84. begin
  85. dis[v]:=dis[u]+len2[e];
  86. pre[v,]:=u;
  87. pre[v,]:=e;
  88. if not inq[v] then
  89. begin
  90. inc(top); q[top]:=v; inq[v]:=true;
  91. end;
  92. end;
  93. e:=next[e];
  94. end;
  95. end;
  96. if dis[src]=-(maxlongint>>) then exit(false);
  97. exit(true);
  98. end;
  99.  
  100. procedure mcf;
  101. var k,e,t:longint;
  102. begin
  103. k:=src; t:=maxlongint;
  104. while k<>source do
  105. begin
  106. t:=min(t,len1[pre[k,]]);
  107. k:=pre[k,];
  108. end;
  109. k:=src;
  110. while k<>source do
  111. begin
  112. e:=pre[k,];
  113. len1[e]:=len1[e]-t;
  114. len1[fan[e]]:=len1[fan[e]]+t;
  115. ans:=ans+t*len2[e];
  116. k:=pre[k,];
  117. end;
  118. end;
  119.  
  120. begin
  121. assign(input,'poj3680.in'); reset(input);
  122. assign(output,'poj3680.out'); rewrite(output);
  123. readln(cas);
  124. for i:= to do
  125. if i and = then fan[i]:=i+
  126. else fan[i]:=i-;
  127. for v:= to cas do
  128. begin
  129. for i:= to s do head[i]:=;
  130. s:=; tot:=; ans:=;
  131. //fillchar(head,sizeof(head),);
  132. read(n,k); m:=;
  133. for i:= to n do
  134. begin
  135. read(a[i],b[i],c[i]);
  136. inc(m); d[m]:=a[i];
  137. inc(m); d[m]:=b[i];
  138. end;
  139. qsort(,m);
  140. up:=;
  141. for i:= to m do
  142. if d[i]<>d[up] then begin inc(up); d[up]:=d[i]; end;
  143. for i:= to n do
  144. begin
  145. x:=hash(a[i]); y:=hash(b[i]);
  146. add(x,y,,c[i]);
  147. end;
  148. source:=up+; src:=up+; s:=up+;
  149. add(source,,k,);
  150. add(up,src,k,);
  151. for i:= to up- do add(i,i+,k,);
  152. while spfa do mcf;
  153. writeln(ans);
  154. end;
  155. close(input);
  156. close(output);
  157. end.

【POJ3680】Intervals(费用流)的更多相关文章

  1. poj3680 Intervals (费用流)

    建图((x,y,c,l)表示x到y,费用c,流量l) (S,1,0,K) (i,i+1,0,K) 这个边上的流量,表示i还可以被覆盖的次数 (N,T,0,K) (i,j,w,1)对于权值为w的区间[i ...

  2. poj 3680 Intervals(费用流)

    http://poj.org/problem?id=3680 巧妙的构图. 题目:给定N个区间(ai,bi)权值wi,求最大权和且每个点最多覆盖K次. 构图:将区间端点离散化,将第i个点连第i+1个点 ...

  3. poj3680 Intervals 区间k覆盖问题 最小费用最大流 建图巧妙

    /** 题目:poj3680 Intervals 区间k覆盖问题 最小费用最大流 建图巧妙 链接:http://poj.org/problem?id=3680 题意:给定n个区间,每个区间(ai,bi ...

  4. POJ-3680:Intervals (费用流)

    You are given N weighted open intervals. The ith interval covers (ai, bi) and weighs wi. Your task i ...

  5. POJ3680 Intervals —— 区间k覆盖问题(最小费用流)

    题目链接:https://vjudge.net/problem/POJ-3680 Intervals Time Limit: 5000MS   Memory Limit: 65536K Total S ...

  6. hdu-5988 Coding Contest(费用流)

    题目链接: Coding Contest Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: 65536/65536 K (Java/Ot ...

  7. POJ2195 Going Home[费用流|二分图最大权匹配]

    Going Home Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 22088   Accepted: 11155 Desc ...

  8. BZOJ3130: [Sdoi2013]费用流[最大流 实数二分]

    3130: [Sdoi2013]费用流 Time Limit: 10 Sec  Memory Limit: 128 MBSec  Special JudgeSubmit: 960  Solved: 5 ...

  9. 洛谷 1004 dp或最大费用流

    思路: dp方法: 设dp[i][j][k][l]为两条没有交叉的路径分别走到(i,j)和(k,l)处最大价值. 则转移方程为 dp[i][j][k][l]=max(dp[i-1][j][k-1][l ...

随机推荐

  1. 转 SQLPLUS中SQL换行执行

    权声明:本文为博主原创文章,未经博主允许不得转载. 正常情况下,在SQLPLUS中输入命令时,可以换行,但不能有空格,否则不能执行,会直接返回到SQL>下.但通过命令设置可以实现语句换行时允许有 ...

  2. HTML5应用缓存与Web Workers

    1.什么是应用程序缓存      HTML5引入了应用程序缓存,这意味着web应用可进行缓存,并可在没有因特网链接时进行访问. 2.应用缓存的优势      离线浏览   用户可在应用离线时使用它们 ...

  3. js添加千位分隔符

    function thousandBitSeparator(num){ var re=/\d{1,3}(?=(\d{3})+$)/g; var n1=num.toString().replace(/^ ...

  4. python学习(day2)

    1.常用数据类型及内置方法 1.列表(list) 定义:在中括号[]内存放任意多个值,用逗号隔开. 具体函数和内置方法如下: #定义学生列表,可存放多个学生 students=['a','b','c' ...

  5. VUE scoped css 局部css内嵌样式方法 >>>

    <style scoped> .ivu-carousel >>> button { background-color: buttonface;} .demo-carous ...

  6. wireshark mqtt协议解析

    local tcp_dissector_table = DissectorTable.get("tcp.port") local ws_dissector_table = Diss ...

  7. C# 如何发送Http请求

    HttpSender是一个用于发送Http消息的轻量C#库,使用非常简单,只需要一两行代码,就能完成Http请求的发送 使用 Nuget,搜索 HttpSender 就能找到这个库 这个库的命名空间是 ...

  8. spark学习(2)---RDD

    一.打印RDD内容 https://blog.csdn.net/wengyupeng/article/details/52808503 1.方法 2种方式: 1 rdd.collect().forea ...

  9. hive纯命令行

    vim /etc/profileexport HIVE_HOME=/export/servers/hive...export PATH=:$HIVE_HOME/bin:$PATH 前台启动hive:h ...

  10. JFinal项目eclipse出现the table mapping of model: com.gexin.model.scenic.Scenic not exists or the ActiveRecordPlugin not start.

    JFinal项目eclipse出现the table mapping of model: com.gexin.model.scenic.Scenic not exists or the ActiveR ...