Description:
小K爸爸的工厂最近生意红火!小K也利用自己的所学所能帮助他的父亲。
有N位客户希望工厂为他们加工产品。每位客户都提供了需要加工的产品的类型,产品到达工厂的时间s和最迟完成加工的时间t。小K根据需要加工的产品类型预计了每个产品加工所需的时间c(时间i可以认为是第i分钟开始的时刻)。工厂里的生产车间一共有M台机器。每个产品在每台机器上都可以加工,但是,一台机器在任何时候最多只能加工一件产品,而一件产品在任何时候也最多只能被一台机器加工。同时,我们可以在某台机器正在加工时将工作打断,换另一个产品加工。小K希望帮助他父亲计算一下,能否找到一个方案,使得所有的产品都在规定的时间内完成加工?
题解:
裸的最大流啊。
先考虑对每个时间都建一个点,超级源S到它连流量是m的边。
时间到属于它的任务连流量为1的边,任务到超级汇T连流量为c的边。
如果最大流=∑c,则Yes。
但是这显然会T。
注意到n很小,那么标记是起点和终点的点,没有被标记的连续的一段点连的边是一样的,压成一个点,总点数就是O(n)的。
Code:
#include<cstdio>
#include<cstring>
#include<algorithm>
#define fo(i, x, y) for(int i = x; i <= y; i ++)
#define min(a, b) ((a) < (b) ? (a) : (b))
using namespace std;
const int N = 2e5 + 5, E = 5e6 + 5, INF = 1 << 30;
int Q, n, m, c[N], s[N], t[N];
int bz[N], td, tf;
struct node {
int x, y;
} a[N];
int final[N], to[E], next[E], r[E], tot;
int S, T, co[N], d[N], cur[N], sum, ans;
void Clear() {
memset(bz, 0, sizeof bz);
fo(i, 1, T) final[i] = 0;
fo(i, 1, tot) next[i] = 0;
tot = 1; td = 0;
sum = 0; ans = 0;
memset(co, 0, sizeof co);
memset(d, 0, sizeof d);
}
void link(int x, int y, int z) {
next[++ tot] = final[x], to[tot] = y, r[tot] = z, final[x] = tot;
next[++ tot] = final[y], to[tot] = x, r[tot] = 0, final[y] = tot;
}
int dg(int x, int flow) {
if(x == T) return flow;
int use = 0;
for(int i = cur[x]; i; i = next[i], cur[x] = i) {
int y = to[i];
if(d[y] + 1 == d[x] && r[i]) {
int tmp = dg(y, min(flow - use, r[i]));
use += tmp; r[i] -= tmp; r[i ^ 1] += tmp;
if(use == flow) return use;
}
}
cur[x] = final[x];
if(!(-- co[d[x]])) d[T] = T;
++ co[++ d[x]];
return use;
}
int main() {
for(scanf("%d", &Q); Q; Q --) {
Clear();
scanf("%d %d", &n, &m);
fo(i, 1, n) scanf("%d %d %d", &c[i], &s[i], &t[i]), t[i] --;
fo(i, 1, n) bz[s[i]] = bz[t[i]] = 1;
int la = -1;
fo(i, 0, 1e5) if(bz[i]) {
if(la != i - 1) {
a[++ td].x = la + 1;
a[td].y = i - 1;
}
a[++ td].x = i;
a[td].y = i;
la = i;
}
tf = td; td += n;
S = td + 1; T = S + 1;
fo(i, 1, tf) link(S, i, m * (a[i].y - a[i].x + 1));
fo(i, 1, tf) fo(j, 1, n) {
if(s[j] <= a[i].x && t[j] >= a[i].y)
link(i, j + tf, a[i].y - a[i].x + 1);
}
fo(i, 1, n) link(i + tf, T, c[i]), sum += c[i];
co[0] = T;
for(; d[T] < T;) ans += dg(S, INF);
if(ans == sum) printf("Yes\n"); else printf("No\n");
}
}