hrbust 1061 Boss Xnby’s Scheduling Problem【Dinic最大流+建图+当前弧优化】

本文探讨了BossXnby的公司中多个任务在有限数量的并行机器上的调度问题,通过建立网络流模型,利用Dinic算法求解最大流,判断是否能在截止日期前完成所有任务。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Boss Xnby’s Scheduling Problem
Time Limit: 2000 MSMemory Limit: 65536 K
Total Submit: 109(25 users)Total Accepted: 31(21 users)Rating: Special Judge: No
Description

Boss xnby owns a company, as you know, in the company there are lots of jobs to do.Each job J has a processing requirement pj (denoting the number of machine days required to complete the job), a release date rj (representing the beginning of the day when job j becomes avalible for processing), and a due date dj ≥ rj  + pj (representing the beginning of the day by which the job must be completed). What could we do when facing so many jobs? Thank goodness, in xnby’s company, there are some parallel machines can do these jobs.Since a machine can work on only one job at a time and each job can be processed by at most one machine at a time.And preemptions(i.e., we can interrupt a job and process it on different machines on different days) is allowed.Now xnby can use these parallel machines to process these boring jobs,but he also need to determine a feasible schedule that completes all jobs before their due dates or show no such schedule existd.Xnby is a boss, he is a big shot, he had no time to do with these trivial things, so he arranged for you to do this task.

Input

An integer T (T ≤ 100) indicated the number of test cases. 

For each test cases:

Two integers J and M (J ≤ 100, M ≤ 100) denote jobs and machines respectively.

In the following J lines(each job one line, in ascending order), each line contains three integers p, r, d (p ≤ 100,r ≤ 100 and d ≤ 200) denote processing requirement, release date and due date respectively.

Output

For each test case, output “Boss xnby is angry!” if no such schedule exists.Otherwise output “Boss xnby is happy!”.

Sample Input
1
4 3
2 3 5
1 1 4
2 3 7
4 5 9
Sample Output

Boss xnby is happy!

Hint

One schedule: Job 1 must be done in day 3 and day 4, and job 2 can be done in day 1,job 3 can be done in day 5 or day 6,the last job’s first two days can be done in day 5 and day 6(since there 3 machines, so it will not conflict with job 3), the remaining 2 days can be done in day 7 and day 8.

Author
monster@monster

题目大意:


有t组数据,接下来一行两个元素,分别表示任务总数和机器数,每天每台机器只能对一台任务进行工作,并且一个工作也只能使用一台机器。

接下来那些行表示任务完成的总天数,起始天数和截止日期,问能否完成所有任务。


思路:


1、建立源点S,各个任务作为一个节点,从S连入,其边权值为任务完成需要的总天数,表示做这个任务需要这么些天。

然后建立每天为一个节点,每个任务节点连接对应期限天数,边权值都为1,表示如果当前任务选择了这一天去做,而且每个任务每天只能用一台机器,所以边权为1.

然后从每个天数节点汇到T,边权值为m,表示每天一共可以用m台机器。


2、跑一遍Dinic即可、如果最大流==所有任务的总天数和,那么就是能够完成所有任务。


3、裸跑Dinic过不掉,会TLE ,需要加上当前弧优化才能过。


Ac代码:


#include<stdio.h>
#include<string.h>
#include<queue>
#include<iostream>
using namespace std;
#define INF 0x3f3f3f3f
struct node
{
    int from;
    int to;
    int w;
    int next;
}e[10000000];
int cur[10000];
int divv[10000];
int head[10000];
int n,m,ss,tt,cont;
void add(int from,int to,int w)
{
    e[cont].to=to;
    e[cont].w=w;
    e[cont].next=head[from];
    head[from]=cont++;
}
int makedivv()
{
    memset(divv,0,sizeof(divv));
    divv[ss]=1;
    queue<int >s;
    s.push(ss);
    while(!s.empty())
    {
        int u=s.front();
        if(u==tt)return 1;
        s.pop();
        for(int i=head[u];i!=-1;i=e[i].next)
        {
            int v=e[i].to;
            int w=e[i].w;
            if(w&&divv[v]==0)
            {
                divv[v]=divv[u]+1;
                s.push(v);
            }
        }
    }
    return 0;
}
int Dfs(int u,int maxflow,int tt)
{
    if(tt==u)return maxflow;
    int ret=0;
    for(int &i=cur[u];i!=-1;i=e[i].next)
    {
        int v=e[i].to;
        int w=e[i].w;
        if(w&&divv[v]==divv[u]+1)
        {
            int f=Dfs(v,min(maxflow-ret,w),tt);
            e[i].w-=f;
            e[i^1].w+=f;
            ret+=f;
            if(ret==maxflow)return ret;
        }
    }
    return ret;
}
int Dinic()
{
    int ans=0;
    while(makedivv()==1)
    {
        memcpy(cur,head,sizeof(head));
        ans+=Dfs(ss,INF,tt);
    }
    return ans;
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        cont=0;
        memset(head,-1,sizeof(head));
        scanf("%d%d",&n,&m);
        ss=0;
        tt=1000;
        int sum=0;
        int minn=INF;
        int maxn=-INF;
        for(int i=1;i<=n;i++)
        {
            int p,b,e;
            scanf("%d%d%d",&p,&b,&e);
            sum+=p;
            add(ss,i,p);
            add(i,ss,0);
            minn=min(minn,b);
            maxn=max(maxn,e);
            for(int j=b;j<e;j++)
            {
                add(i,j+n,1);
                add(j+n,i,0);
            }
        }
        for(int i=minn;i<=maxn;i++)
        {
            add(i+n,tt,m);
            add(tt,i+n,0);
        }
        int tmp=Dinic();
        if(tmp>=sum)
        {
            printf("Boss xnby is happy!\n");
        }
        else printf("Boss xnby is angry!\n");
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值