Task Schedule
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 8673 Accepted Submission(s): 2664
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
Author
allenlowesy
Source
题意:给N个任务,每个任务在Si~Ei天内任意Pi天完成,有M个机器,每次每个机器只能处理一个任务,问能否在完成所有任务。
思路:最大流,设置一个超级源点,向每个任务作边,边权为任务的Pi,每个任务向其Si~Ei作边,边权为1,每个被选中的Si~Ei这些点再向汇点作边,边权为M,表示每天最多有M台机器同时运作,那么看看最大流是否为Pi的和即可,考虑将源点和汇点交换过来,速度快了700多ms,使用Dinic算法,以前写的Dinic太弱了无限TLE,这里有两个优化,一是在层次图中一次DFS处理完所有增广路,二是当前结点通不到汇点就设为-1,下次不再搜索这条路。
# include <iostream>
# include <cstdio>
# include <cstring>
using namespace std;
const int maxn = 2e3+3;
const int INF = 0x7fffffff;
int cnt=0, dis[maxn], Next[maxn], vis[maxn], q[maxn];
struct node
{
int e, w, next;
}edge[200000];
void add_edge(int u, int v, int w)
{
edge[cnt].e = v;
edge[cnt].w = w;
edge[cnt].next = Next[u];
Next[u] = cnt++;
edge[cnt].e = u;
edge[cnt].w = 0;
edge[cnt].next = Next[v];
Next[v] = cnt++;
}
bool bfs()
{
memset(dis, -1, sizeof(dis));
dis[0] = 0;
int l=0, r=0;
q[r++] = 0;
while(l<r)
{
int u = q[l];
++l;
for(int i=Next[u]; i!=-1; i=edge[i].next)
{
int v = edge[i].e, w = edge[i].w;
if(dis[v]==-1 && w>0)
{
dis[v] = dis[u] + 1;
q[r++] = v;
}
}
}
return dis[1888] != -1;
}
int dfs(int u, int pre)
{
int f = 0, ans = 0;
if(u == 1888) return pre;
for(int i=Next[u]; i!=-1; i=edge[i].next)
{
int v = edge[i].e, w = edge[i].w;
if(dis[v]==dis[u]+1 && w>0 && (f=dfs(v, min(pre, w))))
{
edge[i].w -= f;
edge[i^1].w += f;
ans += f;
pre -= f;
if(!pre)
break;
}
}
if(ans)
return ans;
dis[u] = -1;
return 0;
}
int main()
{
int a, b, c, t, n, m, sum, total,cas=1;
scanf("%d",&t);
while(t--)
{
total = cnt = sum = 0;
scanf("%d%d",&n,&m);
memset(Next, -1, sizeof(Next));
memset(vis, 0, sizeof(vis));
int imin = INF, imax = 0;
for(int i=1; i<=n; ++i)
{
scanf("%d%d%d",&a,&b,&c);
for(int j=b; j<=c; ++j)
{
add_edge(j, i+500, 1);
if(!vis[j])
{
vis[j] = 1;
add_edge(0, j, m);
}
}
add_edge(i+500, 1888, a);
total += a;
}
int add;
while(bfs())
sum += dfs(0, INF);
if(sum == total)
printf("Case %d: Yes\n\n",cas++);
else
printf("Case %d: No\n\n",cas++);
}
return 0;
}