hdu 3572 Task Schedule(最大流)

Task Schedule

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


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
题意:有n个任务和m个任务,每个任务在同一个时间只能由一台机器执行,一台机器同一个时间只能执行一个任务,任务可以断开执行

现在给你每个任务的最早开始时间,任务所需时间和任务最晚完成时间,问你所有任务是否都可以完成

思路: 用0做源点,连向所有任务,容量为任务所需的时间(因为我们最后需要判断是否所有所需的时间都能流向终点),然后把每个任务跟其对应的时间段内的时间连一条线,权值为1,再把所有时间跟终点连一条线,权值为m即可

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
using namespace std;
#define N 1050
#define INF 99999999
struct Edge
{
    int v,next,cap;
} edge[N*N];
int cnt,head[N],d[N];
void init()
{
    memset(head,-1,sizeof(head));
    cnt=0;
}
void addedge(int u,int v,int cap)
{
    edge[cnt].v=v,edge[cnt].cap=cap;
    edge[cnt].next=head[u],head[u]=cnt++;
    edge[cnt].v=u,edge[cnt].cap=0;
    edge[cnt].next=head[v],head[v]=cnt++;
}
int bfs(int s,int t)
{
    memset(d,-1,sizeof(d));
    d[s]=0;
    queue<int>q;
    q.push(s);
    while(!q.empty())
    {
        int u=q.front();
        q.pop();
        for(int i=head[u]; i!=-1; i=edge[i].next)
        {
            int v=edge[i].v,cap=edge[i].cap;
            if(d[v]==-1&&cap>0)
            {
                d[v]=d[u]+1;
                q.push(v);
            }
        }
    }
    return d[t]!=-1;
}
int dfs(int s,int t,int f)
{
    if(s==t||f==0) return f;
    int flow=0;
    for(int i=head[s]; i!=-1; i=edge[i].next)
    {
        int v=edge[i].v,cap=edge[i].cap;
        if(d[v]==d[s]+1&&cap>0)
        {
            int x=min(f-flow,cap);
            x=dfs(v,t,x);
            flow+=x;
            edge[i].cap-=x;
            edge[i^1].cap+=x;
        }
    }
    if(!flow) d[s]=-2;
    return flow;
}
int Dinic(int s,int t)
{
    int flow=0,f;
    while(bfs(s,t))
    {
        while(f=dfs(s,t,INF))
            flow+=f;
    }
    return flow;
}
int main()
{
    int T,n,m,tot=1;
    int b,p,e;
    scanf("%d",&T);
    while(T--)
    {
        init();
        scanf("%d %d",&n,&m);
        int s=0,t=1002;
        int sum=0;
        for(int i=1; i<=n; i++)
        {
            scanf("%d %d %d",&p,&b,&e);
            sum+=p;
            addedge(s,i,p);
            for(int j=n+b; j<=n+e; j++)
                addedge(i,j,1);
        }
        for(int i=1+n; i<=n+500; i++)
            addedge(i,t,m);
        if(Dinic(s,t)==sum) printf("Case %d: Yes\n\n",tot++);
        else printf("Case %d: No\n\n",tot++);
    }
    return 0;
}






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值