POJ 3680 Intervals 离散 + 费用流

Intervals











Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 6246   Accepted: 2542

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 ≤ KN ≤ 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

POJ
Founder Monthly Contest – 2008.07.27
, windy7926778

题目大意:给你 n 个开区间,区间有权值,问如何选择区间使得每个点上覆盖的区间数不超过 k
个的前提下获得的最大权值和。

题目分析:做之前正好看过大白鼠的方法,所以过的没压力。首先,先对区间的端点进行离散化,之后对每个区间的左端点u和右端点v建边(u,v,1,-w),再对每个坐标点
i 建边(i,i + 1,k,0),设离散后最大的点的坐标为x,建立源汇点s = 0, t = x +
1,建边(x,t,1,0)。跑一次最小费用最大流,结果即花费的相反数。

代码如下:

#include <stdio.h>
#include <string.h>
#include <algorithm>
#define min(a, b) ((a) < (b) ? (a) : (b))
#define REP(i, n) for(int i = 0; i < n; ++i)
#define MS0(X) memset(X, 0, sizeof X)
#define MS1(X) memset(X, -1, sizeof X)
using namespace std;
const int maxE = 3000000;
const int maxN = 500;
const int maxM = 55;
const int oo = 0x3f3f3f3f;
struct Edge{
int v, c, w, n;
};
Edge edge[maxE];
int adj[maxN], l;
int d[maxN], cur[maxN], Minflow;
int inq[maxN], Q[maxE], head, tail;
int cost, flow, s, t;
int n, m, cnt;
struct Node{
int l, r, w;
}a[maxN];
int b[maxN];
void addedge(int u, int v, int c, int w){
edge[l].v = v; edge[l].c = c; edge[l].w = w; edge[l].n = adj[u]; adj[u] = l++;
edge[l].v = u; edge[l].c = 0; edge[l].w = -w; edge[l].n = adj[v]; adj[v] = l++;
}
int SPFA(){
memset(d, oo, sizeof d);
memset(inq, 0, sizeof inq);
head = tail = 0;
d[s] = 0;
Minflow = oo;
cur[s] = -1;
Q[tail++] = s;
while(head != tail){
int u = Q[head++];
inq[u] = 0;
for(int i = adj[u]; ~i; i = edge[i].n){
int v = edge[i].v;
if(edge[i].c && d[v] > d[u] + edge[i].w){
d[v] = d[u] + edge[i].w;
cur[v] = i;
Minflow = min(edge[i].c, Minflow);
if(!inq[v]){
inq[v] = 1;
Q[tail++] = v;
}
}
}
}
if(d[t] == oo) return 0;
flow += Minflow;
cost += Minflow * d[t];
for(int i = cur[t]; ~i; i = cur[edge[i ^ 1].v]){
edge[i].c -= Minflow;
edge[i ^ 1].c += Minflow;
}
return 1;
}
int MCMF(){
flow = cost = 0;
while(SPFA());
return cost;
}
void work(){
MS1(adj);
l = 0;
scanf("%d%d", &n, &m);
REP(i, n) scanf("%d%d%d", &a[i].l, &a[i].r, &a[i].w);
cnt = 0;
REP(i, n){
b[cnt++] = a[i].l;
b[cnt++] = a[i].r;
}
sort(b, b + cnt);
int cnt1 = 0;
REP(i, cnt) if(i && b[i] != b[cnt1]) b[++cnt1] = b[i];
cnt = ++cnt1;
//不会离散化就这么蠢蠢的离散好了QvQ
REP(i, n) REP(j, cnt) if(a[i].l == b[j]){
a[i].l = j;
break;
}
REP(i, n) REP(j, cnt) if(a[i].r == b[j]){
a[i].r = j;
break;
}
s = 0; t = cnt;
REP(i, n) addedge(a[i].l, a[i].r, 1, -a[i].w);
REP(i, cnt) addedge(i, i + 1, m, 0);
printf("%d\n", -MCMF());
}
int main(){
int T, cas;
for(scanf("%d", &T), cas = 1; cas <= T; ++cas) work();
return 0;
}

POJ 3680

POJ 3680 Intervals 离散 + 费用流,布布扣,bubuko.com

时间: 2024-08-07 21:20:23

POJ 3680 Intervals 离散 + 费用流的相关文章

poj 3680 Intervals 最大费用流

题意: 给n给开区间(ai,bi)及相应权值wi,现在要选一些区间,要求任一点不能被超过k个区间覆盖,目标是最大化总的权重. 分析: 转化为求最大费用流,改改最小费用流的模板就好. 代码: //poj 3680 //sep9 #include <iostream> #include <vector> #include <queue> #include <algorithm> using namespace std; const int maxN=2048;

POJ 3680 Intervals(经典费用流)

解题思路: 区间K覆盖问题:数轴上有一些带权值的区间,选出权和尽量大的一些区间,使得任意一个点最多被K个区间覆盖. 构图方法为:把每一个数作为一个节点,然后对于权值为W的区间[ u, v ]连一条边,容量为1,费用为-w,再对所有相邻 的点连边i -> i + 1,容量为K,费用为0:最后求最左端到最右端的最小费用最大流即可.如果数值范围太大,需要先进行离散化. #include <iostream> #include <cstring> #include <cstdi

POJ 3670 Intervals(费用流)

POJ 3680 Intervals 题目链接 题意:给定一些区间,每个区间有一个权值,要求用这些区间去覆盖,每个点最多覆盖k次,问最多得到权值多少 思路:典型的区间k覆盖问题,区间连边容量1,代价-w,然后其他点相邻两两连边,容量k,代价0,跑一下费用流即可 代码: #include <cstdio> #include <cstring> #include <vector> #include <queue> #include <algorithm&g

POJ 2175 Evacuation Plan 费用流 负圈定理

题目给了一个满足最大流的残量网络,判断是否费用最小. 如果残量网络中存在费用负圈,那么不是最优,在这个圈上增广,增广1的流量就行了. 1.SPFA中某个点入队超过n次,说明存在负环,但是这个点不一定在负环上. 2.这个负环可能包括汇点t,所以构建残量网络的时候也要考虑防空洞到t上的容量. //#pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #include<cstring

POJ 3680 Intervals(费用流+离散化)

题目地址:POJ 3680 这题的建图真心想不出来.建图思维还是不够开阔,不够大胆. 这题要先对坐标进行离散化.可以用左边的点发出一条到右边的点的边,容量为1,费用为负的权值.然后从左往右将依次将相邻的两个点都连起来,权值为0,容量为k,也就是说,如果选了这个区间,就会从费用为负数的边流过去,否则,就是从这个费用为0的边流过去.然后建立一个超级源点与最左边的点相连,权值为0,容量为k,这样就保证了重叠数之多为k,因为增广路上所经过的区间必定是不重合的,而流量只有k,所以满足题意. 代码如下: #

poj 3680 Intervals 费用流

题目链接 给一些线段, 每个线段有一个值, 并且覆盖一些点, 求每个点被覆盖次数不超过k时, 可以取得的最大值. 首先将点离散化, 然后连边, i向i+1连一条容量为k, 费用为0的边. 对于每条线段, 起点向终点连一条容量为1, 费用为-val的边, 然后跑费用流就好. 1 #include <iostream> 2 #include <vector> 3 #include <cstdio> 4 #include <cstring> 5 #include

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

题目大意:你有N个开区间,每个区间有个重量wi,你要选择一些区间,使得满足:每个点被不超过K个区间覆盖的前提下,重量最大 思路:感觉是很好想的费用流,把每个区间首尾相连,费用为该区间的重量的相反数(由于要最大,所以是求最大费用最大流),容量为1,至于不超过K的限制,只要从源点到第一个点的流量为K就行,剩下每个相邻的点相连,费用为0,流量只要大于的等于K就可以(我取的正无穷) //poj3680 #include <stdio.h> #include <iostream> #incl

POJ 2135 Farm Tour(费用流)

POJ 2135 Farm Tour 题目链接 题意:给定一个无向图,边有权值,求从1到n再从n到1的最短路 思路:费用流,连边容量为1(注意是无向图),然后源点和1连容量2,n和汇点连容量是2 代码: #include <cstdio> #include <cstring> #include <vector> #include <queue> #include <algorithm> using namespace std; const int

POJ 2175 Evacuation Plan (费用流,负环,消圈法,SPFA)

http://poj.org/problem?id=2175 Evacuation Plan Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3256   Accepted: 855   Special Judge Description The City has a number of municipal buildings and a number of fallout shelters that were build