Task Schedule
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6010 Accepted Submission(s): 1925
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
//要将图转化一下。建立一个源点,一个汇点。将图连通。。。具体看代码
#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
#define N 1010
#define INF 0x3f3f3f3f
using namespace std;
int head[N],edgenum;
int c[N];
int dis[N];
int vis[N];
int n,m;
struct zz
{
int from;
int to;
int cap;
int flow;
int next;
}edge[N*1000];
void add(int u,int v,int w)
{
zz E={u,v,w,0,head[u]};
edge[edgenum]=E;
head[u]=edgenum++;
zz EE={v,u,0,0,head[v]};
edge[edgenum]=EE;
head[v]=edgenum++;
}
queue<int>q;
bool bfs(int s,int e)
{
memset(dis,-1,sizeof(dis));
memset(vis,0,sizeof(vis));
while(!q.empty())
q.pop();
q.push(s);
dis[s]=0;
vis[s]=1;
while(!q.empty())
{
int u=q.front();
q.pop();
for(int i=head[u];i!=-1;i=edge[i].next)
{
zz v=edge[i];
if(!vis[v.to]&&v.cap>v.flow)
{
dis[v.to]=dis[u]+1;
vis[v.to]=1;
if(v.to==e)
return true;
q.push(v.to);
}
}
}
return false;
}
int dfs(int x,int a,int e)
{
if(x==e||a==0)
return a;
int flow=0,f;
for(int &i=c[x];i!=-1;i=edge[i].next)
{
zz &v=edge[i];
if(dis[v.to]==dis[x]+1&&(f=dfs(v.to,min(a,v.cap-v.flow),e))>0)
{
v.flow+=f;
edge[i^1].flow-=f;
flow+=f;
a-=f;
if(a==0)
break;
}
}
return flow;
}
int maxflow(int s,int e)
{
int flow=0;
while(bfs(s,e))
{
memcpy(c,head,sizeof(head));
flow+=dfs(s,INF,e);
}
return flow;
}
int sum,ee;
int main()
{
int t;
int T=1;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
edgenum=0;
memset(head,-1,sizeof(head));
int mm=0;
sum=0;
int u,v,w;
for(int i=1;i<=n;i++)
{
scanf("%d%d%d",&w,&u,&v);
sum+=w;
add(0,i,w);
mm=max(mm,v);
for(int j=u;j<=v;j++)
add(i,n+j,1);
}
ee=n+mm+1;
for(int i=1;i<=mm;i++)
add(n+i,ee,m);
printf("Case %d: ",T++);
if(maxflow(0,ee)>=sum)
printf("Yes\n\n");
else
printf("No\n\n");
}
return 0;
}