您的位置:首页 > 理论基础 > 计算机网络

hdu 3572 Task Schedule 【网络最大流】

2014-10-29 09:07 302 查看

Task Schedule

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

Total Submission(s): 4063 Accepted Submission(s): 1360



[align=left]Problem Description[/align]
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.

[align=left]Input[/align]
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.

[align=left]Output[/align]
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.

[align=left]Sample Input[/align]

2
4 3
1 3 5
1 1 4
2 3 7
3 5 9

2 2
2 1 3
1 2 2


[align=left]Sample Output[/align]

Case 1: Yes

Case 2: Yes
分析:此题难点是建图,有学长说网络流如果图不是自己建的话就废了,很有道理啊,还发现如果数组开小了不一定反馈RE可能是W。建一个源点和汇点,首先把每个任务与源点连接,权值为要完成的天数p,然后每个任务要与其完成的时间范围的每一天连接,权值为1,最后每一天与汇点连接权值为m(因为只有m台机器,每天至多有m台机器工作),真是一个好的网络流建图问题。代码示例:#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
#include<queue>
#define Lh 500000
#define Le 850000
#define max 1000000000
using namespace std;
typedef struct
{
int to;
int w;
int next;
}node;
typedef struct
{
int x;
int t;
}DEP;
node E[Le];
DEP fir,nex;
int head[Lh],headx[Lh],deep[Lh],mark[Lh],cnt;
void ADD(int a,int b,int c)
{
E[++cnt].to=b;
E[cnt].w=c;
E[cnt].next=head[a];
head[a]=cnt;
E[++cnt].to=a;
E[cnt].w=0;
E[cnt].next=head[b];
head[b]=cnt;
}
int min(int x,int y)
{
return x<y?x:y;
}
int bfs(int s,int t,int n)
{
memset(deep,255,sizeof(deep));
queue<DEP>Q;
fir.x=s;
fir.t=0;
deep[s]=0;
Q.push(fir);
while(!Q.empty())
{
fir=Q.front();
Q.pop();
for(int i=head[fir.x];i;i=E[i].next)
{
nex.x=E[i].to;
nex.t=fir.t+1;
if(deep[nex.x]!=-1||!E[i].w)
continue;
deep[nex.x]=nex.t;
Q.push(nex);
}
}
for(int i=0;i<=n;i++)
headx[i]=head[i];
return deep[t]!=-1;
}
int dfs(int s,int t,int flow)
{
if(s==t)
return flow;
int newflow=0;
for(int i=headx[s];i;i=E[i].next)
{
headx[s]=i;
int to=E[i].to;
int w=E[i].w;
if(!w||deep[to]!=deep[s]+1)
continue;
int temp=dfs(to,t,min(w,flow-newflow));
newflow+=temp;
E[i].w-=temp;
E[i^1].w+=temp;
if(newflow==flow)
break;
}
if(!newflow)deep[s]=0;
return newflow;
}
int Dinic(int s,int t,int m)
{
int sum=0;
while(bfs(s,t,m))
{
sum+=dfs(s,t,max);
}
return sum;
}
int main()
{
int T,n,m;
int pi,si,ei;
int num,s,t,sum;
scanf("%d",&T);
for(int tt=1;tt<=T;tt++)
{
scanf("%d%d",&n,&m);
memset(head,0,sizeof(head));
memset(mark,0,sizeof(mark));
cnt=1;
s=1,t=2;
num=0,sum=0;
for(int i=1+t;i<=n+t;i++)
{
scanf("%d%d%d",&pi,&si,&ei);
sum+=pi;
ADD(s,i,pi);
num++;
for(int j=si+n+t;j<=ei+n+t;j++)
{
ADD(i,j,1);
num++;
if(!mark[j])
{
ADD(j,t,m);
mark[j]=1;
num++;
}
}
}
int w=Dinic(s,t,num);
if(w==sum)
{

printf("Case %d: Yes\n\n",tt);
}
else
{
printf("Case %d: No\n\n",tt);
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: