題目鏈接:http://poj.org/problem?id=3762
題意:有N個任務,每個任務有開始時間,結束時間和價值,現在有M天,每天的同一個時刻只能執行一個任務且每個任務必須連續執行,問能獲得的最大價值
思路:先將每個任務的開始時間和結束時間先hash後離散,這樣就可使點集數目cnt在2 * N以內了,題目也就相當於給出了若干區間段,選出權值和儘量大的一些區間使得任意一個數最多被M個區間所覆蓋,也就是經典的區間K覆蓋問題了,建圖如下:
1.源點到1結點連邊容量爲K費用爲0
2.i 到 i + 1結點連邊容量爲K費用爲0
3.對於每個區間[L, R],L到R結點連邊容量爲1費用爲-w
4.cnt向匯點連邊容量爲K費用爲0
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <utility>
#include <cmath>
#include <queue>
#include <set>
#include <map>
#include <climits>
#include <functional>
#include <deque>
#include <ctime>
#include <string>
#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
const int MAXN = 6000;
const int MAXM = 500000;
const int INF = 0x3f3f3f3f;
typedef long long ll;
struct Edge
{
int to, next, cap, flow, cost;
} edge[MAXM];
int head[MAXN], tol;
int pre[MAXN], dis[MAXN];
bool vis[MAXN];
int N;//節點總個數,節點編號從0~N-1
void init(int n)
{
N = n;
tol = 0;
memset(head, -1, sizeof(head));
}
void addedge(int u, int v, int cap, int cost)
{
edge[tol].to = v;
edge[tol].cap = cap;
edge[tol].cost = cost;
edge[tol].flow = 0;
edge[tol].next = head[u];
head[u] = tol++;
edge[tol].to = u;
edge[tol].cap = 0;
edge[tol].cost = -cost;
edge[tol].flow = 0;
edge[tol].next = head[v];
head[v] = tol++;
}
bool spfa(int s, int t)
{
queue<int>q;
for (int i = 0; i < N; i++)
{
dis[i] = INF;
vis[i] = false;
pre[i] = -1;
}
dis[s] = 0;
vis[s] = true;
q.push(s);
while (!q.empty())
{
int u = q.front();
q.pop();
vis[u] = false;
for (int i = head[u]; i != -1; i = edge[i].next)
{
int v = edge[i].to;
if (edge[i].cap > edge[i].flow &&
dis[v] > dis[u] + edge[i].cost )
{
dis[v] = dis[u] + edge[i].cost;
pre[v] = i;
if (!vis[v])
{
vis[v] = true;
q.push(v);
}
}
}
}
if (pre[t] == -1) return false;
else return true;
}
int res = 0;
//返回的是最大流,cost存的是最小費用
int minCostMaxflow(int s, int t, int &cost)
{
int flow = 0;
cost = 0;
while (spfa(s, t))
{
int Min = INF;
for (int i = pre[t]; i != -1; i = pre[edge[i ^ 1].to])
{
if (Min > edge[i].cap - edge[i].flow)
Min = edge[i].cap - edge[i].flow;
}
for (int i = pre[t]; i != -1; i = pre[edge[i ^ 1].to])
{
edge[i].flow += Min;
edge[i ^ 1].flow -= Min;
cost += edge[i].cost * Min;
res = min(cost, res);
}
flow += Min;
}
return flow;
}
struct node
{
int s, t, val;
bool operator < (const node &rhs) const
{
if (s != rhs.s) return s < rhs.s;
return t < rhs.t;
}
} p[MAXN];
int hash(char *s)
{
int s1 = (s[0] - '0') * 10 + (s[1] - '0');
int s2 = (s[3] - '0') * 10 + (s[4] - '0');
int s3 = (s[6] - '0') * 10 + (s[7] - '0');
return s1 * 3600 + s2 * 60 + s3;
}
int ti[MAXN];
int main()
{
int n, k;
while (~scanf("%d%d", &n, &k))
{
int cnt = 0;
for (int i = 0; i < n; i++)
{
char s[15];
scanf("%s", s);
p[i].s = hash(s);
ti[cnt++] = p[i].s;
scanf("%s", s);
p[i].t = hash(s);
ti[cnt++] = p[i].t;
int w;
scanf("%d", &w);
p[i].val = w;
}
sort(ti, ti + cnt);
cnt = unique(ti, ti + cnt) - ti;
for (int i = 0; i < n; i++)
{
p[i].s = lower_bound(ti, ti + cnt, p[i].s) - ti + 1;
p[i].t = lower_bound(ti, ti + cnt, p[i].t) - ti + 1;
}
// for (int i = 0; i < n; i++)
// printf("%d %d %d\n", p[i].s, p[i].t, p[i].val);
int s = 0, t = cnt + 1;
init(t + 1);
addedge(s, 1, k, 0);
addedge(cnt, t, k, 0);
for (int i = 0; i < n; i++)
addedge(p[i].s, p[i].t, 1, -p[i].val);
for (int i = 1; i < cnt; i++)
addedge(i, i + 1, k, 0);
res = 0;
int ans;
minCostMaxflow(s, t, ans);
cout << -res << endl;
}
return 0;
}