题目大意:给出N个时间段,每个时间段都有其相应的收益。
限制条件是每个时刻最多只能被覆盖k次
问最多的收益是多少
解题思路:和上一题的思路是一样的,这里就不详写了
给出传送门
#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
#include <algorithm>
using namespace std;
const int MAXNODE = 4010;
const int MAXEDGE = 100010;
typedef int Type;
const Type INF = 0x3f3f3f3f;
struct Edge{
int u, v, next;
Type cap, flow, cost;
Edge() {}
Edge(int u, int v, Type cap, Type flow, Type cost, int next): u(u), v(v), cap(cap), flow(flow), cost(cost), next(next) {}
};
struct MCMF{
int n, m, s, t;
Edge edges[MAXEDGE];
int head[MAXNODE];
int p[MAXNODE];
Type d[MAXNODE];
Type a[MAXNODE];
bool inq[MAXNODE];
void init(int n) {
this->n = n;
memset(head, -1, sizeof(head));
m = 0;
}
void AddEdge(int u, int v, Type cap, Type cost) {
edges[m] = Edge(u, v, cap, 0, cost, head[u]);
head[u] = m++;
edges[m] = Edge(v, u, 0, 0, -cost, head[v]);
head[v] = m++;
}
bool BellmanFord(int s, int t, Type &flow, Type &cost) {
for (int i = 0; i < n; i++) d[i] = INF;
memset(inq, 0, sizeof(inq));
d[s] = 0; inq[s] = true; p[s] = 0; a[s] = INF;
queue<int> Q;
Q.push(s);
while (!Q.empty()) {
int u = Q.front(); Q.pop();
inq[u] = false;
for (int i = head[u]; ~i; i = edges[i].next) {
Edge &e = edges[i];
if (e.cap > e.flow && d[e.v] > d[u] + e.cost) {
d[e.v] = d[u] + e.cost;
p[e.v] = i;
a[e.v] = min(a[u], e.cap - e.flow);
if (!inq[e.v]) {
Q.push(e.v); inq[e.v] = true;
}
}
}
}
if (d[t] == INF) return false;
flow += a[t];
cost += a[t] * d[t];
int u = t;
while (u != s) {
edges[p[u]].flow += a[t];
edges[p[u] ^ 1].flow -= a[t];
u = edges[p[u]].u;
}
return true;
}
Type MinCost(int s, int t) {
this->s = s; this->t = t;
Type flow = 0, cost = 0;
while (BellmanFord(s, t, flow, cost));
return cost;
}
}mcmf;
#define maxn 2010
#define maxm 4010
int n, k;
int u[maxn], v[maxn], c[maxn];
int tmp[maxm];
void init() {
int cnt = 0;
int h1, h2, m1, m2, s1, s2;
for (int i = 1; i <= n; i++) {
scanf("%d:%d:%d %d:%d:%d %d", &h1, &m1, &s1, &h2, &m2, &s2, &c[i]);
u[i] = h1 * 60 * 60 + m1 * 60 + s1;
v[i] = h2 * 60 * 60 + m2 * 60 + s2;
tmp[cnt++] = u[i]; tmp[cnt++] = v[i];
}
sort(tmp, tmp + cnt);
int m = 1;
for (int i = 1; i < cnt; i++)
if (tmp[i] != tmp[i - 1]) tmp[m++] = tmp[i];
mcmf.init(m + 2);
for (int i = 1; i < m; i++) mcmf.AddEdge(i - 1, i, k, 0);
for (int i = 1; i <= n; i++) {
int l = lower_bound(tmp, tmp + m, u[i]) - tmp;
int r = lower_bound(tmp, tmp + m, v[i]) - tmp;
mcmf.AddEdge(l, r, 1, -c[i]);
}
mcmf.AddEdge(m, 0, k, 0);
mcmf.AddEdge(m - 1, m + 1, k, 0);
printf("%d\n", -mcmf.MinCost(m, m + 1));
}
int main() {
while (scanf("%d%d", &n, &k) != EOF) {
init();
}
return 0;
}