Task Schedule
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5004 Accepted Submission(s): 1634
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.
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.
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.
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
题意:有m台机器和n个任务,然后给出每个任务的开始时间和结束时间以及需要的天数,让你判断有没有这样的安排完成全部的任务。
思路:在时间区间建图,其实是在这个区间的每个点都连一条边;
建图方案:超级源点s到每个任务 i 连边,容量为第 i 个任务需要的天数,然后每个任务i 向它的时间区间的所有日期连一条容量为1的边,即从开始到结束都连,然后所有日期到汇点连容量m的边,因为m台机器同时最多能够做m个task。建图过程中记录源点的流量 , 最后再判断是否满流即可。
#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
#define MAXN 1000+100
#define MAXM 1000000+10
#define INF 1000000+10
using namespace std;
struct Edge
{
int from, to, cap, flow, next;
}edge[MAXM];
int dist[MAXN], vis[MAXN], cur[MAXN], top, head[MAXN];
int n, m;
int sumflow;//记录源点出去的流量
int sink;//记录超级汇点
void init()
{
top = 0;
memset(head, -1, sizeof(head));
}
void addedge(int u, int v, int w)
{
Edge E1 = {u, v, w, 0, head[u]};
edge[top] = E1;
head[u] = top++;
Edge E2 = {v, u, 0, 0, head[v]};
edge[top] = E2;
head[v] = top++;
}
void getmap()
{
int i, j;
int work, s, e;
int last = 0;//所有机器里面 最晚结束时间
sumflow = 0;
for(i = 1; i <= n; i++)
{
scanf("%d%d%d", &work, &s, &e);
sumflow += work;
addedge(0, i, work);//超级源点 连接每个机器 容量为机器完成一个任务的工作时间
last = max(last, e);
for(j = s; j <= e; j++)
addedge(i, n+j, 1);//连接所有时间点
}
sink = n+last+1;
for(j = 1; j <= last; j++)
addedge(n+j, sink, m);连接超级汇点 容量为所有机器同时可以做的任务个数
}
bool BFS(int start, int end)
{
queue<int> Q;
memset(dist, -1, sizeof(dist));
memset(vis, 0, sizeof(vis));
while(!Q.empty()) Q.pop();
Q.push(start);
vis[start] = 1;
dist[start] = 0;
while(!Q.empty())
{
int u = Q.front();
Q.pop();
for(int i = head[u]; i != -1; i = edge[i].next)
{
Edge E = edge[i];
if(!vis[E.to] && E.cap > E.flow)
{
vis[E.to] = 1;
dist[E.to] = dist[u] + 1;
if(E.to == end) return true;
Q.push(E.to);
}
}
}
return false;
}
int DFS(int x, int a, int end)
{
if(x == end || a == 0) return a;
int flow = 0, f;
for(int& i = cur[x]; i != -1; i = edge[i].next)
{
Edge& E = edge[i];
if(dist[E.to] == dist[x]+1 && (f = DFS(E.to, min(a, E.cap-E.flow), end)) > 0)
{
E.flow += f;
edge[i^1].flow -= f;
flow += f;
a -= f;
if(a == 0) break;
}
}
return flow;
}
int Maxflow(int start, int end)
{
int flow = 0;
while(BFS(start, end))
{
memcpy(cur, head, sizeof(head));
flow += DFS(start, INF, end);
}
return flow;
}
int main()
{
int t;
int k = 1;
scanf("%d", &t);
while(t--)
{
scanf("%d%d", &n, &m);
init();
getmap();
printf("Case %d: ", k++);
if(Maxflow(0, sink) == sumflow)//满流
printf("Yes\n\n");
else
printf("No\n\n");
}
return 0;
}
dinic: