https://vjudge.net/problem/HDU-3572
题意:Our geometry princess XMM has stoped her study in computational geometry to concentrate on her newly opened factory. Her factory has introduced M new machines in order to process the coming N tasks. For the i-th task, the factory has to start processing it at or after day Si, process it for Pi days, and finish the task before or at day Ei. A machine can only work on one task at a time, and each task can be processed by at most one machine at a time. However, a task can be interrupted and processed on different machines on different days.
Now she wonders whether he has a feasible schedule to finish all the tasks in time. She turns to you for help.
特殊建图, 源点到每个人连线,每个人到每一天连线,保证每个人每天只能用一台机器。
再求最大流,比较最大流和所需要的流即可。
#include <iostream>
#include <queue>
#include <vector>
#include <memory.h>
using namespace std;
typedef long long LL;
const int MAXN = 5e2+10;
const int INF = 1<<30;
struct Edge
{
int from, to, cap;
Edge(int f, int t, int c):from(f), to(t), cap(c){}
};
vector<Edge> edges;
vector<int> G[MAXN*4];
int Dis[MAXN*4];
int Vis[MAXN];
int n, m;
int s, t;
bool Bfs()
{
//Bfs构造分层网络
memset(Dis, -1, sizeof(Dis));
queue<int> que;
que.push(s);
Dis[s] = 0;
while (!que.empty())
{
int u = que.front();
que.pop();
for (int i = 0;i < G[u].size();i++)
{
Edge & e = edges[G[u][i]];
if (e.cap > 0 && Dis[e.to] == -1)
{
que.push(e.to);
Dis[e.to] = Dis[u]+1;
}
}
}
return (Dis[t] != -1);
}
int Dfs(int u, int flow)
{
//flow 表示当前流量上限
if (u == t)
return flow;
int res = 0;
for (int i = 0;i < G[u].size();i++)
{
Edge & e = edges[G[u][i]];
if (e.cap > 0 && Dis[u]+1 == Dis[e.to])
{
int tmp = Dfs(e.to, min(flow, e.cap)); // 递归计算顶点 v
flow -= tmp;
e.cap -= tmp;
res += tmp;
edges[G[u][i]^1].cap += tmp;
if (flow == 0)
break;
}
}
if (res == 0)
Dis[u] = -1;
return res;
}
int MaxFlow()
{
int res = 0;
while (Bfs())
{
res += Dfs(0, INF);
}
return res;
}
void Insert(int l, int r, int c)
{
// 正反向插边
edges.emplace_back(l, r, c);
edges.emplace_back(r, l, 0);
G[l].push_back(edges.size()-2);
G[r].push_back(edges.size()-1);
}
void Init()
{
edges.clear();
for (int i = 0;i <= t;i++)
G[i].clear();
}
int main()
{
int l, r, c;
int times, cnt = 0;
scanf("%d", ×);
while (times--)
{
scanf("%d%d", &n, &m);
s = 0, t = 500+n+1;
Init();
int sum = 0;
for (int i = 1;i <= n;i++)
{
scanf("%d%d%d", &c, &l, &r);
Insert(0, i, c);
for (int j = l;j <= r;j++)
{
Insert(i, n+j, 1);
Vis[j] = 1;
}
sum += c;
}
for (int i = 1;i <= 500;i++)
if (Vis[i])
Insert(n+i, t, m);
int res = MaxFlow();
if (res == sum)
printf("Case %d: Yes\n\n", ++cnt);
else
printf("Case %d: No\n\n", ++cnt);
}
return 0;
}