hdu 3572 Task Schedule SAP模板+完美建图

Task Schedule

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


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
 

Recommend
zhouzeyong





这个题目的意思是一个人要完成任务,有n件工作,有M个机器,要完成工作,每个工作有起始天,终结天,还有工作多久可以完成,问能否可以完成,可以的就输入yes,否则输出no

下面是建成的图




#include<stdio.h>
#include<cstring>
#include<iostream>
using namespace std;
#define  N 1010
#define  M 500040
#define  inf 0x3f3f3f3f
int idx;
int cur[N],pre[N],dis[N],gap[N],aug[N],head[N];

struct node
{
    int u,v,w,next;

}unit[M];
void add(int u,int v,int w)
{
    unit[idx].u=u;
    unit[idx].v=v;
    unit[idx].w=w;
    unit[idx].next=head[u];
    head[u]=idx++;
    unit[idx].u = v;
    unit[idx].v = u;
    unit[idx].w = 0;
    unit[idx].next = head[v];
    head[v] = idx++;
}
int SAP(int s,int e,int n)
{



    int max=0;
    int v,u=s;
    int id,mind;
    aug[s]=inf;
    pre[s]=-1;
    memset(dis, 0, sizeof(dis));
    memset(gap, 0, sizeof(gap));
    gap[0]=n;
    int i,j;
    for(i=0;i<=n;i++)
    {
        cur[i]=head[i];
    }
    while(dis[s]<n)
    {

         int flag=-1;
         if (u == e)
        {
            max += aug[e];
            for (v = pre[e]; v != -1; v = pre[v])
            {
                id = cur[v];
                unit[id].w -= aug[e];
                unit[id^1].w += aug[e];
                aug[v] -= aug[e];
                if (unit[id].w == 0)
                u = v;
            }
        }
         for (id = cur[u]; id != -1; id = unit[id].next)
        {
            v = unit[id].v;
            if (unit[id].w > 0 && dis[u] == dis[v] + 1)
            {
                flag = 1;
                pre[v] = u;
                cur[u] = id;
                aug[v] = min(aug[u], unit[id].w);
                u = v;
                break;
            }
        }
        if (flag == -1)
        {
            if (--gap[dis[u]] == 0) break; /* gap优化,层次树出现断层则结束算法 */
            mind = n;
            cur[u] = head[u];
            for (id = head[u]; id != -1; id = unit[id].next)
            {
                v = unit[id].v;
                if (unit[id].w > 0 && dis[v] < mind)
                {
                    mind = dis[v];
                    cur[u] = id; // 修改标号的同时修改当前弧
                }
            }
            dis[u] = mind + 1;
            gap[dis[u]]++;
            if (u != s) u = pre[u]; // 回溯继续寻找允许弧
        }
    }
    return max;

}




int main()
{
    int T, n, m, p, s, e;
    int Max, sum, source, sink, vn;
    int i,j;
    scanf("%d", &T);
    for(int h=1;h<=T;h++)
    {
        idx=0;
        memset(head, -1, sizeof(head));
        sum = 0;
        source = 0;
        Max = 0;
        scanf("%d%d", &n,&m);
        for(i=1;i<=n;i++)
        {
            scanf("%d%d%d",&p,&s,&e);
            sum=sum+p;
            Max=max(Max,e);
            add(source,i,p);
            for (j = s; j <= e; j++)
            {
                add(i,n+j,1);
            }

        }
        sink=n+Max+1;
        vn=sink+1;
         for (i=1;i<=Max;++i)
        {
            add(n+i,sink,m);
        }
        if (SAP(source, sink, vn) == sum)
        printf("Case %d: Yes\n\n", h);
        else
        printf("Case %d: No\n\n", h);




    }
    return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值