HDU3572 Task Schedule 【最大流】

Task Schedule

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 4003    Accepted Submission(s): 1347

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

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

题意:有n个机器,m项任务,每个任务需要Pi天时间,开工日期到收工日期为Si到Ei,一次只能在一台机器上加工,可以挪到别的机器上,问能否按期完成所有任务。

题解:这题关键在构图,设置一个源点到每项任务有一条边,容量为该项任务所需要的天数,每项任务到合法加工日期内的每个天数加一条边,容量为1,即每天工作量为1,然后每个天数到汇点添加一条边,容量为机器数量n,表示一天最大加工量。可惜的是代码超时了-_-#

#include <stdio.h>
#include <string.h>

#define maxn 710
#define maxm 700000
#define inf 0x3f3f3f3f

int head[maxn], n, m, id; // n machines
struct Node {
    int u, v, c, next;
} E[maxm];
int source, sink, tar, maxDay, nv;
int que[maxn], Layer[maxn], pre[maxn];
bool vis[maxn];

void addEdge(int u, int v, int c) {
    E[id].u = u; E[id].v = v;
    E[id].c = c; E[id].next = head[u];
    head[u] = id++;

    E[id].u = v; E[id].v = u;
    E[id].c = 0; E[id].next = head[v];
    head[v] = id++;
}

void getMap() {
    int i, j, u, v, p, s, e;
    id = tar = maxDay = 0;
    scanf("%d%d", &m, &n);
    memset(head, -1, sizeof(head));
    source = 0; sink = 705;
    for(i = 1; i <= m; ++i) {
        scanf("%d%d%d", &p, &s, &e);
        tar += p;
        if(e > maxDay) maxDay = e;
        addEdge(source, i, p);
        for(j = s; j <= e; ++j)
            addEdge(i, m + j, 1);
    }
    sink = m + maxDay + 1; nv = sink + 1;
    for(i = 1; i <= maxDay; ++i)
        addEdge(m + i, sink, n);
}

bool countLayer() {
    memset(Layer, 0, sizeof(int) * nv);
    int id = 0, front = 0, u, v, i;
    Layer[source] = 1; que[id++] = source;
    while(front != id) {
        u = que[front++];
        for(i = head[u]; i != -1; i = E[i].next) {
            v = E[i].v;
            if(E[i].c && !Layer[v]) {
                Layer[v] = Layer[u] + 1;
                if(v == sink) return true;
                else que[id++] = v;
            }
        }
    }
    return false;
}

int Dinic() {
    int i, u, v, minCut, maxFlow = 0, pos, id = 0;
    while(countLayer()) {
        memset(vis, 0, sizeof(bool) * nv);
        memset(pre, -1, sizeof(int) * nv);
        que[id++] = source; vis[source] = 1;
        while(id) {
            u = que[id - 1];
            if(u == sink) {
                minCut = inf;
                for(i = pre[sink]; i != -1; i = pre[E[i].u])
                    if(minCut > E[i].c) {
                        minCut = E[i].c; pos = E[i].u;
                    }
                maxFlow += minCut;
                for(i = pre[sink]; i != -1; i = pre[E[i].u]) {
                    E[i].c -= minCut;
                    E[i^1].c += minCut;
                }
                while(que[id-1] != pos)
                    vis[que[--id]] = 0;
            } else {
                for(i = head[u]; i != -1; i = E[i].next)
                    if(E[i].c && Layer[u] + 1 == Layer[v = E[i].v] && !vis[v]) {
                        vis[v] = 1; que[id++] = v; pre[v] = i; break;
                    }
                if(i == -1) --id;
            }
        }
    }
    return maxFlow;
}

void solve(int cas) {
    printf("Case %d: %s\n\n", cas, tar == Dinic() ? "Yes" : "No");
}

int main() {
    // freopen("stdin.txt", "r", stdin);
    int t, cas;
    scanf("%d", &t);
    for(cas = 1; cas <= t; ++cas) {
        getMap();
        solve(cas);
    }
    return 0;
}
时间: 2024-10-24 16:13:24

