网络流(最大费用最大流) :POJ 3680 Intervals
Time Limit: 5000MS | Memory Limit: 65536K | |
Total Submissions: 7218 | Accepted: 3011 |
Description
You are given N weighted open intervals. The ith interval covers (ai, bi) and weighs wi. Your task is to pick some of the intervals to maximize the total weights under the limit that no point in the real axis is covered more than k times.
Input
The first line of input is the number of test case.
The first line of each test case contains two integers, N and K (1 ≤ K ≤ N ≤ 200).
The next N line each contain three integers ai, bi, wi(1 ≤ ai < bi ≤ 100,000, 1 ≤ wi ≤ 100,000) describing the intervals.
There is a blank line before each test case.
Output
For each test case output the maximum total weights in a separate line.
Sample Input
4 3 1
1 2 2
2 3 4
3 4 8 3 1
1 3 2
2 3 4
3 4 8 3 1
1 100000 100000
1 2 3
100 200 300 3 2
1 100000 100000
1 150 301
100 200 300
Sample Output
14
12
100000
100301
这题就是求最大区间K覆盖,最大费用最大流走起~~~
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
using namespace std;
const int INF=;
const int maxn=,maxm=;
int cnt,fir[maxn],nxt[maxm],to[maxm],cap[maxm],val[maxm],dis[maxn],path[maxn];
struct data{
int l,r,v;
bool operator <(const data A)const{
if(l!=A.l)
return l<A.l;
return r<A.r;
}
}ar[maxn]; struct data2{
int a,pos;
bool operator <(const data2 A)const{
if(a!=A.a)
return a<A.a;
return pos<A.pos;
}
}br[maxn]; void addedge(int a,int b,int c,int v)
{
nxt[++cnt]=fir[a];to[cnt]=b;cap[cnt]=c;val[cnt]=v;fir[a]=cnt;
}
int S,T;
int Spfa()
{
queue<int>q;
memset(dis,-,sizeof(dis));
q.push(S);dis[S]=;
while(!q.empty())
{
int node=q.front();q.pop();
for(int i=fir[node];i;i=nxt[i])
if(cap[i]&&dis[node]+val[i]>dis[to[i]]){
dis[to[i]]=val[i]+dis[node];
path[to[i]]=i;
q.push(to[i]);
}
}
return dis[T]==-?:dis[T];
} int Aug()
{
int p=T,f=INF;
while(p!=S)
{
f=min(f,cap[path[p]]);
p=to[path[p]^];
}
p=T;
while(p!=S)
{
cap[path[p]]-=f;
cap[path[p]^]+=f;
p=to[path[p]^];
}
return f;
} int MCMF()
{
int ret=,d;
while(d=Spfa())
ret+=Aug()*d;
return ret;
} int cont;
void Init()
{
cnt=;cont=;
memset(fir,,sizeof(fir));
} int main()
{
int Q,n,k;
scanf("%d",&Q);
while(Q--)
{
Init();
scanf("%d%d",&n,&k);
for(int i=;i<=n;i++)
scanf("%d%d%d",&ar[i].l,&ar[i].r,&ar[i].v);
sort(ar+,ar+n+);
for(int i=;i<=n;i++){
br[i*-].a=ar[i].l;br[i*].a=ar[i].r;
br[i*-].pos=br[i*].pos=i;
}
sort(br+,br+*n+);
for(int i=;i<=n;i++)
ar[i].l=ar[i].r=;
for(int i=;i<=*n;i++)
{
int p=br[i].pos;
if(!ar[p].l){
ar[p].l=++cont;
}
else{
ar[p].r=++cont;
}
} S=;T=cont+;
for(int i=;i<cont+;i++)
addedge(i,i+,k,),addedge(i+,i,,);
for(int i=;i<=n;i++)
addedge(ar[i].l,ar[i].r,,ar[i].v),addedge(ar[i].r,ar[i].l,,-ar[i].v);
printf("%d\n",MCMF());
} return ;
}
网络流(最大费用最大流) :POJ 3680 Intervals的更多相关文章
- poj 3680 Intervals(费用流)
http://poj.org/problem?id=3680 巧妙的构图. 题目:给定N个区间(ai,bi)权值wi,求最大权和且每个点最多覆盖K次. 构图:将区间端点离散化,将第i个点连第i+1个点 ...
- POJ 3680: Intervals【最小费用最大流】
题目大意:你有N个开区间,每个区间有个重量wi,你要选择一些区间,使得满足:每个点被不超过K个区间覆盖的前提下,重量最大 思路:感觉是很好想的费用流,把每个区间首尾相连,费用为该区间的重量的相反数(由 ...
- POJ 3680 Intervals 最小费用最大流(MCMF算法)
题意:给出 n ,k 表示接下来给你 n 段开区间,每段区间都有它的权值,问选出一些区间,使它的权值最大,并且在实轴上的每个点,不得超过 k次被覆盖. 思路:首先要理解建图思路,首先有一个基图,相邻点 ...
- 网络流(最小费用最大流):POJ 2135 Farm Tour
Farm Tour Time Limit: 1000ms Memory Limit: 65536KB This problem will be judged on PKU. Original ID: ...
- poj 3680 Intervals
给定N个带权的开区间,第i个区间覆盖区间(ai,bi),权值为wi.现在要求挑出一些区间使得总权值最大,并且满足实轴上任意一个点被覆盖不超过K次. 1<=K<=N<=200.1< ...
- HDU 6118 度度熊的交易计划(网络流-最小费用最大流)
度度熊参与了喵哈哈村的商业大会,但是这次商业大会遇到了一个难题: 喵哈哈村以及周围的村庄可以看做是一共由n个片区,m条公路组成的地区. 由于生产能力的区别,第i个片区能够花费a[i]元生产1个商品,但 ...
- POJ 3680 Intervals(费用流)
Intervals Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 5762 Accepted: 2288 Descrip ...
- POJ 3680 Intervals(费用流+负权优化)
[题目链接] http://poj.org/problem?id=3680 [题目大意] 有N个带权重的区间,现在要从中选取一些区间, 要求任意点都不被超过K个区间所覆盖,请最大化总的区间权重. [题 ...
- Minimum Cost 【POJ - 2516】【网络流最小费用最大流】
题目链接 题意: 有N个商家它们需要货物源,还有M个货物供应商,N个商家需要K种物品,每种物品都有对应的需求量,M个商家每种物品都是对应的存货,然后再是K个N*M的矩阵表示了K个物品从供货商运送到商家 ...
随机推荐
- 使用java注解的例子有没有
使用java注解的例子 参考文档:http://www.cnblogs.com/pepcod/archive/2013/02/20/2918719.html http://www.shaoqun.co ...
- CSS控制鼠标滑过时的效果
用css控制鼠标样式的语法如下:<span style="cursor:*">文本或其它页面元素</span>把 * 换成如下15个效果的一种: 下面是对这 ...
- EntityFramework 中生成的类加注释
EF5在生成实体类时获取不到数据库中表的说明字段,需要使用单独的t4模板来获取 下载文件 将文件与edmx 放同一文件夹 1.在生成类的t4模板中加入 <#@ include file=&quo ...
- JAVA package与import机制
JAVA package与import机制 http://files.cnblogs.com/files/misybing/JAVA-package-and-import.pdf import org ...
- Length 和 Width在矩形中的定义.
Length is the longer or longest dimension of a rectangle (or even an object). Ref:http://mathforum.o ...
- c# 访问修饰符的访问权限
1. 访问修饰符. 指定声明的类型和类型成员的可访问性. (1) public:是类型和类型成员的访问修饰符.公共访问是允许的最高访问级别.对访问公共成员没有限制. (2) private:是一个成员 ...
- 在使用Kettle的集群排序中 Carte的设定——(基于Windows)
本片文章主要是关于使用Kettle的UI界面: Spoon来实现基于集群的对数据库中的数据表数据进行排序的试验. 以及在实验过程中所要开启的Carte服务的一些配置文件的设置, 还有基于Windows ...
- 【转】iOS使用NSMutableAttributedString实现富文本
iOS使用NSMutableAttributedString实现富文本 在iOS开发中,常常会有一段文字显示不同的颜色和字体,或者给某几个文字加删除线或下划线的需求.之前在网上找了一些资料,有的是重绘 ...
- 输出内容(document.write)
document.write() 直接在页面中输出内容 第一种 直接输出 document.write("I Love Javascript !") //输出内容为:I Love ...
- POJ 2942.Knights of the Round Table (双连通)
简要题解: 意在判断哪些点在一个图的 奇环的双连通分量内. tarjan求出所有的点双连通分量,再用二分图染色判断每个双连通分量是否形成了奇环,记录哪些点出现在内奇环内 输出没有在奇环内的点的数目 ...