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
题意 m个机器,n个任务,每个任务都有开始和结束的时间,一个机器一次只能够完成一个任务,问最后能否完成所有的任务。
看代码吧
#include<stdio.h>
#include<string.h>
#include<queue>
#define LL long long
using namespace std;
const int MAXN = 1000+100;
const int MAXM =1e6;
const int inf = 0x3f3f3f3f;
inline int read(){
int f=1,x=0;char ch=getchar();
while(ch<'0'||ch>'9') {if(ch=='-') f=-1;ch=getchar();}
while(ch>='0'&&ch<='9') {x=x*10+ch-'0';ch=getchar();}
return x*f;
}
/*------------------------------*/
struct Edge {
int from,to,cap,flow,nexts;
}edge[MAXM];
int head[MAXN],top=0;
int full_flow;
int last_time;
int S,T;
int n,m;
void init(){
memset(head,-1,sizeof(head));
top=0;
}
void addedge(int a,int b,int c){
Edge e={a,b,c,0,head[a]};
edge[top]=e;head[a]=top++;
e={b,a,0,0,head[b]};
edge[top]=e;head[b]=top++;
}
void getmap(){//*****
int p,st,ed;
last_time=0;
full_flow=0;
for(int i=1;i<=n;i++){
scanf("%d%d%d",&p,&st,&ed);
full_flow+=p;
addedge(S,i,p);
last_time=max(last_time,ed);
for(int j=st;j<=ed;j++){
addedge(i,n+j,1);
}
}
T=n+last_time+1;
for(int i=1;i<=last_time;i++){
addedge(n+i,T,m);
}
}
int vis[MAXN],dis[MAXN],cur[MAXN];
bool bfs(int st,int ed){
memset(vis,0,sizeof(vis));
memset(dis,-1,sizeof(dis));
queue<int>Q;Q.push(st);
vis[st]=1;dis[st]=0;
while(!Q.empty()){
int now=Q.front();Q.pop();
for(int i=head[now];i!=-1;i=edge[i].nexts){
Edge e=edge[i];
if(!vis[e.to]&&e.cap-e.flow>0){
vis[e.to]=1;
dis[e.to]=dis[now]+1;
if(e.to==ed) return 1;
Q.push(e.to);
}
}
}
return 0;
}
int dfs(int now,int a,int ed){
if(a==0||now==ed) return a;
int flow=0;int f;
for(int &i=cur[now];i!=-1;i=edge[i].nexts){
Edge &e=edge[i];
if(dis[e.to]==dis[now]+1&&(f=dfs(e.to,min(a,e.cap-e.flow),ed))>0){
e.flow+=f;
flow+=f;
edge[i^1].flow-=f;
a-=f;
if(a==0) break;
}
}
return flow;
}
int max_flow(int st,int ed){
int flow=0;
while(bfs(st,ed)){
memcpy(cur,head,sizeof(head));
flow+=dfs(st,inf,ed);
}
return flow;
}
int main(){
int t;scanf("%d",&t);
for(int i=1;i<=t;i++){
scanf("%d%d",&n,&m);S=0;
init();
getmap();
printf("Case %d: ",i);
if(full_flow==max_flow(S,T)) puts("Yes\n");
else puts("No\n");
}
return 0;
}