HDU 3572 Task Schedule

Task Schedule

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


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

题解:
把每个任务和每个时刻都作为节点,增设一个源点和一个汇点。对于任务i,连接一条源点指向 i 最
大容量为pi 的边,然后连接 i 到[ si , ei ]区间里每个时刻,最大容量为1,最后连接每个时刻到
汇点,最大容量为m(因为同一时刻最多只能有m台机器在操作)。此时,完成构图。
用网络流算法求解到最大流之后,判断是不是满流(每个任务都有天数的限制,不能少也不能多)。
代码:

#include<bits/stdc++.h>
using namespace std;
const int maxn=505;
int n,m,x=0,y=1500;
int level[maxn*3],iter[maxn*3];
struct node
{
    int to,cap,rev;
};
vector<node>G[maxn*3];
void add_edge(int from,int to,int cap)
{
    G[from].push_back((node){to,cap,G[to].size()});
    G[to].push_back((node){from,0,G[from].size()-1});
}
void add(int t,int st,int to)
{
    add_edge(x,st,t);
    for(int i=st;i<=to;i++)add_edge(st,500+i,1);
}
void bfs()
{
    memset(level,-1,sizeof(level));
    level[x]=0;
    queue<int>P;P.push(x);
    while(!P.empty())
    {
        int v=P.front();P.pop();
        for(int i=0;i<G[v].size();i++)
        {
            node e=G[v][i];
            if(e.cap>0&&level[e.to]<0)
            {
                level[e.to]=level[v]+1;
                P.push(e.to);
            }

        }
    }
}
int dfs(int v,int t,int f)
{
    if(v==t)return f;
    for(int &i=iter[v];i<G[v].size();i++)
    {
        node &e=G[v][i];
        if(e.cap>0&&level[e.to]>level[v])
        {
            int d=dfs(e.to,t,min(f,e.cap));
            if(d>0)
            {
                e.cap-=d;
                G[e.to][e.rev].cap+=d;
                return d;
            }
        }
    }
    return 0;
}
int max_flow()
{
    int flow=0;
    while(1)
    {
        bfs();
        if(level[y]<0)return flow;
        memset(iter,0,sizeof(iter));
        int f;
        while((f=dfs(x,y,2000000000))>0)flow+=f;
    }
}
int main()
{
    int T,cas=0;scanf("%d",&T);
    while(T--)
    {
        for(int i=0;i<maxn*3;i++)G[i].clear();
        scanf("%d%d",&n,&m);
        int sum=0;
        for(int i=1;i<=n;i++)
        {
            int t,st,to;scanf("%d%d%d",&t,&st,&to);
            add(t,st,to);
            sum+=t;
        }
        for(int i=501;i<=1001;i++)add_edge(i,y,m);
        if(max_flow()==sum)printf("Case %d: Yes\n",++cas);
        else printf("Case %d: No\n",++cas);
        printf("\n");
    }
    return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值