【POJ3680】Intervals(费用流)
题意:有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(费用流)的更多相关文章
- 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 ...
- poj 3680 Intervals(费用流)
http://poj.org/problem?id=3680 巧妙的构图. 题目:给定N个区间(ai,bi)权值wi,求最大权和且每个点最多覆盖K次. 构图:将区间端点离散化,将第i个点连第i+1个点 ...
- poj3680 Intervals 区间k覆盖问题 最小费用最大流 建图巧妙
/** 题目:poj3680 Intervals 区间k覆盖问题 最小费用最大流 建图巧妙 链接:http://poj.org/problem?id=3680 题意:给定n个区间,每个区间(ai,bi ...
- POJ-3680:Intervals (费用流)
You are given N weighted open intervals. The ith interval covers (ai, bi) and weighs wi. Your task i ...
- POJ3680 Intervals —— 区间k覆盖问题(最小费用流)
题目链接:https://vjudge.net/problem/POJ-3680 Intervals Time Limit: 5000MS Memory Limit: 65536K Total S ...
- hdu-5988 Coding Contest(费用流)
题目链接: Coding Contest Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Ot ...
- POJ2195 Going Home[费用流|二分图最大权匹配]
Going Home Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 22088 Accepted: 11155 Desc ...
- BZOJ3130: [Sdoi2013]费用流[最大流 实数二分]
3130: [Sdoi2013]费用流 Time Limit: 10 Sec Memory Limit: 128 MBSec Special JudgeSubmit: 960 Solved: 5 ...
- 洛谷 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 ...
随机推荐
- 支付宝添加scheme的方法
点击项目名称,点击“Info”选项卡,在“URL Types”选项中,点击“+”,在“URL Schemes”中输入“myAlipay”.“myAlipay”来自于文件“APViewControlle ...
- E. Comments dfs模拟
http://codeforces.com/contest/747/problem/E 首先,把字符串变成这个样子. hello,2,ok,0,bye,0,test,0,one,1,two,2,a,0 ...
- Tcpdump的用法
见 超级详细Tcpdump 的用法 http://www.itshouce.com.cn/linux/linux-tcpdump.html
- XDocument
XDocument学习(Winform) using System; using System.Collections.Generic; using System.ComponentModel; us ...
- 解决ASP.NET Core通过docker-compose up启动应用无法配置https的解决办法
错误重现一下: 新建了一个ASP.NET Core应用,在VS2017下添加Docker支持,选择Linux环境 然后再给这个web应用再右键添加容器业务流程协调程序支持,然后解决方案就多了一个doc ...
- AJPFX关于代码块的总结
代码块: { 执行语句; }(1) 当出现在局部位置时, 为局部代码块. 局部位置: 如语句块中, 函数中, 构造代码块中, 静 ...
- svn 使用手册
版本控制器:SVN 1 开发中的实际问题 1.1 小明负责的模块就要完成了,就在即将Release之前的一瞬间,电脑突然蓝屏,硬盘光荣牺牲!几个月来的努力付之东流——需求之一:备份! 1.2 这个项目 ...
- 支持中英文和国旗的android国家代码/国际电话区号选择器
最近在做app登录的时候,因为需要支持国外手机号注册和登录,所以就涉及到国际电话区号的选择.在github上面找了一下,国家名称基本都是只有英文版本,而手动的去把中文一个个加上实在是一件费时费力的事情 ...
- Apache Tomcat 之路(一 基本概念)
关于apache tomcat 基本概念(https://tomcat.apache.org/tomcat-7.0-doc/index.html) 1.tomcat 是servlet/jsp 容器,对 ...
- WNDCLASS和WNDCLASSEX
typedef struct { UINT cbSize; UINT style; WNDPROC lpfnWndProc; int cbClsExtra; int cbWndExtra; HINST ...