POJ 3762 The Bonus Salary!(最小K覆盖)

时间:2022-03-24 15:58:31

POJ 3762 The Bonus Salary!

题目链接

题意:给定一些任务。每一个任务有一个时间,有k天。一个时间仅仅能运行一个任务,每一个任务有一个价值。问怎么安排能得到最多价值

思路:典型的区间k覆盖问题

代码:

#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <algorithm>
#include <map>
using namespace std; const int MAXNODE = 4505;
const int MAXEDGE = 100005;
typedef int Type;
const Type INF = 0x3f3f3f3f; struct Edge {
int u, v;
Type cap, flow, cost;
Edge() {}
Edge(int u, int v, Type cap, Type flow, Type cost) {
this->u = u;
this->v = v;
this->cap = cap;
this->flow = flow;
this->cost = cost;
}
}; struct MCFC {
int n, m, s, t;
Edge edges[MAXEDGE];
int first[MAXNODE];
int next[MAXEDGE];
int inq[MAXNODE];
Type d[MAXNODE];
int p[MAXNODE];
Type a[MAXNODE]; void init(int n) {
this->n = n;
memset(first, -1, sizeof(first));
m = 0;
} void add_Edge(int u, int v, Type cap, Type cost) {
edges[m] = Edge(u, v, cap, 0, cost);
next[m] = first[u];
first[u] = m++;
edges[m] = Edge(v, u, 0, 0, -cost);
next[m] = first[v];
first[v] = m++;
} bool bellmanford(int s, int t, Type &flow, Type &cost) { for (int i = 0; i < n; i++) d[i] = INF;
memset(inq, false, sizeof(inq));
d[s] = 0; inq[s] = true; p[s] = s; a[s] = INF;
queue<int> Q;
Q.push(s);
while (!Q.empty()) {
int u = Q.front(); Q.pop();
inq[u] = false;
for (int i = first[u]; i != -1; i = next[i]) {
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 += d[t] * a[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) {
Type flow = 0, cost = 0;
while (bellmanford(s, t, flow, cost));
return cost;
}
} gao; const int N = 2005; map<int, int> hash;
int n, k, hn; int get(int x) {
if (!hash.count(x)) hash[x] = hn++;
return hash[x];
} struct Task {
int l, r, w;
} task[N]; int main() {
while (~scanf("%d%d", &n, &k)) {
hash.clear();
hn = 1;
int h1, m1, s1, h2, m2, s2, w;
for (int i = 0; i < n; i++) {
scanf("%d:%d:%d %d:%d:%d %d", &h1, &m1, &s1, &h2, &m2, &s2, &w);
int t1 = h1 * 3600 + m1 * 60 + s1;
int t2 = h2 * 3600 + m2 * 60 + s2;
task[i].l = get(t1); task[i].r = get(t2); task[i].w = w;
}
gao.init(hn + 1);
for (int i = 0; i < n; i++)
gao.add_Edge(task[i].l, task[i].r, 1, -task[i].w);
int pre = 0;
for (map<int, int>::iterator it = hash.begin(); it != hash.end(); it++) {
gao.add_Edge(pre, it->second, k, 0);
pre = it->second;
}
gao.add_Edge(pre, hn, k, 0);
printf("%d\n", -gao.Mincost(0, hn));
}
return 0;
}