Intervals POJ - 3680(最小费用流)
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
题意:
您将获得N个加权的开放时间间隔。 第i个间隔覆盖(ai,bi),权重wi。 您的任务是选择一些间隔,以使实轴上的任何点均不覆盖k次以上的限制达到最大。
题解:
1.把数轴上每个数作为一个点。(数不重复)
2.对于相邻的点,连一条边:i–>i+1, 容量为INF, 费用为0。
汇点s到0点,t到最后一点容量为k, 费用为0。
3.对于区间[u, v], 连一条边:u–>v,容量为1, 费用为-w。
4.最小费用流, 把得到的最小花费取反,即为答案。
#include <cstdio>
#include <queue>
#include <vector>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <iostream>
#include <iomanip>
using namespace std;
#define SZ(v) (int)v.size()
#define pii pair<int,int>const int INF = 1e9;
const int MAXN = 2e4 + 10;struct MCMF {
struct Edge {
int v, cap, cost, rev;Edge(int v, int cap, int cost, int rev):v(v),cap(cap),cost(cost),rev(rev){
}};int flow, cost, s, t, n;int dist[MAXN], H[MAXN], pv[MAXN], pe[MAXN];std::vector<Edge> G[MAXN];bool dijkstra() {
std::priority_queue<pii, std::vector<pii>, std::greater<pii> > q;std::fill(dist, dist + n + 1, INF);dist[s] = 0; q.push({
0, s});while (!q.empty()) {
pii x = q.top(); q.pop();int &u = x.second;if (dist[u] < x.first) continue;for (int i = 0; i < SZ(G[u]); ++i) {
Edge &e = G[u][i];int &v = e.v;pii y(dist[u] + e.cost + H[u] - H[v], v);if (e.cap > 0 && dist[v] > y.first) {
dist[v] = y.first;pe[v] = i, pv[v] = u;q.push(y);}}}if (dist[t] == INF) return false;for (int i = 0; i <= n; ++i) H[i] += dist[i];int f = INF;for (int v = t; v != s; v = pv[v]) f = std::min(f, G[pv[v]][pe[v]].cap);flow += f;cost += f * H[t];for (int v = t; v != s; v = pv[v]) {
Edge &e = G[pv[v]][pe[v]];e.cap -= f;G[v][e.rev].cap += f;}return true;}void solve(int s, int t) {
this->s = s, this->t = t;flow = cost = 0;std::fill(H, H + n + 1, 0);while (dijkstra());}void init(int n) {
this->n = n;for (int i = 0; i <= n; ++i) G[i].clear();}void add_edge(int u, int v, int cap, int cost) {
G[u].push_back(Edge(v, cap, cost, SZ(G[v])));G[v].push_back(Edge(u, 0, -cost, SZ(G[u]) - 1));}} mcmf;int main() {
int T;scanf("%d" ,&T);while(T--){
int N, K;scanf("%d%d", &N, &K);int a[MAXN], b[MAXN], w[MAXN];vector<int> x;for(int i = 0; i < N; i++) {
scanf("%d%d%d", &a[i], &b[i], &w[i]);x.push_back(a[i]);x.push_back(b[i]);}sort(x.begin(), x.end());//去除重复的数x.erase(unique(x.begin(), x.end()), x.end());int len = x.size();int s = len + 1, t = s + 1;mcmf.init(t);for(int i = 0; i < len - 1; i++) {
mcmf.add_edge(i, i + 1, INF, 0);}mcmf.add_edge(s, 0, K, 0);mcmf.add_edge(len - 1, t, K, 0);for(int i = 0; i < N; i++) {
int u = find(x.begin(), x.end(), a[i]) - x.begin();int v = find(x.begin(), x.end(), b[i]) - x.begin();mcmf.add_edge(u, v, 1, -w[i]);}mcmf.solve(s, t);mcmf.flow = K;printf("%d\n", -mcmf.cost);}
}