|
|
|
|
|
|
5 2
09:00:00 09:30:00 2
09:40:00 10:00:00 3
09:29:00 09:59:00 10
09:30:00 23:59:59 4
07:00:00 09:31:00 3
Sample Output
16
Hint
代码如下:
#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 = 1000000; const int maxN = 5000; const int maxM = 2014; const int oo = 0x3f3f3f3f; struct Edge{ int v, c, w, n; }; Edge edge[maxE]; int adj[maxN], l; int d[maxN], cur[maxN], f[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; f[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; f[v] = min(edge[i].c, f[u]); if(!inq[v]){ inq[v] = 1; Q[tail++] = v; } } } } if(d[t] == oo) return 0; flow += f[t]; cost += f[t] * d[t]; for(int i = cur[t]; ~i; i = cur[edge[i ^ 1].v]){ edge[i].c -= f[t]; edge[i ^ 1].c += f[t]; } return 1; } int MCMF(){ flow = cost = 0; while(SPFA()); return cost; } void work(){ int hh, mm, ss, ww, cnt1; MS1(adj); l = 0; cnt = 0; REP(i, n){ scanf("%d:%d:%d", &hh, &mm, &ss); a[i].l = hh * 3600 + mm * 60 + ss; scanf("%d:%d:%d", &hh, &mm, &ss); a[i].r = hh * 3600 + mm * 60 + ss; scanf("%d", &a[i].w); } REP(i, n){ b[cnt++] = a[i].l; b[cnt++] = a[i].r; } sort(b, b + cnt); cnt1 = 0; REP(i, cnt) if(i && b[i] != b[cnt1]) b[++cnt1] = b[i]; cnt = ++cnt1; 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(){ while(~scanf("%d%d", &n, &m)) work(); return 0; }