Task Schedule HDU - 3572

C - Task Schedule HDU - 3572
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

首先可以明确的是,每一天最多有m台机器同时工作,对不同的任务而言,可以取同一天进行工作,但是对于这一天不能超过m台机器同时工作,对于某个任务,有对应的[s,e]区间可以取工作,那么这个任务必定要在这些天内做到p天,因为日期最大为500天,因此可以将每一天看作一个结点,对于某个任务而言,如果取第i天去工作,那么该任务结点向这一天的结点流入1,由于于是,对于任务结点而言,它要流出的是p才能达到要求,可以用一个超级源点先向它流入p,而对于某一天而言,最大流出为m,将每一天的流出到一个超级汇点,那么从超级源点到超级汇点的最大流达到了所需求的就是Yes,否则No.

#include <iostream>
#include <queue>
#include <vector>
#include <cstring>
#include <cstdio>
using namespace std;
#define INF 0x3f3f3f3f
struct edge{
    int to,f,rev;
};
vector<edge> G[1005];
int cur[1005],d[1005],pi[505],si[505],ei[505],s,t,sum,k;
void addedge(int x,int y,int z){
    G[x].push_back(edge{y,z,(int)G[y].size()});
    G[y].push_back(edge{x,0,(int)G[x].size() - 1});
}
void bfs(){
    memset(d,-1,sizeof(d));
    d[0] = 0;
    queue<int> q;
    q.push(0);
    while(!q.empty()){
        int u = q.front();
        q.pop();
        int si = G[u].size();
        for(int i = 0;i < si;++i){
            edge &t = G[u][i];
            if(t.f && d[t.to] == -1){
                q.push(t.to);
                d[t.to] = d[u] + 1;
            }
        }
    }
}
int dfs(int u,int flow){
    if(u == t){
        return flow;
    }
    int si = G[u].size();
    for(int &i = cur[u];i < si;++i){
        edge &t = G[u][i];
        if(t.f && d[t.to] > d[u]){
            int f = dfs(t.to,min(t.f,flow));
            if(f > 0){
                t.f -= f;
                G[t.to][t.rev].f += f;
                return f;
            }
        }
    }
    return 0;
}
void maxflow(){
    int flow = 0;
    while(1){
        bfs();
        if(d[t] == -1){
            break;
        }
        memset(cur,0,sizeof(cur));
        int f;
        while((f = dfs(s,INF)) > 0){
            flow += f;
        }
    }
    cout << "Case " << ++k <<": ";
    if(flow == sum){
        cout << "Yes" << endl;
    }
    else{
        cout << "No" << endl;
    }
    putchar('\n');
}
int main()
{
    int T,n,m;
    cin >> T;
    while(T--){
        cin >> n >> m;
        sum = 0;
        for(int i = 1;i <= n;++i){
            scanf("%d%d%d",&pi[i],&si[i],&ei[i]);
            sum += pi[i];
        }
        s = 0,t = 500 + n + 1;
        for(int i = 1;i <= n;++i){
            addedge(s,500 + i,pi[i]);
            for(int j = si[i];j <= ei[i];++j){
                addedge(500 + i,j,1);
            }
        }
        for(int i = 1;i <= 500;++i){
            addedge(i,t,m);
        }
        maxflow();
        for(int i = 0;i <= 500 + n + 1;++i){
            G[i].clear();
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值