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 ...
随机推荐
- Sqlmap基础(一)
(1)选项:-r REQUESTFILE Load HTTP request from a file (2)选项:--current-db Retrieve DBMS curr ...
- 爬虫Larbin解析(一)——Larbin配置与使用
介绍 功能:网络爬虫 开发语言:c++ 开发者:Sébastien Ailleret(法国) 特点:只抓取网页,高效(一个简单的larbin的爬虫可以每天获取500万的网页) 安装 安装平台:Ubun ...
- gcc编译代码报错及编译方式
一.error: 'for' loop initial declarations are only allowed in C99 mode 前段时间写了一个小C程序,放在linux下用gcc编译出错, ...
- (三)C#关于txt文件的读取和写入
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Tex ...
- Oracle 数据集成的实际解决方案
就针对市场与企业的发展的需求,Oracle公司提供了一个相对统一的关于企业级的实时数据解决方案,即Oracle数据集成的解决方案.以下的文章主要是对其解决方案的具体描述,望你会有所收获. Oracle ...
- zoj 3778 Talented Chef(思维题)
题目 题意:一个人可以在一分钟同时进行m道菜的一个步骤,共有n道菜,每道菜各有xi个步骤,求做完的最短时间. 思路:一道很水的思维题, 根本不需要去 考虑模拟过程 以及先做那道菜(比赛的时候就是这么考 ...
- python模拟http请求2
发现了一个非常好用的第三方module:requests,模拟接口非常简单. 详细了解请移步:http://docs.python-requests.org/en/latest/ 非常不错 #!cod ...
- django - 好的 获取 参数值 方法
第一步: # 参数列表 parameters = ('user_id', 'day_time', 'normal_data', 'hourly_data', 'product_id') # 需要传入的 ...
- poj 3260 The Fewest Coins
// 转载自http://blog.163.com/benz_/blog/static/18684203020115721917109/算法不难看出,就是一个无限背包+多重背包.问题在于背包的范围.设 ...
- 3. 使用绘图API自定义视图 --- 旋转的方块
import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; impor ...