HDU3572_Task Schedule(网络流最大流)[通俗易懂]

HDU3572_Task Schedule(网络流最大流)

大家好,又见面了,我是全栈君。

解题报告

题意:

工厂有m台机器,须要做n个任务。对于一个任务i。你须要花费一个机器Pi天,并且,開始做这个任务的时间要>=Si,完毕这个任务的时间<=Ei。

对于一个任务,仅仅能由一个机器来完毕。一个机器同一时间仅仅能做一个任务。

可是,一个任务能够分成几段不连续的时间来完毕。问,是否能做完所有任务。

思路:

网络流在于建模,这题建模方式是:

把每一天和每一个任务看做点。由源点到每一任务。建容量为pi的边(表示任务须要多少天完毕)。

每一个任务每一天,若是能够在这天做任务,建一条容量为1的边。最后。把每天到汇点再建一条边容量m(表示每台机器最多工作m个任务)。

#include <map>
#include <queue>
#include <vector>
#include <cstdio>
#include <cstring>
#include <iostream>
#define inf 99999999
using namespace std;
int n,m,l[2010],head[2010],cnt,M;
struct node
{
    int v,w,next;
} edge[555000];
void add(int u,int v,int w)
{
    edge[M].v=v;
    edge[M].w=w;
    edge[M].next=head[u];
    head[u]=M++;

    edge[M].v=u;
    edge[M].w=0;
    edge[M].next=head[v];
    head[v]=M++;
}
int bfs()
{
    memset(l,-1,sizeof(l));
    l[0]=0;
    int i,u,v;
    queue<int >Q;
    Q.push(0);
    while(!Q.empty())
    {
        u=Q.front();
        Q.pop();
        for(i=head[u]; i!=-1; i=edge[i].next)
        {
            v=edge[i].v;
            if(l[v]==-1&&edge[i].w>0)
            {
                l[v]=l[u]+1;
                Q.push(v);
            }
        }
    }
    if(l[cnt]>0)return 1;
    return 0;
}
int dfs(int u,int f)
{
    int a,i;
    if(u==cnt)return f;
    for(i=head[u]; i!=-1; i=edge[i].next)
    {
        int v=edge[i].v;
        if(l[v]==l[u]+1&&edge[i].w>0&&(a=dfs(v,min(f,edge[i].w))))
        {
            edge[i].w-=a;
            edge[i^1].w+=a;
            return a;
        }
    }
    l[u]=-1;//没加优化会T
    return 0;
}
int main()
{
    int t,i,j,s,p,e,k=1;
    scanf("%d",&t);
    while(t--)
    {
        M=0;
        memset(head,-1,sizeof(head));
        scanf("%d%d",&n,&m);
        int sum=0,maxx=0;
        for(i=1; i<=n; i++)
        {
            scanf("%d%d%d",&p,&s,&e);
            add(0,i,p);
            sum+=p;
            if(maxx<e)
                maxx=e;
            for(j=s; j<=e; j++)
                add(i,j+n,1);
        }
        cnt=n+maxx+1;
        for(i=1; i<=maxx; i++)
        {
            add(n+i,cnt,m);
        }
        int ans=0,a;
        while(bfs())
            while(a=dfs(0,inf))
                ans+=a;
        printf("Case %d: ",k++);
        if(ans==sum)
            printf("Yes\n");
        else printf("No\n");
        printf("\n");
    }
    return 0;
}

Task Schedule

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




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

 


Author
allenlowesy
 


Source

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/116195.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • python数组操作备忘[通俗易懂]

    python数组操作备忘[通俗易懂]现有一段数组signalssignals[2:2,1]取值将为

    2022年8月13日
    6
  • android 定时器

    android 定时器在Android开发中,定时器一般有以下3种实现方法:一、采用Handler与线程的sleep(long)方法二、采用Handler的postDelayed(Runnable,long)方法三、采用Handler与timer及TimerTask结合的方法下面逐一介绍:一、采用Handle与线程的sleep(long)方法Handler主要用来处理接受到的消

    2022年7月25日
    10
  • 另类走遍美国的生活语言:美国黑话英语大观(详解)

    另类走遍美国的生活语言:美国黑话英语大观(详解)文章来源:生活是语言美国的黑人社会中的帮派问题经常地在绕舌歌曲中被讨论,自然也有许多与帮派相关的黑话会常在绕舌歌中出现,如:O.G.-即OriginalGangster,用来指有案底的帮派份子。redrum-就是谋杀(murder)的意思,只是这里把它反过来写。Rolling60s-一黑帮的支派。set-帮派分支。Settripping-帮派内斗。

    2022年5月12日
    78
  • 160亿数据点图表控件LightningChart振动分析可以检测什么?

    160亿数据点图表控件LightningChart振动分析可以检测什么?LightningChart.NET完全由GPU加速,并且性能经过优化,可用于实时显示海量数据-超过10亿个数据点。LightningChart包括广泛的2D,高级3D,Polar,Smith,3D饼/甜甜圈,地理地图和GIS图表以及适用于科学,工程,医学,航空,贸易,能源和其他领域的体绘制功能。点击下载LightningChart.NET最新试用版当您想到振动分析时,您会想到什么?它正在成为结构工程中一种非常常见的识别方法,用于识别潜在的结构完整性问题,例如隐藏的物体或空隙。这是一种识别机械问题的

    2022年10月15日
    4
  • Python 二进制,十进制,十六进制转换「建议收藏」

    Python 二进制,十进制,十六进制转换「建议收藏」十六进制到十进制使用int()函数,第一个参数是字符串’0Xff’,第二个参数是说明,这个字符串是几进制的数。 转化的结果是一个十进制数。>>>int(‘0xf’,16) 15二进制到十进制>>>int(‘10100111110′,2)   1342八进制到十进制>>>int(’17’,8)  15其实可以

    2022年5月19日
    46
  • Java 8 开发的 4 大顶级技巧,你都知道吗 ?

    点击上方“全栈程序员社区”,星标公众号 重磅干货,第一时间送达 来源:https://dzone.com/articles/java-8-top-tips 正文 我使用Java 8…

    2021年6月28日
    78

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号