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 ...
随机推荐
- 区分int a() 和 int a
事因 #include <iostream> using namespace std; struct A { A(int) {} A() {} void fun() {}; }; int ...
- asp.net开源CMS推荐
随着网络技术的发展,目前国内CMS的开发商越来越多,各自都有其独特的优势,大家在选择的时候觉得眼花缭乱,不知道选择哪个比较好,我个人认为开源的CMS还是适合我们学习及研究使用,下边就几个国内的asp. ...
- PHP大神的十大优良习惯
概述:通往PHP大神的道路上,应该保持优良的传统和习惯. 1.多阅读手册和源代码 没什么比阅读手册更值得强调的事了–仅仅通过阅读手册你就可以学习到很多东西,特别是很多有关于字符串和数组的函数.就在这些 ...
- 【Linux常用工具】03. Linux性能测试工具ab
在Apache服务器的套件中,有一个叫做 ab (ApacheBench) 的工具. ApacheBench 主要是用来测试Apache服务器执行效率用的 ApacheBench 可以针对某个特定的 ...
- Zookeeper、HBase的伪分布
1.Zookeeper伪分布的部署(3个节点) 所谓的“伪分布式集群”就是在一台服务器中,启动多个Zookeeper实例.“完全分布式集群”是每台服务器,启动一个Zookeeper实例. 1.1.解压 ...
- Android开发之获取时间SystemClock
转载:http://blog.csdn.net/tianfeng701/article/details/7562359 在Andriod中关于线程一部分中经常会遇到计算时间的操作,这里面应用较多的是S ...
- Spring 事务中 readOnly 的解释
spring 中事务的PROPAGATION_REQUIRED,Readonly的解释 (2012-11-21 16:29:38) 转载▼ 标签: 杂谈 一. ...
- Struts2系列——struts2的result
在action的指定方法执行完毕后总会返回一个字符串,struts2根据返回的字符串去action的配置中的result去找匹配的名字,根据配置执行下一步的操作. 在ActionSupport基类中定 ...
- web.xml元素介绍
每一个站的WEB-INF下都有一个web.xml的设定文件,它提供了对我们站台的配置设定.web.xml中定义元素有:◆站台的名称和说明◆针对环境参数(Context)做初始化工作◆Servlet的名 ...
- UVa 12186 Another Crisis
题意: 给出一个树状关系图,公司里只有一个老板编号为0,其他人员从1开始编号.除了老板,每个人都有一个直接上司,没有下属的员工成为工人. 工人们想写一份加工资的请愿书,只有当不少于员工的所有下属的T% ...