HDU 3572 【最大流 && 时间区间建图】

Task Schedule

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

Total Submission(s): 5398    Accepted Submission(s): 1742

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

题意:用m个机器,处理n个任务,每个任务必须在[si,ei]时间段完成,需要pi天才能完成。每个机器只能处理一个任务,让你判断有没有这样的安排完成全部的任务

思路:在时间区间建图,其实是在这个区间的每个点都连一条边。

建图方案:超级源点s到每个任务 i 连边,容量为第 i 个任务需要的天数,然后向每个任务的时间区间的所有日期连一条容量为1的边,即从开始到结束都连,然后所有日期到汇点连容量m的边,(因为m台机器同时最多能够做m个任务)。建图过程中记录源点的流量 , 最后再判断是否满流即可。

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue>
#define maxn 10000 + 100
#define maxm 1000000 + 10000
#define INF 0x3f3f3f3f
using namespace std;
int n, m;//n个任务,m个机器

struct node {
    int u, v, cap, flow, next;
};
node edge[maxm];
int dist[maxn], head[maxn], cur[maxn];
bool vis[maxn];
int cnt, sum ,sect;//sect 为超级汇点, sum记录源点出去的流量

void init(){
    cnt = 0;
    memset(head, -1, sizeof(head));
}

void add(int u, int v, int w){
    edge[cnt] = {u, v, w, 0, head[u]};
    head[u] = cnt++;
    edge[cnt] = {v, u, 0, 0, head[v]};
    head[v] = cnt++;
}

void getmap(){
    sum = 0;
    int last = 0;
    for(int i = 1; i <= n; ++i){
        int st, ed, time;
        scanf("%d%d%d", &time, &st, &ed);
        sum += time;
        add(0, i, time);// 0 为超级源点 连接每个任务 容量为完成每个任务的工作时间
        last = max(last, ed);
        for(int j = st; j <= ed; ++j)
            add(i, n + j, 1); // 每个任务连接自己的时间区域,容量为1
    }
    sect = n + last + 1;
    for(int i = 1; i <= sect; ++i)
        add(n + i, sect, m);//连接超级汇点,所有的日期连到超级汇点,容量为m
}

bool BFS(int st, int ed){
    queue<int>q;
    memset(vis, 0 ,sizeof(vis));
    memset(dist, -1, sizeof(dist));
    q.push(st);
    dist[st] = 0;
    vis[st] = 1;
    while(!q.empty()){
        int u = q.front();
        q.pop();
        for(int i = head[u]; i != -1; i = edge[i].next){
            node E = edge[i];
            if(!vis[E.v] && E.cap > E.flow){
                vis[E.v] = 1;
                dist[E.v] = dist[u] + 1;
                if(E.v == ed) return true;
                q.push(E.v);
            }
        }
    }
    return false;
}

int DFS(int x, int ed, int a){
    if(a == 0 || x == ed)
        return a;
    int flow = 0, f;
    for(int &i = cur[x]; i != -1; i = edge[i].next){
        node &E = edge[i];
        if(dist[E.v] == dist[x] + 1 && (f = DFS(E.v, ed, min(a, E.cap - E.flow))) > 0){
            E.flow += f;
            edge[i ^ 1].flow -= f;
            a -= f;
            flow += f;
            if(a == 0) break;
        }
    }
    return flow;
}

int maxflow(int st, int ed){
    int flow = 0;
    while(BFS(st, ed)){
        memcpy(cur, head, sizeof(head));
        flow += DFS(st, ed, INF);
    }
    return flow;
}

int main (){
    int T;
    int k = 1;
    scanf("%d", &T);
    while(T--){
        scanf("%d%d", &n, &m);
        init();
        getmap();
        printf("Case %d: ", k++);
        if(maxflow(0, sect) == sum) //满流
            printf("Yes\n\n");
        else
            printf("No\n\n");
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-13 17:39:09

HDU 3572 【最大流 && 时间区间建图】的相关文章

POJ 3422 HDU 2686,3376 费用流拆点建图

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3376 http://acm.hdu.edu.cn/showproblem.php?pid=2686 http://poj.org/problem?id=3422 POJ 3422为从矩阵左上角走到右下角,最多走k次,每个格子里的数字只能算一次,后面可以重复经过,求经过的各个数字的和的最大值. 拆点,入点向出点连流量为1,费用为当前格子负值的边,向下方,右方连边,流量为k,费用为0,起点连流量为1,

HDU 4888 Redraw Beautiful Drawings 网络流 建图

题意: 给定n, m, k 下面n个整数 a[n] 下面m个整数 b[n] 用数字[0,k]构造一个n*m的矩阵 若有唯一解则输出这个矩阵,若有多解输出Not Unique,若无解输出Impossible 思路:网络流,,, n行当成n个点,m列当成m个点 从行-列连一条流量为k的边,然后源点-行连一条a[i]的边, 列-汇点 流量为b[i] 瞎了,该退役了 T^T #include<stdio.h> #include<string.h> #include<iostream&

图的着色问题 带时间区间的图着色问题

安排会面问题,有几个研究组,不同的学生各自有自己感兴趣的若干个组想去看,每个组讨论时间为t,求总的最小的时间. 1.枚举颜色的可能 o((n-1)^n) 2.进行k着色,从1开始,看可能与否,不行把k加1然后再判断 3. 韦尔奇.鲍威尔法对图G进行着色 a)将图G中的结点按度数的递减次序排列 b)用第一种颜色对第一点着色,按排列次序,对前面的着色点不邻接的每一点用上同样的颜色 c)用第二种颜色对尚未着色的点重复(b),第三种继续. 4.有个叫 路线着色问题 还没看懂 5.有时间区间的图着色问题

HDU2732Leapin&amp;#39; Lizards(最大流SAP,建图---折点法)

Leapin' Lizards Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1531    Accepted Submission(s): 623 Problem Description Your platoon of wandering lizards has entered a strange room in the labyr

HDU2732Leapin&#39; Lizards(最大流SAP,建图---折点法)

Leapin' Lizards Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1531    Accepted Submission(s): 623 Problem Description Your platoon of wandering lizards has entered a strange room in the labyr

hdu 5294 Tricks Device 最短路建图+最小割

链接:http://acm.hdu.edu.cn/showproblem.php?pid=5294 Tricks Device Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 375    Accepted Submission(s): 98 Problem Description Innocent Wu follows Dumb Zh

HDU 3572 最大流

[题意]有n个任务,每个任务必须开始于第Si天之后(包括Si),结束于第Ei天之前(包括Ei),每个任务持续的时间为Pi,现在有m台机器,每台每天只能专注做其中一件任务,每个任务做的时间可以不连续.问是否存在一种方案使得这n个任务顺利完成 [类型]最大流 [建图]设一个源点S,将每个任务分别化成一个点,S向每个任务连一条边,容量为Pi,接着每个任务向时间Si~Ei分别连一条容量为1的边,每个时间向汇点连一条容量为m的边.这样跑最大流即可. #include<cstdio> #include&l

hdu 2883 kebab(时间区间压缩 &amp;amp;&amp;amp; dinic)

kebab Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1243    Accepted Submission(s): 516 Problem Description Almost everyone likes kebabs nowadays (Here a kebab means pieces of meat grilled on

hdu 2883 kebab(时间区间压缩 &amp;&amp; dinic)

kebab Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1243    Accepted Submission(s): 516 Problem Description Almost everyone likes kebabs nowadays (Here a kebab means pieces of meat grilled on