题意:有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的边

跑最大费用最大流

 var head,vet,next,len1,len2,a,b,c,d,fan:array[..]of longint;
inq:array[..]of boolean;
q:array[..]of longint;
pre:array[..,..]of longint;
dis:array[..]of longint;
n,m,k,up,i,tot,ans,s,source,src,cas,v,x,y:longint; procedure swap(var x,y:longint);
var t:longint;
begin
t:=x; x:=y; y:=t;
end; procedure qsort(l,r:longint);
var i,j,mid:longint;
begin
i:=l; j:=r; mid:=d[(l+r)>>];
repeat
while mid>d[i] do inc(i);
while mid<d[j] do dec(j);
if i<=j then
begin
swap(d[i],d[j]);
inc(i); dec(j);
end;
until i>j;
if l<j then qsort(l,j);
if i<r then qsort(i,r);
end; function min(x,y:longint):longint;
begin
if x<y then exit(x);
exit(y);
end; function hash(x:longint):longint;
var l,r,mid:longint;
begin
l:=; r:=up;
while l<=r do
begin
mid:=(l+r)>>;
if d[mid]=x then exit(mid);
if d[mid]<x then l:=mid+
else r:=mid-;
end;
end; procedure add(a,b,c,d:longint);
begin
inc(tot);
next[tot]:=head[a];
vet[tot]:=b;
len1[tot]:=c;
len2[tot]:=d;
head[a]:=tot; inc(tot);
next[tot]:=head[b];
vet[tot]:=a;
len1[tot]:=;
len2[tot]:=-d;
head[b]:=tot;
end; function spfa:boolean;
var u,e,v,i,top:longint;
begin
for i:= to s do
begin
dis[i]:=-(maxlongint>>);
inq[i]:=false;
end;
top:=; q[]:=source; dis[source]:=; inq[source]:=true;
while top> do
begin
u:=q[top]; dec(top); inq[u]:=false;
e:=head[u];
while e<> do
begin
v:=vet[e];
if (len1[e]>)and(dis[u]+len2[e]>dis[v]) then
begin
dis[v]:=dis[u]+len2[e];
pre[v,]:=u;
pre[v,]:=e;
if not inq[v] then
begin
inc(top); q[top]:=v; inq[v]:=true;
end;
end;
e:=next[e];
end;
end;
if dis[src]=-(maxlongint>>) then exit(false);
exit(true);
end; procedure mcf;
var k,e,t:longint;
begin
k:=src; t:=maxlongint;
while k<>source do
begin
t:=min(t,len1[pre[k,]]);
k:=pre[k,];
end;
k:=src;
while k<>source do
begin
e:=pre[k,];
len1[e]:=len1[e]-t;
len1[fan[e]]:=len1[fan[e]]+t;
ans:=ans+t*len2[e];
k:=pre[k,];
end;
end; begin
assign(input,'poj3680.in'); reset(input);
assign(output,'poj3680.out'); rewrite(output);
readln(cas);
for i:= to do
if i and = then fan[i]:=i+
else fan[i]:=i-;
for v:= to cas do
begin
for i:= to s do head[i]:=;
s:=; tot:=; ans:=;
//fillchar(head,sizeof(head),);
read(n,k); m:=;
for i:= to n do
begin
read(a[i],b[i],c[i]);
inc(m); d[m]:=a[i];
inc(m); d[m]:=b[i];
end;
qsort(,m);
up:=;
for i:= to m do
if d[i]<>d[up] then begin inc(up); d[up]:=d[i]; end;
for i:= to n do
begin
x:=hash(a[i]); y:=hash(b[i]);
add(x,y,,c[i]);
end;
source:=up+; src:=up+; s:=up+;
add(source,,k,);
add(up,src,k,);
for i:= to up- do add(i,i+,k,);
while spfa do mcf;
writeln(ans);
end;
close(input);
close(output);
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. ORA-00445: Background Process "xxxx" Did Not Start After 120 Seconds

    Recent linux kernels have a feature called Address Space Layout Randomization (ASLR).ASLR  is a feat ...

  2. Matrix Transformation codechef 数学题

    https://www.codechef.com/problems/MTRNSFRM 我只能说codechef的题好劲爆,这题居然是easy的题,太可怕了.而且还有一点就是codechef的题解很难看 ...

  3. hdu 5036 Explosion bitset优化floyd

    http://acm.hdu.edu.cn/showproblem.php?pid=5036 题意就是给定一副有向图,现在需要走遍这n个顶点,一开始出发的顶点是这n个之中的随便一个. 如果走了1,那么 ...

  4. 线程池机制使nginx性能提高9倍

    原文标题:Thread Pools in NGINX Boost Performance 9x! 原文官方地址:https://www.nginx.com/blog/thread-pools-boos ...

  5. 从源码对比DefaultServeMux 与 gorilla/mux

    从源码对比DefaultServeMux 与 gorilla/mux DefaultServeMux Golang自带的net/http库中包含了DefaultServeMux方法,以此可以搭建一个稳 ...

  6. 原生jsonp跨域

    <script> // jsonp跨域原生写法 var script = document.createElement('script'); script.src = 'http://19 ...

  7. Java处理ZIP文件的解决方案——Zip4J(不解压直接通过InputStream形式读取其中的文件,解决中文乱码)

    一.JDK内置操作Zip文件其实,在JDK中已经存在操作ZIP的工具类:ZipInputStream. 基本使用: public static Map<String, String> re ...

  8. 使用VS Code调试Flutter(检查用户页面)

    官方提供的是Flutter Widget Inspector,详见https://flutterchina.club/inspector/ 我用的是另外一种好用的调试工具 Dart DevTools ...

  9. 允许IIS下载无后缀文件及“请求的内容似乎是脚本,因而将无法由静态文件处理程序来处理。”的解决方法

    1)增加MIME类型 ,如下 application/octet-stream 2)注意是"." , ".*"则适用于任何有文件后缀时使用,无后缀请不要加*

  10. C# 使用MongoDB(学习记录)

    1)下载MongoDB https://www.mongodb.com/download-center#community 2)在D盘新建Data->db 3)执行命令 mongod --dbp ...