Task Schedule
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5921 Accepted Submission(s): 1895
Problem Description
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.
Input
On the first line comes an integer T(T<=20), indicating the number of test cases.
You are given two integer N(N<=500) and M(M<=200) on the first line of each test case. Then on each of next N lines are three integers Pi, Si and Ei (1<=Pi, Si, Ei<=500), which have the meaning described in the description. It is guaranteed that in a feasible schedule every task that can be finished will be done before or at its end day.
Output
For each test case, print “Case x: ” first, where x is the case number. If there exists a feasible schedule to finish all the tasks, print “Yes”, otherwise print “No”.
Print a blank line after each test case.
Sample Input
2
4 3
1 3 5
1 1 4
2 3 7
3 5 9
2 2
2 1 3
1 2 2
Sample Output
Case 1: Yes
Case 2: Yes
这道题是在vjudge上的最大流专题中找的,但当时看不出哪里是最大流,然后妥妥的百度。。。失败啊!!!
这道题最重要的如何抽象成图,进而用最大流解决。可以假设一个超级源点为0,把每个任务看成一个点,从1到n,,然后连线,容量为完成任务所需的时间。然后将此点和Si和Ei内的每一天连线,容量为1。然后将每一天和汇点连线,因为m台机器,故容量为m。然后,Dinic模板,比较一下求得的最大流和完成任务所需的总天数即可
#include <iostream>
#include <cstdio>
#include <cstring>
#include <set>
#include <cmath>
#include <queue>
#include <algorithm>
#include <functional>
using namespace std;
const int INF = 0x3f3f3f3f;
const int N = 1010;
struct edge
{
int to, cap, rev;
};
vector <edge> G[N];
int level[N];
int iter[N];
void add_edge(int from, int to, int cap)
{
edge e;
e.to = to, e.cap = cap, e.rev = G[to].size();
G[from].push_back(e);
e.to = from, e.cap = 0, e.rev = G[from].size() - 1;
G[to].push_back(e);
}
void bfs(int s)
{
memset(level, -1, sizeof(level));
queue <int> que;
level[s] = 0;
que.push(s);
while(! que.empty())
{
int v = que.front(); que.pop();
for(int i = 0; i < G[v].size(); i++)
{
edge &e = G[v][i];
if(e.cap > 0 && level[e.to] < 0)
{
level[e.to] = level[v] + 1;
que.push(e.to);
}
}
}
}
int dfs(int v, int t, int f)
{
if(v == t) return f;
for(int &i = iter[v]; i < G[v].size(); i++)
{
edge &e = G[v][i];
if(e.cap > 0 && level[v] < level[e.to])
{
int d = dfs(e.to, t, min(f, e.cap));
if(d > 0)
{
e.cap -= d;
G[e.to][e.rev].cap += d;
return d;
}
}
}
return 0;
}
int max_flow(int s, int t)
{
int flow = 0;
while(true)
{
bfs(s);
if(level[t] < 0) return flow;
memset(iter, 0, sizeof(iter));
int f;
while((f = dfs(s, t, INF)) > 0) flow += f;
}
}
int main()
{
int t, n, m, a, b, c, x = 0;
scanf("%d", &t);
while(t--)
{
scanf("%d%d", &n, &m);
int sum = 0, tmp = -1;
for(int i = 1; i <= n; i++)
{
scanf("%d%d%d", &a, &b, &c);
sum += a;
tmp = max(tmp, c);
add_edge(0, i, a);
for(int j = b; j <= c; j++)
add_edge(i, n+j, 1); //任务点到天数的连线
}
int s = tmp + n + 1;
for(int i = 1; i <= tmp; i++)
add_edge(n+i, s, m); //天数是从n+1到n+tmp
int res = max_flow(0, s);
if(res >= sum) printf("Case %d: Yes\n\n", ++x);
else printf("Case %d: No\n\n", ++x);
for(int i = 0; i < N; i++)
G[i].clear();
}
return 0;
}