Task Schedule
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 8345 Accepted Submission(s): 2549
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
Author
allenlowesy
Source
2010 ACM-ICPC Multi-University Training Contest(13)——Host by UESTC
Recommend
zhouzeyong | We have carefully selected several similar problems for you: 3491 1533 3416 3081 3338
分析:最大流+满流判断(即为:最大流是否等于边上的流量和)
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
using namespace std;
struct edge{int to,cap,rev;};
const int INF=10000000,max_v=1010;
vector<edge> G[max_v];
int level[max_v],iter[max_v];
void Add_Edge(int from,int to,int cap){
G[from].push_back((edge){to,cap,G[to].size()});
G[to].push_back((edge){from,0,G[from].size()-1});
}
void BFS(int s){
queue<int> q;
memset(level,-1,sizeof level );
level[s]=0; q.push(s);
while(!q.empty()){
int u=q.front();q.pop();
for(int i=0;i<G[u].size();i++){
edge &e=G[u][i];
if(e.cap>0&&level[e.to]<0){
level[e.to]=level[u]+1;
q.push(e.to);
}
}
}
}
int DFS(int u,int t,int f){
int ret=0;
if(u==t) return f;
for(int &i=iter[u];i<G[u].size();i++){
edge &e=G[u][i];
if(level[u]<level[e.to] && e.cap){
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;
for(;;){
BFS(s);
if(level[t]<0) return flow;
memset(iter,0,sizeof iter );
int f;
while((f=DFS(s,t,INF))>0){
flow+=f;
}
}
return flow;
}
int main(){
int t,i,n,m,x,y,z,sum,j;
scanf("%d",&t);
int cas=1;
while(t--){
sum=0;
for(i=0;i<1005;i++) G[i].clear();
scanf("%d%d",&n,&m);
int maxx=0;
for(i=1;i<=n;i++){
scanf("%d%d%d",&x,&y,&z);
Add_Edge(0,i,x);
for(j=y+n;j<=z+n;j++)
Add_Edge(i,j,1);
sum+=x;
maxx=max(maxx,z);
}
for(i=n+1;i<=maxx+n;i++)
Add_Edge(i,1005,m);
int tmp=max_flow(0,1005);
printf("Case %d: ",cas++);
if(tmp==sum) printf("Yes\n\n");
else printf("No\n\n");
}
return 0;
}