• 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
    ",ans);
            printf("Case %d: ",++Case);
            if(ans==sump) puts("Yes");
            else puts("No");
            printf("
    ");
        }
        return 0;
    }

      

      

     
  • 相关阅读:
    RPC细谈
    RPC浅谈
    动态规划
    libco 的定时器实现: 时间轮
    一次HTTP请求的完整过程——协议篇(DNS、TCP、HTTP)
    多个CPU、多核CPU以及超线程(Hyper-Threading)
    Linux下which、whereis、locate、find命令的区别
    warning C4819: 该文件包含不能在当前代码页(936)中表示的字符。请将该文件保存为 Unicode 格式以防止数据丢失
    使用OutputDebugString输出调试信息
    VS或windows用代码产生GUID
  • 原文地址:https://www.cnblogs.com/MeowMeowMeow/p/7512457.html
Copyright © 2020-2023  润新知