# POJ 3680 Intervals（区间覆盖建图）

Intervals
Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 8549 Accepted: 3641
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

POJ Founder Monthly Contest – 2008.07.27, windy7926778

### 题目大意：

有N$N$个带权开区间，让你从中选择一些使得权值最大，实数轴上任意一点被覆盖不超过K次，输出最大权值和。

### 解题思路：

经典的在区间上建图题目。首先把所有端点离散化编号从1$1$n$n$，建立源点0$0$，汇点n+1$n+1$。对于除汇点之外的点建立从i$i$i+1$i+1$的边，容量为K，费用为0。对于每个区间(l,r)$(l, r)$，建立从l$l'$r$r'$的边，容量为1$1$，费用为wi$-w_{i}$。跑一遍最小费用最大流，结果的相反数即为答案。

### AC代码：

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <stack>
using namespace std;
#define INF 0x3f3f3f3f
#define LL long long
#define fi first
#define se second
#define sqr(x) ((x)*(x))

const int MAXN=200+3;
const int MAXV=MAXN*2;
const int MAXE=MAXV*4;

struct Edge
{
int to,next,cap,cost;
Edge(int t=0,int n=0,int ca=0,int f=0,int co=0):to(t),next(n),cap(ca),cost(co){}
}edge[MAXE];

int N, K, l[MAXN], r[MAXN], w[MAXN];
vector<int> save;
int pre[MAXV],dis[MAXV];
bool vis[MAXV];
int V;//节点总个数，节点编号从0~V-1

void add_edge(int u,int v,int cap,int cost)
{
}

bool spfa(int s,int t)
{
queue<int> que;
for(int i=0;i<V;++i)
{
dis[i]=INF;
vis[i]=false;
pre[i]=-1;
}
dis[s]=0;
vis[s]=true;
que.push(s);
while(!que.empty())
{
int u=que.front(); que.pop();
vis[u]=false;
{
int v=edge[i].to;
if(edge[i].cap>0&&dis[v]>dis[u]+edge[i].cost)
{
dis[v]=dis[u]+edge[i].cost;
pre[v]=i;
if(!vis[v])
{
vis[v]=true;
que.push(v);
}
}
}
}
return pre[t]!=-1;
}

int min_cost_flow(int s,int t,int &cost)//返回的是最大流，cost存的是最小费用
{
int flow=0;
cost=0;
while(spfa(s,t))
{
int the_min=INF;
for(int i=pre[t];i!=-1;i=pre[edge[i^1].to])
the_min=min(the_min,edge[i].cap);
for(int i=pre[t];i!=-1;i=pre[edge[i^1].to])
{
edge[i].cap-=the_min;
edge[i^1].cap+=the_min;
cost+=edge[i].cost*the_min;
}
flow+=the_min;
}
return flow;
}

void init()
{
save.clear();
tol=0;
}

int main()
{
int T_T;
scanf("%d", &T_T);
while(T_T--)
{
scanf("%d%d", &N, &K);
init();
for(int i=0;i<N;++i)
{
scanf("%d%d%d", &l[i], &r[i], &w[i]);
save.push_back(l[i]);
save.push_back(r[i]);
}
sort(save.begin(), save.end());
save.erase(unique(save.begin(), save.end()), save.end());
for(int i=0;i<N;++i)
{
l[i]=lower_bound(save.begin(), save.end(), l[i])-save.begin()+1;
r[i]=lower_bound(save.begin(), save.end(), r[i])-save.begin()+1;
}
V=save.size()+2;
for(int i=0;i<V;++i)
int s=0, t=V-1;
for(int i=0;i<V-1;++i)
}