HDU3572——Task Schedule (最大流判断满流)


Task Schedule

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


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


题目大意:工厂有m台机器,每个机器同时只能完成一项任务,给了n个任务,每个任务有一个规定的时间,必须在这段时间内完成,且花费一定的天数,经过建图以后这是一个最大流问题,首先建立一个源点,将所有任务与源点连边,流量是每个任务持续的天数,将第i天与汇点连边,流量是m,之后将任务与其起始区间内所有天数连边,流量是1,跑最大流判断一下是否满流即可。

代码:
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
const int maxn=1010;
const int INF=0x7fffffff;
struct Edge
{
    int to,from,cap,flow;
    Edge(int u,int v,int c,int f):from(u),to(v),cap(c),flow(f){}
};

struct Edmonds_karp
{
    int n,m,s,t;
    vector<Edge> edges;
    vector<int> G[maxn];
    bool vis[maxn];
    int cur[maxn];
    int d[maxn];

    void init(int n,int s,int t)
    {
        this->n=n, this->s=s, this->t=t;
        edges.clear();
        for(int i=0;i<n;++i) G[i].clear();
    }

    void add_edge(int from,int to,int cap)
    {
        edges.push_back( Edge(from,to,cap,0) );
        edges.push_back( Edge(to,from,0,0) );
        m=edges.size();
        G[from].push_back(m-2);
        G[to].push_back(m-1);
    }

    bool BFS()
    {
        queue<int> Q;
        memset(vis,0,sizeof(vis));
        vis[s]=true;
        d[s]=0;
        Q.push(s);
        while(!Q.empty())
        {
            int x=Q.front(); Q.pop();
            for(int i=0;i<G[x].size();++i)
            {
                Edge& e=edges[G[x][i]];
                if(!vis[e.to] && e.cap>e.flow)
                {
                    vis[e.to]=true;
                    d[e.to]=d[x]+1;
                    Q.push(e.to);
                }
            }
        }
        return vis[t];
    }

    int DFS(int x,int a)
    {
        if(x==t || a==0) return a;
        int flow=0, f;
        for(int &i=cur[x];i<G[x].size();++i)
        {
            Edge &e=edges[G[x][i]];
            if(d[e.to]==d[x]+1 && (f=DFS(e.to,min(a,e.cap-e.flow) ) )>0)
            {
                e.flow +=f;
                edges[G[x][i]^1].flow -=f;
                flow +=f;
                a -=f;
                if(a==0) break;
            }
        }
        return flow;
    }

    int max_flow()
    {
        int ans=0;
        while(BFS())
        {
            memset(cur,0,sizeof(cur));
            ans +=DFS(s,INF);
        }
        return ans;
    }
}a;
int l[maxn],r[maxn],d[maxn];
int vis[maxn];
int main()
{
    ios::sync_with_stdio(false);
    int t;
    //cin>>t;
    scanf("%d",&t);
    for(int k=1;k<=t;k++)
    {
        int n,m;
        scanf("%d%d",&n,&m);
        a.init(1002,0,1001);
        //a.n=maxn-1;
        a.s=0,a.t=1001;
        memset(vis,0,sizeof(vis));
        int full_flow=0;
        for(int i=0;i<n;i++){
            //cin>>d[i]>>l[i]>>r[i];
            scanf("%d%d%d",&d[i],&l[i],&r[i]);
            full_flow+=d[i];
        }
        for(int i=0;i<n;i++)
        {
            for(int j=l[i];j<=r[i];j++){
                a.add_edge(501+i,j,1);
                vis[j]=true;
            }
        }
        for(int i=1;i<=500;i++)
        {
            if(vis[i])
                a.add_edge(i,1001,m);
        }
        for(int i=0;i<n;i++)
            a.add_edge(0,501+i,d[i]);
        int ans=a.max_flow();
        //cout<<ans<<endl;
        printf("Case %d: ",k);
        if(ans==full_flow)
            puts("Yes");
        else
            puts("No");
        puts("");
    }
    //cout << "Hello world!" << endl;
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值