|
|
|
|
|
|
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
题目大意:给你 n 个开区间,区间有权值,问如何选择区间使得每个点上覆盖的区间数不超过 k 个的前提下获得的最大权值和。
#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], a[maxN]; 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; a[s] = 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; a[v] = min(edge[i].c, a[u]); if(!inq[v]){ inq[v] = 1; Q[tail++] = v; } } } } if(d[t] == oo) return 0; flow += a[t]; cost += a[t] * d[t]; for(int i = cur[t]; ~i; i = cur[edge[i ^ 1].v]){ edge[i].c -= a[t]; edge[i ^ 1].c += a[t]; } 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; }