HDU3572 Task Schedule 【最大流】的相关文章

hdu 3572 Task Schedule 最大流 Dinic算法,,卡时间。。建图非常有讲究

Task Schedule Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 4617    Accepted Submission(s): 1513 Problem Description Our geometry princess XMM has stoped her study in computational geometry t

HDU 3572 Task Schedule (最大流)

C - Task Schedule Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 3572 Description Our geometry princess XMM has stoped her study in computational geometry to concentrate on her newly opened fac

HDU3572:Task Schedule【最大流】

上了一天课 心塞塞的 果然像刘老师那么说 如果你有挂科+4级没过 那基本上是WF队 题目大意:有时间补吧 思路:给每个任务向每个时间点连边容量为1 每个时间点向汇点连边 容量为机器的个数 源点向每个任务连边 容量为该任务所需时间 最后看是否满流 #include <stdio.h> #include <iostream> #include<queue> #include <string.h> #include <algorithm> #defin

HDU3572 Task Schedule(ISAP)

学了几天的网络流,感觉还是ISAP算法比较实用,用这道题整理了一下,可以当作模版 题意:给出n个任务+m台机器,还有一个任务处理时限+开始时间+结束时间,一个时刻里一台机器只能处理一个任务,但是一个任务可以在不同机器处理,问能否处理完所有任务? 方法:最大流.这个题的建图算是经典,因为限定每个时刻每台机器只能处理一个任务,所以可以把时间点分配给各个合法的机器...具体是先设定一个超 级源点S(我设为0这个点),连向各个任务,容量为该任务所需时间,各个任务连向在范围内的时间点,容量为1,所有时间点

hdu-3572 Task Schedule---最大流判断满流+dinic算法

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3572 题目大意: 给N个任务,M台机器.每个任务有最早才能开始做的时间S,deadline E,和持续工作的时间P.每个任务可以分段进行,但是在同一时刻,一台机器最多只能执行一个任务. 问存不存在可行的工作时间. 解题思路: 由于时间<=500且每个任务都能断断续续的执行,那么我们把每一天时间作为一个节点来用网络流解决该题. 建图: 源点s(编号0), 时间1-500天编号为1到500, N个任务

Task Schedule(Hdu3572网络流)

跟着风神学图论 Task Schedule(Hdu3572网络流) 题意: 有一个工厂最近新近了一批机器人,这些机器人被用来解决若干个任务.每个任务都有完成所需的时间pi和规定了必须在si天或者si天之后才能处理这个任务,且需要在ei天之内完成. 工厂规定每个机器人每天只能处理一个任务,且每个任务最多只能由一个机器处理. 问这些机器人能否在顺利完成这些任务. 题解: 网络流建模. 对于每一个任务来说,它必须要在si到ei天之间被处理. 并且要在pi天以内完成. 所以我们可以把每一天当成点来考虑,

HDU 3572 Task Schedule(拆点+最大流dinic)

Task Schedule Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 7753    Accepted Submission(s): 2381 Problem Description Our geometry princess XMM has stoped her study in computational geometry t

hdu 3572 Task Schedule(最大流)

hdu 3572 Task Schedule 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 tas

hdoj 3572 Task Schedule【最大流】

题目:hdoj 3572 Task Schedule 题意:有m台机器和n个任务,然后给出每个任务的开始时间和结束时间以及需要的天数,让你判断有没有这样条件的安排 分析:网络流题目,比较难想到的是把时间区间怎么在图里面建,其实是在这个区间的每个点都连一条边,建图方案. 超级源点s到每个任务 i 连边,容量为第 i 个任务需要的天数,然后每个任务向满足要求的日期连一条容量为1的边,即从开始到结束都连,然后所有日期到汇点连容量m的边,因为每个机器最多同时能够做m个task. 这样建图发现图中的点最多