Intervals

Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 9320   Accepted: 4014

题目链接:http://poj.org/problem?id=3680

Description:

You are given N weighted open intervals. The ith interval covers (aibi) and weighs wi. Your task is to pick some of the intervals to maximize the total weights under the limit that no point in the real axis is covered more than k times.

Input:

The first line of input is the number of test case.
The first line of each test case contains two integers, N and K (1 ≤ K ≤ N ≤ 200).
The next N line each contain three integers aibiwi(1 ≤ ai < bi ≤ 100,000, 1 ≤ wi ≤ 100,000) describing the intervals. 
There is a blank line before each test case.

Output:

For each test case output the maximum total weights in a separate line.

Sample Input:

4

3 1
1 2 2
2 3 4
3 4 8 3 1
1 3 2
2 3 4
3 4 8 3 1
1 100000 100000
1 2 3
100 200 300 3 2
1 100000 100000
1 150 301
100 200 300

Sample Output:

14
12
100000
100301

题意:

给出n个开区间,每个区间都有其权值,现在要求选择权值和最大的几个区间,并且每个点被区间覆盖的次数不超过k。注意这里的点不是整数点,是数轴上面所有的点。

题解:

区间覆盖的权值问题也可以用网络流...orz

我们并不关系区间边界的具体大小,只需要知道其相对大小就行了,所以我们可以考虑进行离散化,以免空间太大数组不能存储。

这里我们首先将点排序、去重、离散化后,0->1,1->2....p->p+1连一条容量为k的边,表示经过这些边的流量不能超过k。

然后根据我们输入的区间,比如离散化后的点为p,q,那么连一条p->q容量为1费用为相应负权值的边。

最后跑个最大流量最小费用就行啦~

这样建图为什么是正确的呢?我想的是总流量不超过k则限制了每个点的覆盖次数,对于两个不相交的区间,那么一个流则可以跑完。对于相交的区间,则需要多的流才能够跑完。

这种建图方式虽然不是很直观,但是yy一下就好了。

代码如下:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <vector>
#include <queue>
#define mp make_pair
#define fir first
#define sec second
#define INF 1e9
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int N = ;
int n,k,T;
int head[N],vis[N],d[N],a[N],pa[N],pre[N];
struct Edge{
int v,next,c,w;
}e[N*N<<];
int tot ;
void adde(int u,int v,int c,int w){
e[tot].v=v;e[tot].next=head[u];e[tot].w=w;e[tot].c=c;head[u]=tot++;
e[tot].v=u;e[tot].next=head[v];e[tot].w=-w;e[tot].c=;head[v]=tot++;
}
int spfa(int s,int t,int &flow,int &cost){
for(int i=;i<=t;i++) d[i]=a[i]=INF;d[s]=;
memset(vis,,sizeof(vis));vis[s]=;
memset(pre,-,sizeof(pre));memset(pa,-,sizeof(pa));
queue <int> q;q.push(s);
while(!q.empty()){
int u=q.front();q.pop();vis[u]=;
for(int i=head[u];i!=-;i=e[i].next){
int v=e[i].v;
if(e[i].c> && d[v]>d[u]+e[i].w){
d[v]=d[u]+e[i].w;
pa[v]=u;pre[v]=i;
a[v]=min(a[u],e[i].c);
if(!vis[v]){
vis[v]=;
q.push(v);
}
}
}
}
if(d[t]==INF) return ;
flow+=a[t];
cost+=a[t]*d[t];
for(int i=t;i!=-;i=pa[i]){
int edge = pre[i];
e[edge].c-=a[t];
e[edge^].c+=a[t];
}
return ;
}
int Min_cost(int s,int t){
int flow=,cost=;
while(spfa(s,t,flow,cost));
return cost;
}
int main(){
cin>>T;
while(T--){
tot=;memset(head,-,sizeof(head));
scanf("%d%d",&n,&k);
vector <int> x;
vector <pair<pii,int> > g;
for(int i=;i<=n;i++){
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
x.push_back(u);x.push_back(v);
g.push_back(mp(mp(u,v),w));
}
sort(x.begin(),x.end());
x.erase(unique(x.begin(),x.end()),x.end());
int len = x.size();
for(int i=;i<=len;i++) adde(i,i+,k,);
for(int i=;i<n;i++){
int u=g[i].fir.fir,v=g[i].fir.sec,w=g[i].sec;
int p1 = lower_bound(x.begin(),x.end(),u)-x.begin()+;
int p2 = lower_bound(x.begin(),x.end(),v)-x.begin()+;
adde(p1,p2,,-w);
}
printf("%d\n",-Min_cost(,len+));
}
return ;
}
/*
2
4 2
6 10 4
1 8 10
10 19 3
8 14 1 4 2
2 4 4
1 3 3
4 6 2
3 5 1
*/

POJ3680:Intervals(离散化+最大流最小费用)的更多相关文章

  1. POJ3680 Intervals(最小费用最大流)

    选择若干条线段使权值最大,并且点覆盖次数不超过k. 建图如下:vs到0建立容量为k费用为0的边:坐标终点到vt连接一条容量为k费用为0的边:对于每两个相邻坐标连接一条容量为INF费用为0的边:对于线段 ...

  2. poj3680 Intervals 区间k覆盖问题 最小费用最大流 建图巧妙

    /** 题目:poj3680 Intervals 区间k覆盖问题 最小费用最大流 建图巧妙 链接:http://poj.org/problem?id=3680 题意:给定n个区间,每个区间(ai,bi ...

  3. POJ3680 Intervals —— 区间k覆盖问题(最小费用流)

    题目链接:https://vjudge.net/problem/POJ-3680 Intervals Time Limit: 5000MS   Memory Limit: 65536K Total S ...

  4. poj3680 Intervals (费用流)

    建图((x,y,c,l)表示x到y,费用c,流量l) (S,1,0,K) (i,i+1,0,K) 这个边上的流量,表示i还可以被覆盖的次数 (N,T,0,K) (i,j,w,1)对于权值为w的区间[i ...

  5. Poj3680 Intervals

    这题比较经典,题意大致上就是给你n个点和m个区间,每个区间有一个正权值,让你选出一些区间,使得每个点都不会被覆盖超过k次,且选出的区间权值和最大. -------------------------- ...

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

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

  7. POJ 3680:Intervals(最小费用最大流)***

    http://poj.org/problem?id=3680 题意:给出n个区间[Li,Ri],每个区间有一个权值wi,要使得每个点都不被超过k个区间覆盖(最多能被k个区间覆盖),如果选取了第i个区间 ...

  8. POJ-3680:Intervals (费用流)

    You are given N weighted open intervals. The ith interval covers (ai, bi) and weighs wi. Your task i ...

  9. BZOJ2673 [Wf2011]Chips Challenge 费用流 zkw费用流 网络流

    https://darkbzoj.cf/problem/2673 有一个芯片,芯片上有N*N(1≤N≤40)个插槽,可以在里面装零件. 有些插槽不能装零件,有些插槽必须装零件,剩下的插槽随意. 要求装 ...

随机推荐

  1. 用ajax获取淘宝关键字接口

    可定需要查看淘宝界面的结构,按F12查看网页,此时先清除一下网页中的数据,让Network制空,随后在输入框中输入新的内容,比如钱包,数据中会出现新的数据.点击及查看蓝色方框中的内容 点击之后,你可以 ...

  2. git push 时 fatal: Unable to create 'D:/phpStudy/WWW/green_tree/.git/index.lock': File exists.解决办法

    找到自己的项目,找到.git文件夹,进去把目标文件删除即可 或者使用rm -rf 命令(如果没有那个文件件或者文件,将隐藏文件打开就可以看到了)

  3. Linux系统完整安装在虚拟机Mini

    打开VMware Workstation虚拟机,然后如下图一步到位: 此处只是简单的安装Linux系统,要想查看安装后的IP等配置看: https://www.cnblogs.com/gentle-a ...

  4. POJ:2139-Six Degrees of Cowvin Bacon

    传送门:http://poj.org/problem?id=2139 Six Degrees of Cowvin Bacon Time Limit: 1000MS Memory Limit: 6553 ...

  5. 17-比赛2 F - Fox And Two Dots (dfs)

    Fox And Two Dots CodeForces - 510B ================================================================= ...

  6. 笔记-mysql-管理及基础操作

    笔记-mysql使用-管理及基础操作 1.      简介 mysql是一个免费的关系型数据库,不过好像被oracle收购了.... 希望它继续免费. 1.1.    相关术语 数据库,表,列,行,冗 ...

  7. oracle11g导出表时空表导不出解决方案

    oracle11g用exp命令导出数据库表时,有时会发现只导出了一部分表时而且不会报错,原因是有空表没有进行导出,之前一直没有找到方法于是用最笨的方法重新建这些空表,当然在我们实际当中表的数量大时我们 ...

  8. scala初体验-02

    上一节,我们讲了scala的安装的即一些初步方法,今天,我们来介绍一下scala里面的一些基本操作 1.对于map的的编写,这个是广泛用于Array里面的 val arr = Array(1,2,3, ...

  9. DHCP服务(dhcpd)

    DHCP动态分配主机地址(Dynamic Host Configuration Protocol) 动态主机配置协议(DHCP)是一种基于UDP协议且仅限于在局域网内部使用的网络协议,主要用于大型的局 ...

  10. 关于update 表名 set 字段1 = 值1 and 字段2 = 值2的执行结果说明

    技术交流群: 233513714 如果执行了以下的语句,则brand等于‘OPPO’条件所对应的数据不会做改变,但是sequence_brand列除brand = 'OPPO'之外的所有数据都会变为0 ...