Title
Portal POJ 3680
answer
Discretized coordinates, connected to the end of the interval, the capacity is , the weight is ; For all adjacent coordinate values, the capacity is INF, and the weight is ; this corresponds to a DAG. There are negative weighted edges in the graph, so that the initial full flow processing, you can directly use dijkstra to find the minimum cost flow.
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue>
#include <vector>
#define min(a,b) (((a) < (b)) ? (a) : (b))
#define max(a,b) (((a) > (b)) ? (a) : (b))
#define abs(x) ((x) < 0 ? -(x) : (x))
#define INF 0x3f3f3f3f
#define delta 0.85
#define eps 1e-5
#define PI 3.14159265358979323846
using namespace std;
#define MAX_V 405
struct edge{
int to, cap, cost, rev;
edge(int to, int cap, int cost, int rev):to(to), cap(cap), cost(cost), rev(rev){}
};
int V;
vector<edge> G[MAX_V];
int h[MAX_V], dist[MAX_V];
int prevv[MAX_V], preve[MAX_V];
void add_edge(int from, int to, int cap, int cost){
G[from].push_back(edge(to, cap, cost, G[to].size()));
G[to].push_back(edge(from, 0, -cost, G[from].size() - 1));
}
int min_cost_flow(int s, int t, int f){
int res = 0;
memset(h, 0, sizeof(h));
while(f > 0){
// Dijkstra
priority_queue<pair<int, int>, vector<pair<int, int> >, greater<pair<int, int> > > que;
memset(dist, 0x3f, sizeof(dist));
dist[s] = 0;
que.push(pair<int, int>(0, s));
while(!que.empty()){
pair<int, int> p = que.top(); que.pop();
int v = p.second;
if(dist[v] < p.first) continue;
for(int i = 0; i < G[v].size(); i++){
edge &e = G[v][i];
int d2 = dist[v] + e.cost + h[v] - h[e.to];
if(e.cap > 0 && d2 < dist[e.to]){
dist[e.to] = d2;
prevv[e.to] = v;
preve[e.to] = i;
que.push(pair<int, int>(dist[e.to], e.to));
}
}
}
if(dist[t] == INF){
return -1;
}
for(int v = 0; v < V; v++) h[v] += dist[v];
int d = f;
for(int v = t; v != s; v = prevv[v]){
d = min(d, G[prevv[v]][preve[v]].cap);
}
f -= d;
res += d * h[t];
for(int v = t; v != s; v = prevv[v]){
edge &e = G[prevv[v]][preve[v]];
e.cap -= d;
G[v][e.rev].cap += d;
}
}
return res;
}
#define MAX_N 200
#define MAX_K 200
int N, K;
int a[MAX_N], b[MAX_N], w[MAX_N];
// 坐标离散化
int compress(int *x, int *y){
vector<int> xs(N * 2);
for(int i = 0, i2 = N; i < N; i++, i2++) xs[i] = x[i], xs[i2] = y[i];
sort(xs.begin(), xs.end());
xs.erase(unique(xs.begin(), xs.end()), xs.end());
for(int i = 0; i < N; i++){
x[i] = distance(xs.begin(), lower_bound(xs.begin(), xs.end(), x[i]));
y[i] = distance(xs.begin(), lower_bound(xs.begin(), xs.end(), y[i]));
}
return xs.size();
}
void solve(){
int sz = compress(a, b), s = sz, t = s + 1;
V = t + 1;
for(int v = 0; v < V; v++) G[v].clear();
add_edge(s, 0, K, 0);
add_edge(sz - 1, t, K, 0);
for(int i = 0; i + 1 < sz; i++) add_edge(i, i + 1, INF, 0);
int sum = 0;
for(int i = 0; i < N; i++){
// 令所有负权边初始时满流
add_edge(b[i], a[i], 1, w[i]);
add_edge(s, b[i], 1, 0);
add_edge(a[i], t, 1, 0);
sum += w[i];
}
// 答案为最小费用流的相反数
printf("%d\n", sum - min_cost_flow(s, t, K + N));
}
int main(){
int t;
scanf("%d", &t);
while(t--){
scanf("%d%d", &N, &K);
for(int i = 0; i < N; i++) scanf("%d%d%d", a + i, b + i, w + i);
solve();
}
return 0;
}