您的位置:首页 > 其它

hdu 3572 Task Schedule(最大流,判断满流+isap模版)

2013-10-07 23:17 369 查看


Task Schedule

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 2371 Accepted Submission(s): 858



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

题意:有n个任务和m台机器。每个任务都分别给出pi,si,ei,表示该任务只能在第si到ei天进行,且任务必须进行pi天才能完成。每个任务可以分成pi个部分,每个部分的进行不一定是连续的;每个任务都可以在不同的机器进行,一台机器同一时间只能做一个任务。问能否在约束条件下完成这n个任务。思路:最大流判断是否满流。把每个任务看成一个点,源点s到每个任务连边,容量为任务需要运行的天数。
把每天看成一个点,每天向汇点t连边,容量为m,表示一天最多运行m个任务。
每个任务都向s—e天连边,容量为1,表示这个任务可以在这些天运行。
最后判断最大流是否等于所有任务需要运行的天数总和即可。

AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <queue>
#include <stack>
#include <vector>
#include <cmath>
#include <cstdlib>
#define L(rt) (rt<<1)
#define R(rt) (rt<<1|1)
#define ll long long
#define eps 1e-6
using namespace std;

const int INF=1000000000;
const int maxn=1005;
int eh[maxn],dis[maxn],low[maxn],cnt[maxn],pre[maxn],cur[maxn];
int n,m,s,t,num;
struct Edge
{
int u,v,cap,flow,next;
}et[maxn*maxn];
void init()
{
memset(eh,-1,sizeof(eh));
num=0;
}
void add(int u,int v,int cap,int flow)
{
Edge e={u,v,cap,flow,eh[u]};
et[num]=e;
eh[u]=num++;
}
void addedge(int u,int v,int cap)
{
add(u,v,cap,0);
add(v,u,0,0);
}
int isap(int s,int t,int n)
{
int u,v,now,flow=0;
memset(dis,0,sizeof(dis));
memset(cnt,0,sizeof(cnt));
memset(low,0,sizeof(low));
for(u=0;u<=n;u++) cur[u]=eh[u];
low[s]=INF,cnt[0]=n,u=s;
while(dis[s]<n)
{
for(now=cur[u];now!=-1;now=et[now].next)
if(et[now].cap-et[now].flow&&dis[u]==dis[v=et[now].v]+1) break;
if(now!=-1)
{
cur[u]=pre[v]=now;
low[v]=min(et[now].cap-et[now].flow,low[u]);
u=v;
if(u==t)
{
for(;u!=s;u=et[pre[u]].u)
{
et[pre[u]].flow+=low[t];
et[pre[u]^1].flow-=low[t];
}
flow+=low[t];
low[s]=INF;
}
}
else
{
if(--cnt[dis[u]]==0) break;
dis[u]=n,cur[u]=eh[u];
for(now=eh[u];now!=-1;now=et[now].next)
if(et[now].cap-et[now].flow&&dis[u]>dis[et[now].v]+1)
dis[u]=dis[et[now].v]+1;
cnt[dis[u]]++;
if(u!=s) u=et[pre[u]].u;
}
}
return flow;
}
int main()
{
int ca,pi,si,ei,c=0;
scanf("%d",&ca);
while(ca--)
{
scanf("%d%d",&n,&m);
init();
s=0;
int sum=0,max_day=0;
for(int i=1; i<=n; i++)
{
scanf("%d%d%d",&pi,&si,&ei);
sum+=pi;
max_day=max(max_day,ei);
addedge(s,i,pi);
for(int j=si;j<=ei;j++) addedge(i,j+n,1);
}
t=max_day+1+n;
for(int i=1; i<=max_day; i++) addedge(i+n,t,m);
printf("Case %d: ",++c);
if(sum==isap(s,t,t+1)) printf("Yes\n");
else printf("No\n");
puts("");
}
return 0;
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: