hdu 3572 Task Schedule【最大流Dinic-----建图】

本文探讨了如何通过构建特定图模型并运用Dinic算法解决任务调度问题,确保在有限的机器资源下能够按时完成所有任务。

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

Task Schedule

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


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
 

题目大意:


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

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


思路:


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

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

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


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



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[100005];
int divv[100005];
int head[100005];
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;
    int kase=0;
    scanf("%d",&t);
    while(t--)
    {
        cont=0;
        memset(head,-1,sizeof(head));
        scanf("%d%d",&n,&m);
        ss=0;
        tt=100000;
        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();
        printf("Case %d: ",++kase);
        if(tmp>=sum)
        {
            printf("Yes\n");
        }
        else printf("No\n");
        printf("\n");
    }
}










评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值