【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 ...
随机推荐
- 转 SQLPLUS中SQL换行执行
权声明:本文为博主原创文章,未经博主允许不得转载. 正常情况下,在SQLPLUS中输入命令时,可以换行,但不能有空格,否则不能执行,会直接返回到SQL>下.但通过命令设置可以实现语句换行时允许有 ...
- HTML5应用缓存与Web Workers
1.什么是应用程序缓存 HTML5引入了应用程序缓存,这意味着web应用可进行缓存,并可在没有因特网链接时进行访问. 2.应用缓存的优势 离线浏览 用户可在应用离线时使用它们 ...
- js添加千位分隔符
function thousandBitSeparator(num){ var re=/\d{1,3}(?=(\d{3})+$)/g; var n1=num.toString().replace(/^ ...
- python学习(day2)
1.常用数据类型及内置方法 1.列表(list) 定义:在中括号[]内存放任意多个值,用逗号隔开. 具体函数和内置方法如下: #定义学生列表,可存放多个学生 students=['a','b','c' ...
- VUE scoped css 局部css内嵌样式方法 >>>
<style scoped> .ivu-carousel >>> button { background-color: buttonface;} .demo-carous ...
- wireshark mqtt协议解析
local tcp_dissector_table = DissectorTable.get("tcp.port") local ws_dissector_table = Diss ...
- C# 如何发送Http请求
HttpSender是一个用于发送Http消息的轻量C#库,使用非常简单,只需要一两行代码,就能完成Http请求的发送 使用 Nuget,搜索 HttpSender 就能找到这个库 这个库的命名空间是 ...
- spark学习(2)---RDD
一.打印RDD内容 https://blog.csdn.net/wengyupeng/article/details/52808503 1.方法 2种方式: 1 rdd.collect().forea ...
- hive纯命令行
vim /etc/profileexport HIVE_HOME=/export/servers/hive...export PATH=:$HIVE_HOME/bin:$PATH 前台启动hive:h ...
- 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 ...