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的更多相关文章

  1. 【POJ 1201】 Intervals(差分约束系统)

    [POJ 1201] Intervals(差分约束系统) 11 1716的升级版 把原本固定的边权改为不固定. Intervals Time Limit: 2000MS   Memory Limit: ...

  2. poj 3680 Intervals(费用流)

    http://poj.org/problem?id=3680 巧妙的构图. 题目:给定N个区间(ai,bi)权值wi,求最大权和且每个点最多覆盖K次. 构图:将区间端点离散化,将第i个点连第i+1个点 ...

  3. POJ 3680 Intervals(费用流)

    Intervals Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 5762   Accepted: 2288 Descrip ...

  4. POJ 3680 Intervals(费用流+负权优化)

    [题目链接] http://poj.org/problem?id=3680 [题目大意] 有N个带权重的区间,现在要从中选取一些区间, 要求任意点都不被超过K个区间所覆盖,请最大化总的区间权重. [题 ...

  5. 网络流(最大费用最大流) :POJ 3680 Intervals

    Intervals Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 7218   Accepted: 3011 Descrip ...

  6. poj 3680 Intervals

    给定N个带权的开区间,第i个区间覆盖区间(ai,bi),权值为wi.现在要求挑出一些区间使得总权值最大,并且满足实轴上任意一个点被覆盖不超过K次. 1<=K<=N<=200.1< ...

  7. POJ 3680: Intervals【最小费用最大流】

    题目大意:你有N个开区间,每个区间有个重量wi,你要选择一些区间,使得满足:每个点被不超过K个区间覆盖的前提下,重量最大 思路:感觉是很好想的费用流,把每个区间首尾相连,费用为该区间的重量的相反数(由 ...

  8. POJ 3680 Intervals 最小费用最大流(MCMF算法)

    题意:给出 n ,k 表示接下来给你 n 段开区间,每段区间都有它的权值,问选出一些区间,使它的权值最大,并且在实轴上的每个点,不得超过 k次被覆盖. 思路:首先要理解建图思路,首先有一个基图,相邻点 ...

  9. poj 1716 Integer Intervals (差分约束 或 贪心)

    Integer Intervals Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 12192   Accepted: 514 ...

随机推荐

  1. 41. First Missing Positive

    题目: Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2 ...

  2. 如何用Maven创建一个普通Java项目

    一下内容包括:用Maven创建一个普通Java项目,并把该项目转成IDEA项目,导入到IDEA,最后把这个项目打包成一个jar文件. 有时候运行mvn命令失败,重复运行几次就OK了,无解(可能因为网络 ...

  3. 尝鲜delphi开发android/ios_环境搭建

    Delphi这又老树发新枝了,开始做终端程序开发了,这个东西的准确名字是:RAD Studio XE5,可以使用delphi和c++ builder进行终端开发. 我尽可能讲啰嗦一些,免得回头被人问. ...

  4. Eclipse常见设置及快捷键使用总结(更新中)

    Eclipse中常见设置: 1.Eclipse在保存时设置自动去掉多余的import和格式化代码 路径: window --> preferences --> java --> Ed ...

  5. [UESTC1059]秋实大哥与小朋友(线段树, 离散化)

    题目链接:http://acm.uestc.edu.cn/#/problem/show/1059 普通线段树+离散化,关键是……离散化后建树和查询都要按照基本法!!!RE了不知道多少次………………我真 ...

  6. Mac下安装HBase及详解

    Mac下安装HBase及详解 1. 千篇一律的HBase简介 HBase是Hadoop的数据库, 而Hive数据库的管理工具, HBase具有分布式, 可扩展及面向列存储的特点(基于谷歌BigTabl ...

  7. 1160. Network(最小生成树)

    1160 算是模版了 没什么限制 结束输出就行了 #include <iostream> #include<cstdio> #include<cstring> #i ...

  8. poj 1054 The Troublesome Frog (暴力搜索 + 剪枝优化)

    题目链接 看到分类里是dp,结果想了半天,也没想出来,搜了一下题解,全是暴力! 不过剪枝很重要,下面我的代码 266ms. 题意: 在一个矩阵方格里面,青蛙在里面跳,但是青蛙每一步都是等长的跳, 从一 ...

  9. bzoj2436

    不难发现两边的活动是交替进行的,我们可以dp 先对时间离散化,设f[i,j]到时间i一个会场选j个活动,另一个会场最多有多少活动,那么f[i,j]=max(f[k,j]+s[k,i],f[k,j-s[ ...

  10. 解决IE6下浮动层固定定位的经典方法

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...