POJ No.3680 Intervals
2016-06-01 22:01:39
题目链接: POJ No.3680 Intervals
题目大意:
给定N个带权区间,最多可以重复选一个点M次,求出一种选法使得所得权最大
解法:
费用流
建模:
区间的端点之间按照副权流量1连接,而每个点之间需要再连0权流量无穷作为跳过用
注意的地方:
十万个点肯定是不行的,看我unique离散化大法
//Intervals (POJ No.3680)
//费用流
#include<stdio.h>
#include<algorithm>
#include<queue>
using namespace std;
const int maxn=;
int T,N,K;
int hash[maxn];
int a[maxn];
int w[maxn];
struct edge
{
int to;
int from;
int cost;
int flow;
int next;
edge(){}
edge(int from,int to,int cost,int flow,int next):from(from),to(to),cost(cost),flow(flow),next(next){}
};
edge n[maxn*];
int cnt;
int len;
int now;
int head[maxn];
bool vis[maxn];
int dist[maxn];
int pre[maxn];
queue <int> q;
void insert(int x,int y,int z,int cost)
{
n[++cnt]=edge(x,y,cost,z,head[x]);
head[x]=cnt;
n[++cnt]=edge(y,x,-cost,,head[y]);
head[y]=cnt;
return ;
}
void SPFA(int s,int t)
{
fill(dist,dist+maxn,);
q.push(s);
vis[s]=;
dist[s]=;
while(!q.empty())
{
now=q.front();
q.pop();
vis[now]=;
for(int i=head[now];i;i=n[i].next)
{
if(n[i].flow>)
{
if(dist[n[i].to]>dist[now]+n[i].cost)
{
dist[n[i].to]=dist[now]+n[i].cost;
pre[n[i].to]=i;
if(!vis[n[i].to])
{
vis[n[i].to]=;
q.push(n[i].to);
}
}
}
}
}
for(int i=pre[t];i;i=pre[n[i].from])
{
n[i].flow--;
n[i^].flow++;
}
return ;
}
int Mincost_flow(int s,int t,int num)
{
int ans=;
while(num--)
{
SPFA(s,t);
ans+=dist[t];
}
return -ans;
}
int main()
{
scanf("%d",&T);
while(T--)
{
cnt=;
fill(head,head+maxn,);
scanf("%d %d",&N,&K);
for(int i=;i<=N;i++)
{
scanf("%d %d",&a[i*-],&a[i*]);
scanf("%d",&w[i]);
hash[i*-]=a[i*-];
hash[i*]=a[i*];
}
sort(hash+,hash+*N+);
len=unique(hash+,hash+*N+)-hash-;
for(int i=;i<=N*;i++)
{
a[i]=lower_bound(hash+,hash+len+,a[i])-hash;
if(!(i&))insert(a[i-],a[i],,-w[i/]);
}
for(int i=;i<len;i++)insert(i,i+,,);
insert(len,len+,K,);
insert(,,K,);
printf("%d\n",Mincost_flow(,len+,K));
}
}
POJ No.3680 Intervals的更多相关文章
- 【POJ 1201】 Intervals(差分约束系统)
[POJ 1201] Intervals(差分约束系统) 11 1716的升级版 把原本固定的边权改为不固定. Intervals Time Limit: 2000MS Memory Limit: ...
- poj 3680 Intervals(费用流)
http://poj.org/problem?id=3680 巧妙的构图. 题目:给定N个区间(ai,bi)权值wi,求最大权和且每个点最多覆盖K次. 构图:将区间端点离散化,将第i个点连第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个区间所覆盖,请最大化总的区间权重. [题 ...
- 网络流(最大费用最大流) :POJ 3680 Intervals
Intervals Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 7218 Accepted: 3011 Descrip ...
- poj 3680 Intervals
给定N个带权的开区间,第i个区间覆盖区间(ai,bi),权值为wi.现在要求挑出一些区间使得总权值最大,并且满足实轴上任意一个点被覆盖不超过K次. 1<=K<=N<=200.1< ...
- POJ 3680: Intervals【最小费用最大流】
题目大意:你有N个开区间,每个区间有个重量wi,你要选择一些区间,使得满足:每个点被不超过K个区间覆盖的前提下,重量最大 思路:感觉是很好想的费用流,把每个区间首尾相连,费用为该区间的重量的相反数(由 ...
- POJ 3680 Intervals 最小费用最大流(MCMF算法)
题意:给出 n ,k 表示接下来给你 n 段开区间,每段区间都有它的权值,问选出一些区间,使它的权值最大,并且在实轴上的每个点,不得超过 k次被覆盖. 思路:首先要理解建图思路,首先有一个基图,相邻点 ...
- poj 1716 Integer Intervals (差分约束 或 贪心)
Integer Intervals Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 12192 Accepted: 514 ...
随机推荐
- 项目SVN的IP地址发生变化时修改SVN为新的IP地址
在eclipse或者Myeclipse自带的svn:subclipse中修改ip地址 项目开发中有可能要修改SVN的IP地址,entries文件里面包含svn服务器的地址信息.每个文件夹都会产生一个e ...
- NSMutableArray 排序
NSMutableArray *array1=[NSMutableArray arrayWithObjects:@"1",@"3",@"2" ...
- Haxe - Actuate.Tween
方法解释: Actuate.tween( target : Dynamic , duration : Float , properties : Dynamic , ?overwrite : Bool ...
- 机器学习 —— 概率图模型(Homework: Representation)
前两周的作业主要是关于Factor以及有向图的构造,但是概率图模型中还有一种更强大的武器——双向图(无向图.Markov Network).与有向图不同,双向图可以描述两个var之间相互作用以及联系. ...
- .NET 框架 (转载)
转载:http://www.tracefact.net/CLR-and-Framework/DotNet-Framework.aspx .NET框架 三年前写的<.NET之美>的第六章,现 ...
- PHP输出缓冲控制- Output Control 函数应用详解
说到输出缓冲,首先要说的是一个叫做缓冲器(buffer)的东西.举个简单的例子说明他的作用:我们在编辑一篇文档时,在我们没有保存之前,系统是不会向磁盘写入的,而是写到buffer中,当buffer写满 ...
- [Codeforces673A]Bear and Game(水题,思路)
题目链接:http://codeforces.com/contest/673/problem/A 题意:一个人看一个90分钟的节目,然后告诉你一些有趣的时刻.这个人假如在15分钟内还没有看到有趣的时刻 ...
- MySQL增加列,移动列
ALTER TABLE test ADD COLUMN id INT UNSIGNED NOT NULL auto_increment PRIMARY KEY FIRST 给表添加列是一个常用的操作, ...
- 宏os_file_read_func
# define os_file_read(file, buf, offset, offset_high, n) \ os_file_read_func(file, buf, offset, offs ...
- HTML空格符号   / &ensp / &emsp
半角的不断行的空白格(推荐使用) 半角的空格 全角的空格