Task Schedule
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 10473 Accepted Submission(s): 3219
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
24 31 3 5 1 1 42 3 73 5 92 22 1 31 2 2
Sample Output
Case 1: Yes Case 2: Yes
难点在建图
#include<iostream>
#include<algorithm>
#include<queue>
#include<cstring>
using namespace std;
#define M 1100
#define inf 0x3f3f3f3f
int id,t,n,m;
int head[M],vis[M];
struct Edeg
{
int u,v,w,next;
}G[M*M];
void add(int u,int v,int w)//使用邻接表
{
G[id].u=u; G[id].v=v; G[id].w=w;
G[id].next=head[u]; head[u]=id++;
G[id].u=v; G[id].v=u; G[id].w=0;
G[id].next=head[v]; head[v]=id++;
}
int bfs()
{
int s=0;
memset(vis,-1,sizeof(vis));
queue<int> q;
while(!q.empty()) q.pop();
vis[s]=0;
q.push(s);
while(!q.empty())
{
s=q.front();
q.pop();
for(int i=head[s];i!=-1;i=G[i].next)
{
int v=G[i].v;
if(vis[v]==-1&&G[i].w)
{
vis[v]=vis[s]+1;
if(v==t) return 1;
q.push(v);
}
}
}
return 0;
}
int dfs(int s,int low)
{
if(s==t) return low;
int minflow,ans=0;
for(int i=head[s];i!=-1;i=G[i].next)
{
int v=G[i].v;
if(G[i].w&&vis[v]==vis[s]+1&&(minflow=dfs(v,min(low,G[i].w))))
{
G[i].w-=minflow;
G[i^1].w+=minflow;
ans+=minflow;
if(ans==low) break;//不能直接return ans;超时
}
}
return ans;
}
int main()
{
int C,time=1;
cin>>C;
while(C--)
{
int t1=inf,t2=-1,sum=0,s[505],p[505],e[505];
cin>>n>>m;
for(int i=1;i<=n;i++)
{
cin>>p[i]>>s[i]>>e[i];
t1=min(t1,s[i]);
t2=max(t2,e[i]);
sum+=p[i];
}
t=2*t2+1,id=0;
memset(head,-1,sizeof(head));
for(int i=t1;i<=t2;i++)
add(0,i,m);
for(int i=1;i<=n;i++)
for(int j=s[i];j<=e[i];j++)
{
add(j,j+t2,1);
add(j+t2,t,m);
}
int ans=0,flow;
while(bfs())
{
while(flow=dfs(0,inf))
ans+=flow;
}
if(sum<=ans)
printf("Case %d: Yes\n\n",time++);
else printf("Case %d: No\n\n",time++);
}
}