hdu3572 Task Schedule(基础) [最大流]任务分配,判断满流

Task Schedule

Time Limit: 1000 MS Memory Limit: 32768 KB

64-bit integer IO format: %I64d , %I64u Java class name: Main

[Submit] [Status] [Discuss]

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

Source

2010 ACM-ICPC Multi-University Training Contest(13)——Host by UESTC


题意:你有m台机器,n个任务。每个任务都有开始时间和截止时间,完成它的时间(可中断)。每个任务每天最多用一台机器(即不可多台机器在同一天做同一个任务),问你能否按时完成所有任务。

思路:源点连机器(权值inf),机器连上每一天(权值1),在对某任务的开始时间到截止时间的每一天都连上此任务(权值1),任务连汇点(权值完成所需时间)。最后判断最大流是否为所有任务完成时间之和即可。

#include
#include
#include
#include
#include
using namespace std;
const int inf = 2147483647;
const int MAXN = 2000;

struct edge{
   int cap,to,rev;
   edge(int a = 0,int b = 0,int c = 0):to(a),cap(b),rev(c){}
};
vector G[MAXN];
int level[MAXN];
int iter[MAXN];

void init()
{
    for(int i = 0;i < MAXN;i++){
        G[i].clear();
    }
}

void Add(int f,int t,int c)
{
    G[f].push_back(edge{t,c,G[t].size()}  );
    G[t].push_back(edge{f,0,G[f].size()-1});
}

void bfs(int s)
{
    queueque;
    memset(level,-1,sizeof(level));
    level[s] = 0;
    que.push(s);
    while(!que.empty()){
        int v = que.front();
        que.pop();
        for(int i = 0;i < G[v].size();i++){
            edge &e = G[v][i];
            if(e.cap > 0 && level[e.to] == -1){
                level[e.to] = level[v]+1;
                que.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++){
        edge &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 maxd(int s,int t)
{
    int flow = 0;
    while(true){
        bfs(s);
        if(level[t] == -1) return flow;
        memset(iter,0,sizeof(iter));
        int f;
        while((f = dfs(s,t,inf))>0 ){
            flow += f;
        }
    }
}

int main()
{
    int t;
    int Case = 1;
    while(scanf("%d",&t) != EOF){
        while(t--){
            init();
            int ren,ji;
            int yuan,hui;
            scanf("%d%d",&ren,&ji);
            yuan = 0;
            hui = ren+ji+500+1;
            for(int i = 1;i <= ji;i++){
                Add(0,i,inf);
                for(int j = ji+1;j <= ji+500;j++){
                    Add(i,j,1);
                }
            }
            int sum = 0;
            for(int i = 1;i <= ren;i++){
                int a,b,c;
                scanf("%d%d%d",&a,&b,&c);
                for(int j = ji+b;j <= ji+c;j++){
                    Add(j,ji+500+i,1);
                }
                sum += a;
                Add(ji+500+i,hui,a);
            }
            int ans = maxd(0,hui);
            if(ans == sum){
                printf("Case %d: Yes\n\n",Case++);
            }
            else{
                printf("Case %d: No\n\n",Case++);
            }
        }
    }
    return 0;
}


你可能感兴趣的:(网络流,题解)