HDU-3572 Task Schedule (最大流Dinic做法)

HDU-3572 Task Schedule (最大流Dinic做法)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3572

我的博客:https://acmerszq.cn

题意:

​ M台机器处理N个任务。对于第i个任务,工厂必须在第 S i S_i Si天或之后开始处理,处理 P i P_i Pi天,并在 E i E_i Ei之前或 E i E_i Ei这天完成任务。一台机器只能处理一个任务,一个任务一次只能被一台机器处理,但是能在不同日期和不同机器上中断和处理。判断是否存在时间表完成所有任务。

思路:

​ 基于天数建图。如下图:

HDU-3572 Task Schedule (最大流Dinic做法)_第1张图片

注意:容量为m的路不要重复建路

AC代码:

#include 
using namespace std;

const int MAXN = 2010;     //点数的最大值
const int MAXM = 1200010;  //边数的最大值
const int INF = 0x3f3f3f3f;
struct Edge {
    int to, next, cap, flow;
} edge[MAXM];  //注意是 MAXM
int tol;
int head[MAXN];
void init() {
    tol = 2;
    memset(head, -1, sizeof(head));
}
void addedge(int u, int v, int w, int rw = 0) {
    edge[tol].to = v;
    edge[tol].cap = w;
    edge[tol].flow = 0;
    edge[tol].next = head[u];
    head[u] = tol++;
    edge[tol].to = u;
    edge[tol].cap = rw;
    edge[tol].flow = 0;
    edge[tol].next = head[v];
    head[v] = tol++;
}
int Q[MAXN];
int dep[MAXN], cur[MAXN], sta[MAXN];
bool bfs(int s, int t, int n) {
    int front = 0, tail = 0;
    memset(dep, -1, sizeof(dep[0]) * (n + 1));
    dep[s] = 0;
    Q[tail++] = s;
    while (front < tail) {
        int u = Q[front++];
        for (int i = head[u]; i != -1; i = edge[i].next) {
            int v = edge[i].to;
            if (edge[i].cap > edge[i].flow && dep[v] == -1) {
                dep[v] = dep[u] + 1;
                if (v == t) return true;
                Q[tail++] = v;
            }
        }
    }
    return false;
}
int dinic(int s, int t, int n) {
    int maxflow = 0;
    while (bfs(s, t, n)) {
        for (int i = 0; i < n; i++) cur[i] = head[i];
        int u = s, tail = 0;
        while (cur[s] != -1) {
            if (u == t) {
                int tp = INF;
                for (int i = tail - 1; i >= 0; i--) tp = min(tp, edge[sta[i]].cap - edge[sta[i]].flow);
                maxflow += tp;
                for (int i = tail - 1; i >= 0; i--) {
                    edge[sta[i]].flow += tp;
                    edge[sta[i] ^ 1].flow -= tp;
                    if (edge[sta[i]].cap - edge[sta[i]].flow == 0) tail = i;
                }
                u = edge[sta[tail] ^ 1].to;
            } else if (cur[u] != -1 && edge[cur[u]].cap > edge[cur[u]].flow && dep[u] + 1 == dep[edge[cur[u]].to]) {
                sta[tail++] = cur[u];
                u = edge[cur[u]].to;
            } else {
                while (u != s && cur[u] == -1) u = edge[sta[--tail] ^ 1].to;
                cur[u] = edge[cur[u]].next;
            }
        }
    }
    return maxflow;
}
bool vis[MAXN];
int main() {
    // freopen("RAW/in", "r", stdin);
    // freopen("RAW/out", "w", stdout);
    int T;
    scanf("%d", &T);
    for (int _ = 1; _ <= T; _++) {
        init();
        memset(vis, false, sizeof(vis));
        printf("Case %d: ", _);
        int n, m;
        scanf("%d%d", &n, &m);
        int sum = 0;
        for (int i = 0; i < n; i++) {
            int p, s, e;
            scanf("%d%d%d", &p, &s, &e);
            sum += p;
            addedge(0, 501 + i, p);
            for (int j = s; j <= e; j++) {
                addedge(501 + i, j, 1);
                if (!vis[j]) {
                    addedge(j, 501 + n, m);
                }
                vis[j] = true;
            }
        }
        int ans = dinic(0, 501 + n, n + 2 + 500);
        if (ans == sum)
            printf("Yes\n");
        else
            printf("No\n");
        printf("\n");
    }
    return 0;
}

你可能感兴趣的:(最大流,网络流,最大流)