hdu 3572 Task Schedule

Task Schedule

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或si后开始执行,在ei或ei前执行完毕,每个任务可以分段,在任何一台机器执行,每台机器在一个时间只能执行一个任务。。

题解:拆点的最大流判断是否满流,建图是重点。考虑每个任务可以在任何一台机器执行,因此每个任务在规定的时间内就可以分到任何一台机器(一条机器在一个时间点只执行一个任务),所以将时间进行拆点,添加一个源点S,从源点S向每个任务连边,因为每个任务的执行时间是pi,所以从超级源点连向每个任务的流是pi,即建边add_edge (S,i,pi);然后考虑每个任务的执行,一个任务只能同时在一个时间点被工作,即不能同时既在时间点A上加工又在时间点B上加工,,所以每一个任务 i 向时间点[s,e]连一条容量为1的边; add_edge (i,s--->t,1);最后每一个时间点向T连一条容量为m个边,说明一个时间点只能最多同时有m个机器在工作。最后判断是否漫满流,即判断从S流出的n?p个流量能否全部流入T中。

参考代码:

#include <stdio.h>
#include <algorithm>
#include <math.h>
#include <string.h>
#include <vector>
#include <queue>
#include <stack>
#include <iostream>
#define pi acos(-1.0)
#define INF 0x3f3f3f3f
using namespace std;
#define ll long long
const int maxm=251000+7;
const int maxn=1010;
struct edge
{
    int t,w,f,next;
}e[2*maxm];
int dis[maxn];
int head[maxn],cnt;
void add_edge(int u,int v,int f)
{
    e[cnt].t=v,e[cnt].f=f;
    e[cnt].next=head[u];
    head[u]=cnt++;

    e[cnt].t=u,e[cnt].f=0;
    e[cnt].next=head[v];
    head[v]=cnt++;
}
void init()
{
    memset(head,-1,sizeof head);
    cnt=0;
}

int bfs(int t)
{
    memset(dis,-1,sizeof dis);
    queue<int> q;
    dis[0]=1;
    q.push(0);
    while(!q.empty())
    {
        int k=q.front();q.pop();
        for(int i=head[k];i!=-1;i=e[i].next)
        {
            int v=e[i].t;
            if(e[i].f&&dis[v]==-1)
            {
                dis[v]=dis[k]+1;
                if(v==t) return 1;
                q.push(v);
            }
        }
    }
    return dis[t]!=-1;
}
int dfs(int s,int t,int f)
{
    if(s==t||!f)
        return f;
    int r=0;
    for (int i=head[s]; i!=-1; i=e[i].next)
    {
        int v=e[i].t;
        if(dis[v]==dis[s]+1&&e[i].f)
        {
            int d=dfs(v,t,min(f,e[i].f));
            if(d>0)
            {
                e[i].f-=d;
                e[i^1].f+=d;
                r+=d;
                f-=d;
                if(!f)
                    break;
            }
        }
    }
    if(!r)
        dis[s]=INF;
    return r;
}
int  main()
{
    freopen("C:\\Users\\Administrator\\Desktop\\a.txt","r",stdin);
    //ios::sync_with_stdio(false);
    //freopen("C:\\Users\\Administrator\\Desktop\\b.txt","w",stdout);
    int T,n,m,Case=0;
    scanf("%d",&T);
    while(T--)
    {
        int st=INF,et=0,sump=0;
        scanf("%d%d",&n,&m);
        init();
        for(int i=1;i<=n;i++)
        {
            int p,s,e;
            scanf("%d%d%d",&p,&s,&e);
            add_edge(0,i,p);
            sump+=p;
            st=min(st,s);
            et=max(et,e);
            for(int j=s;j<=e;j++)
                add_edge(i,n+j,1);
        }
        int t=n+et-st+2;
        //cout<<n+st<<" "<<n+et<<" "<<t<<endl;
        for(int i=st+n;i<=et+n;i++)
            add_edge(i,t,m);
        int ans=0,res;
        while(bfs(t))
        {
            res=dfs(0,t,INF);
            ans+=res;
        }
        //printf("%d\n",ans);
        printf("Case %d: ",++Case);
        if(ans==sump) puts("Yes");
        else puts("No");
        printf("\n");
    }
    return 0;
}

  

  

时间: 2024-10-11 09:38:59

hdu 3572 Task Schedule的相关文章

HDU 3572 Task Schedule(ISAP)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3572 题意:m台机器,需要做n个任务.第i个任务,你需要使用机器Pi天,且这个任务要在[Si  ,  Ei]区间内完成才有效.对于一个任务,只能由一个机器来完成,一个机器同一时间只能做一个任务.当然,一个任务可以分成几段不连续的时间来完成.问,能否做完全部任务. 题意很清晰,也就是判断是否是满流. 对于网络流问题,模板大家都有,关键在于如何建图(详见资料) 思路:今天问了龙哥,对建图有了一定的了解,

hdu 3572 Task Schedule(最大流)

hdu 3572 Task Schedule 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 tas

hdu 3572 Task Schedule(网络流 dinic算法)

Task Schedule Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 3412    Accepted Submission(s): 1197 Problem Description Our geometry princess XMM has stoped her study in computational geometry t

HDU 3572 Task Schedule(拆点+最大流dinic)

Task Schedule Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 7753    Accepted Submission(s): 2381 Problem Description Our geometry princess XMM has stoped her study in computational geometry t

HDU 3572 Task Schedule (最大流)

C - Task Schedule Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 3572 Description Our geometry princess XMM has stoped her study in computational geometry to concentrate on her newly opened fac

hdu 3572 Task Schedule 最大流 Dinic算法,,卡时间。。建图非常有讲究

Task Schedule Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 4617    Accepted Submission(s): 1513 Problem Description Our geometry princess XMM has stoped her study in computational geometry t

hdu 3572 Task Schedule (Dinic模板)

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

HDU 3572 Task Schedule(ISAP模板&amp;amp;&amp;amp;最大流问题)

题目链接:http://acm.hdu.edu.cn/showproblem.php? pid=3572 题意:m台机器.须要做n个任务. 第i个任务.你须要使用机器Pi天,且这个任务要在[Si  ,  Ei]区间内完毕才有效. 对于一个任务,仅仅能由一个机器来完毕.一个机器同一时间仅仅能做一个任务. 当然,一个任务能够分成几段不连续的时间来完毕.问,是否能做完所有任务. 题意非常清晰.也就是推断是否是满流. 对于网络流问题,模板大家都有,关键在于怎样建图(详见资料) 思路:今天问了龙哥,对建图

hdu 3572 Task Schedule 网络流

题目链接 有n个任务, m个机器, 每个任务有三个值, s,e, p, 表示, 这个任务必须在s天之后才能开始, 必须在e天之前结束, 并且必须要执行p天, 中间可以中断. 每个机器每天只能执行一个任务, 每个任务也只能被一个机器执行. 源点s和每个任务建边, 权值为p, 每个任务和s-e之间的这些天建边, 权值为1, 每一天和汇点建边, 权值为m. 模板题感觉没什么好说的啊...... 1 #include<bits/stdc++.h> 2 using namespace std; 3 #d