HLG 1061 Boss Xnby’s Scheduling Problem【SAP】

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 p(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!


分析:

我们 可以构建一个二部图,每个任务一个节点,每天一个节点,任务和天数满足上述条件就连一条边,容量为  1。再引入一个源点和汇点,从源点向每个任务节点连一条边,容量为该任务的处理时间p,再引入一个汇点,从每个天数节点连一条边至汇点,容量为M,表示从当天的可以使用的机器数。
 
View Code
#include<stdio.h>
#include<string.h>
#define INF 0x1f1f1f
int n,m,s,u,e;
int c[300][300],r[300][300];
int gap[300];
int dis[300];
void init()
{
    int v,x,q[300],front=0,rear=0;
    memset(gap,0,sizeof(gap));
    memset(dis,0xff,sizeof(dis));
    q[rear++]=u;
    dis[u]=0;
    while(front<rear)
    {
        x=q[front++];
        gap[dis[x]]++;
        for(v=0;v<=e;v++)
            if(dis[v]==-1&&c[v][x]>0)
            {
                dis[v]=dis[x]+1;
                q[rear++]=v;
            }
    }
}
int max_flow()
{
    init(); 
    int flow=0,top=s,i,j,k,pre[300],low[300];
    memset(low,0,sizeof(low));
    while(dis[s]<=e)
    {
        int flag=0;
        low[s]=INF;
        for(i=0;i<=e;i++)
        {
            if(c[top][i]>0&&dis[top]==dis[i]+1&&dis[i]>=0)
            {
                flag=1;
                break;
            }
        }
        if(flag)
        {
            low[i]=c[top][i];
            if(low[i]>low[top])
                low[i]=low[top];
    
            pre[i]=top;
            top=i;
            if(top==u)
            {
                flow+=low[u];
                j=top;
                while(j!=s)
                {
                    k=pre[j];
                    c[k][j]-=low[u];
                    c[j][k]+=low[u];
                    j=k;
                }
                top=s;
                memset(low,0,sizeof(low));
            }
        }
        else 
        {
            int min=e;
            for(j=0;j<=e;j++)
            {
                if(c[top][j]>0&&dis[j]+1<min&&dis[j]>=0)
                    min=dis[j]+1;
            }
            gap[dis[top]]--;
            if(gap[dis[top]]==0)
                break;
            gap[min]++;
            dis[top]=min;
            if(top!=s)
                top=pre[top];
        }
    }
    return flow;
}
int main()
{
    //freopen("D:ce.txt","r",stdin);
    int st,en,la,tot,i,j,ca;
    scanf("%d",&ca);
    while(ca--)
    {
        scanf("%d%d",&n,&m);
        tot=0;
        memset(c,0,sizeof(c));
        s=0;e=0; u=n+1;
        for(i=1;i<=n;i++)
        {
            scanf("%d%d%d",&la,&st,&en);
            c[s][i]=la;
            tot+=la;
            if(la<=en-st)
            for(j=st;j<en;j++)
            {
                c[i][n+j+1]=1;
                c[n+j+1][n+1]=m;
            }
            if(n+en>e)
                e=en+n;
        }
        if(max_flow()==tot)
            printf("Boss xnby is happy!\n");
        else printf("Boss xnby is angry!\n");
    }
    return 0;
}

 

posted @ 2012-04-11 20:44  'wind  阅读(2749)  评论(0编辑  收藏  举报