Task Schedule HDU

来源:互联网 发布:邮箱的正则表达式的js 编辑:程序博客网 时间:2024/06/06 03:59

Task Schedule

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 8495    Accepted Submission(s): 2600


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
24 31 3 5 1 1 42 3 73 5 92 22 1 31 2 2
 

Sample Output
Case 1: Yes Case 2: Yes



最大流问题,题意给出n种机器,m个任务,给出每一个任务完成需要的时间p,要在s,e时间段内完成,问可不可以完成(每个任务可以分部分做)。

建图方法,根据题意建立源点,与每个任务相连,流量为每个任务需要工作的天数。然后把这个点与可以工作的时间点相连,流量为1,然后每个时间点与超级汇点相连,流量自然是m,也就是机器的个数。

这样图就建好了。之前一直用EK算法,今天终于坑了,还有一个算法是O(n2m)理论下届的算法,因为上届很松,所以一般也就是用这个方法。

总算是又学习了一种。

#include <bits/stdc++.h>using namespace std;const int inf=1e9;const int MAXN=1000+10;int n,m,s,e;struct node{    int u,v,f;    int next;} edge[MAXN*520];int head[MAXN];int cnt;void add(int u,int v,int f){    edge[cnt].u=u;    edge[cnt].v=v;    edge[cnt].f=f;    edge[cnt].next=head[u];    head[u]=cnt++;    edge[cnt].u=v;    edge[cnt].v=u;    edge[cnt].f=0;    edge[cnt].next=head[v];    head[v]=cnt++;}int dis[MAXN];int bfs(int s,int t){    int u, v ;    memset(dis,-1,sizeof(dis));    dis[s] = 0 ;    queue<int>q;    q.push(s) ;    while( !q.empty() )    {        u = q.front();        q.pop();        for(int i = head[u] ; i != -1 ; i = edge[i].next)        {            v = edge[i].v ;            if( dis[v] == -1 && edge[i].f )            {                dis[v] = dis[u] + 1 ;                q.push(v) ;            }        }    }    if( dis[t] > 0 )        return 1 ;    return 0 ;}int dfs(int s,int t,int min1){    if( s == t )        return min1 ;    int  flow, ans = 0 ;    for(int i = head[s] ; i != -1 ; i = edge[i].next)    {        int v = edge[i].v ;        if( dis[v] == dis[s] + 1 && edge[i].f && (flow = dfs(v,t,min(min1,edge[i].f) ) ) )        {            edge[i].f -= flow ;            edge[i^1].f += flow ;            ans += flow ;            min1 -= flow ;            if( !min1 )                break;        }    }    if( ans )        return ans ;    dis[s] = -1 ;    return 0;}int getMaxFlow(){    int maxFlow=0,flow;    while(bfs(s,e))    {        while((flow=dfs(s,e,inf))>0)            maxFlow+=flow;    }    return maxFlow;}int main(){    int t;    scanf("%d",&t);    for(int tt=1; tt<=t; ++tt)    {        scanf("%d%d",&n,&m);        cnt=0;        memset(head,-1,sizeof(head));        int p,st,ed;        int MAX=0;        int ans=0;        for(int i=1; i<=n; ++i)        {            scanf("%d%d%d",&p,&st,&ed);            MAX=max(MAX,ed);            ans+=p;            add(0,i,p);            for(int j=st; j<=ed; ++j)            {                add(i,j+n,1);            }        }        s=0;        e=MAX+n+1;        for(int j=1; j<=MAX; ++j)add(j+n,e,m);        printf("Case %d: ",tt);        if(getMaxFlow()==ans)puts("Yes\n");        else puts("No\n");    }    return 0;}







0 0
原创粉丝点击