POJ3680 Intervals —— 区间k覆盖问题(最小费用流)
题目链接:https://vjudge.net/problem/POJ-3680
| Time Limit: 5000MS | Memory Limit: 65536K | |
| Total Submissions: 8711 | Accepted: 3726 |
Description
You are given N weighted open intervals. The ith interval covers (ai, bi) 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 ai, bi, wi(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
Source
题意:
数轴上有一些带权的区间, 选出权值和尽量大的一些区间, 使得任意一个数最多被k个区间覆盖。
题解:
可用最小费用流解决,构图方法如下:
1.把数轴上每个数作为一个点。
2.对于相邻的点,连一条边:i-->i+1, 容量为k, 费用为0。i-->i+1设为k,保证了x-->i(0<=x<i)的流量不高于k。因此,还需在数轴的最右边增加一个汇点, 数轴的最后一个点连向此汇点,容量为k, 费用为0。
3.对于区间[u, v], 连一条边:u-->v,容量为1, 费用为-w。
4.以数轴最左端的点作为源点,跑最小费用流, 把得到的最小花费取反,即为答案。
5.由于此题数轴的范围比较大,而实际用到的点却很少,所以可以先对数轴进行离散化。
代码如下:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
using namespace std;
typedef long long LL;
const int INF = 2e9;
const LL LNF = 9e18;
const int mod = 1e9+;
const int MAXM = 1e5+;
const int MAXN = +; struct Edge
{
int to, next, cap, flow, cost;
}edge[MAXM];
int tot, head[MAXN];
int pre[MAXN], dis[MAXN];
bool vis[MAXN];
int N; void init(int n)
{
N = n;
tot = ;
memset(head, -, sizeof(head));
} void add(int u, int v, int cap, int cost)
{
edge[tot].to = v; edge[tot].cap = cap; edge[tot].cost = cost;
edge[tot].flow = ; edge[tot].next = head[u]; head[u] = tot++;
edge[tot].to = u; edge[tot].cap = ; edge[tot].cost = -cost;
edge[tot].flow = ; edge[tot].next = head[v]; head[v] = tot++;
} bool spfa(int s, int t)
{
queue<int>q;
for(int i = ; i<N; i++)
{
dis[i] = INF;
vis[i] = false;
pre[i] = -;
} dis[s] = ;
vis[s] = true;
q.push(s);
while(!q.empty())
{
int u = q.front();
q.pop();
vis[u] = false;
for(int i = head[u]; i!=-; i = edge[i].next)
{
int v = edge[i].to;
if(edge[i].cap>edge[i].flow && dis[v]>dis[u]+edge[i].cost)
{
dis[v] = dis[u]+edge[i].cost;
pre[v] = i;
if(!vis[v])
{
vis[v] = true;
q.push(v);
}
}
}
}
if(pre[t]==-) return false;
return true;
} int minCostMaxFlow(int s, int t, int &cost)
{
int flow = ;
cost = ;
while(spfa(s,t))
{
int Min = INF;
for(int i = pre[t]; i!=-; i = pre[edge[i^].to])
{
if(Min>edge[i].cap-edge[i].flow)
Min = edge[i].cap-edge[i].flow;
}
for(int i = pre[t]; i!=-; i = pre[edge[i^].to])
{
edge[i].flow += Min;
edge[i^].flow -= Min;
cost += edge[i].cost*Min;
}
flow += Min;
}
return flow;
} int interval[][];
int M[MAXN];
int main()
{
int T, n, k;
scanf("%d", &T);
while(T--)
{
scanf("%d%d", &n, &k);
int cnt = ;
for(int i = ; i<=n; i++)
{
scanf("%d%d%d", &interval[i][],&interval[i][],&interval[i][]);
M[cnt++] = interval[i][];
M[cnt++] = interval[i][];
}
M[cnt++] = INF;
sort(M, M+cnt);
cnt = unique(M, M+cnt)-M; init(cnt);
for(int i = ; i<cnt-; i++)
{
add(i, i+, k, );
}
for(int i = ; i<=n; i++)
{
int left = lower_bound(M, M+cnt, interval[i][])-M;
int right = lower_bound(M, M+cnt, interval[i][])-M;
add(left, right, , -interval[i][]);
} int min_cost;
int start = , end = cnt-;
minCostMaxFlow(start, end, min_cost);
printf("%d\n", -min_cost);
}
}
POJ3680 Intervals —— 区间k覆盖问题(最小费用流)的更多相关文章
- poj3680 Intervals 区间k覆盖问题 最小费用最大流 建图巧妙
/** 题目:poj3680 Intervals 区间k覆盖问题 最小费用最大流 建图巧妙 链接:http://poj.org/problem?id=3680 题意:给定n个区间,每个区间(ai,bi ...
- ACM-ICPC 2018 焦作赛区网络预赛 F. Modular Production Line (区间K覆盖-最小费用流)
很明显的区间K覆盖模型,用费用流求解.只是这题N可达1e5,需要将点离散化. 建模方式步骤: 1.对权值为w的区间[u,v],加边id(u)->id(v+1),容量为1,费用为-w; 2.对所有 ...
- hdu4106 区间k覆盖问题(连续m个数,最多选k个数) 最小费用最大流 建图巧妙
/** 题目:hdu4106 区间k覆盖问题(连续m个数,最多选k个数) 最小费用最大流 建图巧妙 链接:http://acm.hdu.edu.cn/showproblem.php?pid=4106 ...
- CF802C Heidi and Library hard 费用流 区间k覆盖问题
LINK:Heidi and Library 先说一下简单版本的 就是权值都为1. 一直无脑加书 然后发现会引起冲突,可以发现此时需要扔掉一本书. 扔掉的话 可以考虑扔掉哪一本是最优的 可以发现扔掉n ...
- POJ 3762 The Bonus Salary!(最小K覆盖)
POJ 3762 The Bonus Salary! 题目链接 题意:给定一些任务.每一个任务有一个时间,有k天.一个时间仅仅能运行一个任务,每一个任务有一个价值.问怎么安排能得到最多价值 思路:典型 ...
- 【树状数组套主席树】带修改区间K大数
P2617 Dynamic Rankings 题目描述给定一个含有n个数的序列a[1],a[2],a[3]……a[n],程序必须回答这样的询问:对于给定的i,j,k,在a[i],a[i+1],a[i+ ...
- 区间K大数
区间K大数 问题描述 给定一个序列,每次询问序列中第l个数到第r个数中第K大的数是哪个. 输入格式 第一行包含一个数n,表示序列长度. 第二行包含n个正整数,表示给定的序列. 第三个包含一个正整数m, ...
- 区间K 大数查询
算法训练 区间k大数查询 时间限制:1.0s 内存限制:256.0MB 问题描述 给定一个序列,每次询问序列中第l个数到第r个数中第K大的数是哪个. 输入格式 第一行包含一个数n,表示序列 ...
- 【BZOJ】3065: 带插入区间K小值
http://www.lydsy.com/JudgeOnline/problem.php?id=3065 题意:带插入.修改的区间k小值在线查询.(原序列n<=35000, 询问<=175 ...
随机推荐
- Spoj-NPC2015A Eefun Guessing Words
Eefun Guessing Words Eefun is currently learning to read. His way of learning is unique, by trying ...
- sqlserver通过设计器修改表结构保存时提示:保存到文本问题
在sqlserver通过设计器修改表结构后保存时提示:保存到文本问题,这个问题可能通过修改设置项解决 工具>选项>设计器> 在弹出的窗口是把“阻止保存要求重新创建表的更改”选项的 ...
- 【Codeforces Round #505 (Div. 1 + Div. 2) 】
A:https://www.cnblogs.com/myx12345/p/9843966.html B: C:https://www.cnblogs.com/myx12345/p/9844084.ht ...
- 更改UISwitch大小
UISwitch大小总是改不了...找了一个简便方法. mySwitch = [[UISwitch alloc] initWithFrame: CGRectMake( width - 55, hei ...
- elasticsearch入门使用(五) kibana&x-pack安装使用
Kibana User Guide 一.UI安装 https://www.elastic.co/downloads/kibana 下载rpm直接运行即可 二.参数配置 find / -name kib ...
- Fast I/O 模板
[来源:2017 Multi-University Training Contest - Team 1] //面包有毒:P #define BUF_SIZE 100000 //fread -> ...
- Java 新手的通病
一:对算法和数据结构不熟悉 为什么我先拿“数据结构和算法”说事捏?这玩意是写程序最最基本的东东.不管你使用 Java 还是其它的什么语言,都离不开它.而且这玩意是跨语言的,学好之后不管在哪门语言中都能 ...
- CodeForces - 813C The Tag Game(拉格朗日乘数法,限制条件求最值)
[传送门]http://codeforces.com/problemset/problem/813/C [题意]给定整数a,b,c,s,求使得 xa yb zc值最大的实数 x,y,z , 其中x ...
- nginx原配置
#原配置 server { listen ; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main ...
- VC++下编译Libgeotiff(含Libtiff)
转自原文Win10+VC++下编译Libgeotiff(含Libtiff)详细图文教程 GeoTiff是包含地理信息的一种Tiff格式的文件.Libgeotiff就是一个操作GeoTiff文件的库.同 ...