题意:有n个任务和m台机器,每个任务i必须在si天之后才能开始 (包括),在ei之前完成(包括),任务需要pi天完成。一直每台机器一次只能做一个任务。一个任务一天内只能在一个机器上做,但是不同的天可以用不同的机器,求是否能按时完成任务。
思路:这是一道最大流问题。关键时间图,我们可以建立一个超级源点与每个任务相连并且容量是pi,每个任务再与所对应的天建边,流量是1,然后每一天在与超级汇点建边,流量是m。然后用sap求出最大流量,若最大流量与sum(pi)相等,则Yes,否则No。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;
const int maxn = 1111;
const int maxm = 505000;
const int oo = 1 << 30;
int idx;
int cur[maxn], pre[maxn];
int dis[maxn], gap[maxn];
int aug[maxn], head[maxn];
struct Node
{
int u, v, w;
int next;
}edge[maxm];
void addEdge(int u, int v, int w)
{
edge[idx].u = u;
edge[idx].v = v;
edge[idx].w = w;
edge[idx].next = head[u];
head[u] = idx++;
edge[idx].u = v;
edge[idx].v = u;
edge[idx].w = 0;
edge[idx].next = head[v];
head[v] = idx++;
}
int SAP(int s, int e, int n)
{
int max_flow = 0, v, u = s;
int id, mindis;
aug[s] = oo;
pre[s] = -1;
memset(dis, 0, sizeof(dis));
memset(gap, 0, sizeof(gap));
gap[0] = n; // 我觉得这一句要不要都行,因为dis[e]始终为0
for (int i = 0; i <= n; ++i)
{ // 初始化当前弧为第一条弧
cur[i] = head[i];
}
while (dis[s] < n)
{
bool flag = false;
if (u == e)
{
max_flow += aug[e];
for (v = pre[e]; v != -1; v = pre[v]) // 路径回溯更新残留网络
{
id = cur[v];
edge[id].w -= aug[e];
edge[id^1].w += aug[e];
aug[v] -= aug[e]; // 修改可增广量,以后会用到
if (edge[id].w == 0) u = v; // 不回退到源点,仅回退到容量为0的弧的弧尾
}
}
for (id = cur[u]; id != -1; id = edge[id].next)
{ // 从当前弧开始查找允许弧
v = edge[id].v;
if (edge[id].w > 0 && dis[u] == dis[v] + 1) // 找到允许弧
{
flag = true;
pre[v] = u;
cur[u] = id;
aug[v] = min(aug[u], edge[id].w);
u = v;
break;
}
}
if (flag == false)
{
if (--gap[dis[u]] == 0) break; /* gap优化,层次树出现断层则结束算法 */
mindis = n;
cur[u] = head[u];
for (id = head[u]; id != -1; id = edge[id].next)
{
v = edge[id].v;
if (edge[id].w > 0 && dis[v] < mindis)
{
mindis = dis[v];
cur[u] = id; // 修改标号的同时修改当前弧
}
}
dis[u] = mindis + 1;
gap[dis[u]]++;
if (u != s) u = pre[u]; // 回溯继续寻找允许弧
}
}
return max_flow;
}
int main()
{
int t, n, m, pi, si, ei;
int Max, sum, source, sink, vn;
scanf("%d", &t);
for (int cas = 1; cas <= t; ++cas)
{
idx = 0;
memset(head, -1, sizeof(head));
sum = 0; source = 0; Max = 0;
scanf("%d %d", &n, &m);
for (int i = 1; i <= n; ++i)
{
scanf("%d %d %d", &pi, &si, &ei);
sum += pi;
Max = max(Max, ei);
addEdge(source, i, pi);
for (int j = si; j <= ei; ++j)
{
addEdge(i, n + j, 1);
}
}
sink = n + Max + 1;
vn = sink + 1;
for (int i = 1; i <= Max; ++i)
{
addEdge(n + i, sink, m);
}
if (SAP(source, sink, vn) == sum)
printf("Case %d: Yes\n\n", cas);
else printf("Case %d: No\n\n", cas);
}
return 0;
}