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 ...
随机推荐
- windows系统下Python环境的搭建
1.下载最新的Python版本3.5.0.
- 基于SimHash的微博去重
一.需求:对微博数据进行去重,数据量比较小,几十万条左右. 二.解决方案 1.采用SimHash的指纹信息去重方法. 三.实现方案 1.对每一条微博使用tf-idf与特征词 2.使用每条微博的特征词, ...
- navigationbar的一些设置记录
1.设置navigationbar背景颜色 [[UINavigationBar appearance] setBarTintColor:[UIColor blackColor]]; PS:如果颜色不对 ...
- Android移动应用开发中常见的经验技巧总结
转:http://wwwdevstorecn/essay/essayInfo/6128.html 1. 对话保持的解决方案. 要求: 1.app中使用webview访问具体网站的内容,但是app与服务 ...
- 教你如何将 Sublime 3 打造成 Python/Django IDE开发利器
Sublime Text 是一款非常强大的文本编辑器, 下面我们介绍如何将 Sublime Text 3 打造成一款 Python/Django 开发利器: 1. 安装 Sublime Text 3 ...
- ASIFormDataRequest实现post的代码示例
用jquery实现的Post方法可能如下 var param = $.param({ data: JSON.stringify({"from":"234",&q ...
- H264 帧结构分析、帧判断
http://blog.csdn.net/dxpqxb/article/details/7631304 H264以NALU(NAL unit)为单位来支持编码数据在基于分组交换技术网络中传输. NAL ...
- DirectDraw 直接显示RGB图象的最简单实现
来自: #include "DDraw.h" class CDDraw { public: void CleanUp(); void DrawDIB(BITMAPINFOH ...
- Android-AnimationDrawable(三)运行的几种方式
项目开发用到了AnimationDrawable,调用start后没有运行,很纳闷.google搜了下.记录一下. 这个AnimationDrawable.start不能直接写在onClick,onS ...
- HDU 1671 Phone List(POJ 3630)
Phone List Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...