HDU - 2883 kebab (最大流)

题目大意:有一个烤肉老板,每个单位时间可以完成M的烤肉

现在有N位客人,给出每位客人来的时间,走的时间,烤肉的数量和每串烤肉所需的单位时间

问这个老板能否完成每位客人的需求

解题思路:这题和HDU 3572相似,但又不能像那题那样做,因为这题时间长度有点大

所以将时间区间当成一个点,将该区间连向超级汇点,容量为区间长度*M

将所有客人连向超级源点,容量为烤肉数量*每串烤肉所需时间

接下来的难点就是怎么将客人和时间区间连起来了

如果时间区间在客人来的时间和走的时间这段区间内,就表明这段时间可以用来帮客人烤肉,所以可以连接,容量为INF

这样图就建好了

附上大神的详细题解

详细题解

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
#define N 1010
#define INF 0x3f3f3f3f

struct Edge {
    int from, to, cap, flow;
    Edge() {}
    Edge(int from, int to, int cap, int flow): from(from), to(to), cap(cap), flow(flow) {}
};

struct ISAP {
    int p[N], num[N], cur[N], d[N];
    int t, s, n, m;
    bool vis[N];

    vector<int> G[N];
    vector<Edge> edges;

    void init(int n) {
        this->n = n;
        for (int i = 0; i <= n; i++) {
            G[i].clear();
            d[i] = INF;
        }
        edges.clear();
    }

    void AddEdge(int from, int to, int cap) {
        edges.push_back(Edge(from, to, cap, 0));
        edges.push_back(Edge(to, from, 0, 0));
        m = edges.size();
        G[from].push_back(m - 2);
        G[to].push_back(m - 1);
    }

    bool BFS() {
        memset(vis, 0, sizeof(vis));

        queue<int> Q;
        d[t] = 0;
        vis[t] = 1;
        Q.push(t);

        while (!Q.empty()) {
            int u = Q.front();
            Q.pop();

            for (int i = 0; i < G[u].size(); i++) {
                Edge &e = edges[G[u][i] ^ 1];
                if (!vis[e.from] && e.cap > e.flow) {
                    vis[e.from] = true;
                    d[e.from] = d[u] + 1;
                    Q.push(e.from);
                }
            }
        }
        return vis[s];
    }

    int Augment() {
        int u = t, flow = INF;
        while (u != s) {
            Edge &e = edges[p[u]];
            flow = min(flow, e.cap - e.flow);
            u = edges[p[u]].from;
        }

        u = t;
        while (u != s) {
            edges[p[u]].flow += flow;
            edges[p[u] ^ 1].flow -= flow;
            u = edges[p[u]].from;
        }
        return flow;
    }

    int Maxflow(int s, int t) {
        this->s = s; this->t = t;
        int flow = 0;
        BFS();
        if (d[s] > n)
            return 0;

        memset(num, 0, sizeof(num));
        memset(cur, 0, sizeof(cur));
        for (int i = 0; i < n; i++)
            if (d[i] < INF)
                num[d[i]]++;
        int u = s;

        while (d[s] <= n) {
            if (u == t) {
                flow += Augment();
                u = s;
            }
            bool ok = false;
            for (int i = cur[u]; i < G[u].size(); i++) {
                Edge &e = edges[G[u][i]];
                if (e.cap > e.flow && d[u] == d[e.to] + 1) {
                    ok = true;
                    p[e.to] = G[u][i];
                    cur[u] = i;
                    u = e.to;
                    break;
                }
            }

            if (!ok) {
                int Min = n;
                for (int i = 0; i < G[u].size(); i++) {
                    Edge &e = edges[G[u][i]];
                    if (e.cap > e.flow)
                        Min = min(Min, d[e.to]);
                }
                if (--num[d[u]] == 0)
                    break;
                num[d[u] = Min + 1]++;
                cur[u] = 0;
                if (u != s)
                    u = edges[p[u]].from;
            }
        }
        return flow;
    }
};

ISAP isap;
int S[N], E[N], num[N], T[N], All[N];
int n, m;

void solve() {
    int t, cnt = 0, s = 0, Sum = 0;

    for (int i = 1; i <= n; i++) {
        scanf("%d%d%d%d", &S[i], &num[i], &E[i], &T[i]);
        All[cnt++] = S[i];
        All[cnt++] = E[i];
        Sum += num[i] * T[i];
    }
    sort(All, All + cnt);
    cnt = unique(All, All + cnt) - All;
    t = n + cnt + 1;
    isap.init(t);

    for (int i = 1; i <= n; i++)
        isap.AddEdge(s, i, num[i] * T[i]);
    for (int i = 1; i < cnt; i++) {
        isap.AddEdge(i + n, t, (All[i] - All[i - 1]) * m);
        for (int j = 1; j <= n; j++) {
            if (S[j] <= All[i - 1] && E[j] >= All[i]) {
                isap.AddEdge(j, i + n, INF);
            }
        }
    }
    if (Sum == isap.Maxflow(s, t))
        printf("Yes\n");
    else
        printf("No\n");
}

int main() {
    while (scanf("%d%d", &n, &m) != EOF) {
        solve();
    }
    return 0;
}

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

时间: 2024-10-10 16:07:55

HDU - 2883 kebab (最大流)的相关文章

HDU 2883 kebab(最大流)

HDU 2883 kebab 题目链接 题意:有一个烧烤机,每次最多能烤 m 块肉,现在有 n 个人来买烤肉,每个人到达时间为 si,离开时间为 ei,点的烤肉数量为 ci,每个烤肉所需烘烤时间为 di,注意一个烤肉可以切成几份来烤 思路:把区间每个点存起来排序后,得到最多2 * n - 1个区间,这些就表示几个互相不干扰的时间,每个时间内只可能有一个任务器做,这样建模就简单了,源点连向汇点,容量为任务需要总时间,区间连向汇点,容量为区间长度,然后每个任务如果包含了某个区间,之间就连边容量无限大

HDU 2883 kebab

kebab Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HDU. Original ID: 288364-bit integer IO format: %I64d      Java class name: Main Almost everyone likes kebabs nowadays (Here a kebab means pieces of meat grilled on a long

hdu 2883 kebab 网络流

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2883 Almost everyone likes kebabs nowadays (Here a kebab means pieces of meat grilled on a long thin stick). Have you, however, considered about the hardship of a kebab roaster while enjoying the delicio

HDU 2883 —— kebab

原题:http://acm.hdu.edu.cn/showproblem.php?pid=2883 #include<cstdio> #include<cstring> #include<string> #include<queue> #include<vector> #include<algorithm> #define inf 1e9 using namespace std; const int maxn = 41000; con

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

hdu 2883(构图+最大流+压缩区间)

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

hdoj 2883 kebab 【经典最大流】

题目:hdoj 2883 kebab 题意:现在有n个人要烤肉,有m个烤肉架,然后给出每个人的烤肉开始时间si,结束时间ei,以及要烤肉的串数num,还有拷一串的时间ti,然后问你能不能满足所有人的要求. 分析:这是一个比较经典的最大流,经典在于建图方法,这个题目难点在于时间跨度在0---100 0000,如果时间短的话就可以用题目3572的做法了.点击打开链接 后面看了别人的建图方法,确实比较经典,经典在于把区间缩点了,这个题目的难点在于时间跨度大,那么我们可以把所有的时间点记录下来,然后排序

K - Leapin&#39; Lizards - HDU 2732(最大流)

题意:在一个迷宫里面有一些蜥蜴,这个迷宫有一些柱子组成的,并且这些柱子都有一个耐久值,每当一只蜥蜴跳过耐久值就会减一,当耐久值为0的时候这个柱子就不能使用了,每个蜥蜴都有一个最大跳跃值d,现在想知道有多少蜥蜴不能离开迷宫(跳出迷宫就可以离开了.) 输入描述:输入矩阵的行M和跳跃值D,接着输入两个矩阵(列数自己求),第一个矩阵是每个柱子的耐久值,下面一个矩阵有'L'的表示这个柱子上有一只蜥蜴. 分析:题目明白还是比较容易的,矩阵的点数是20*20,也就400个点,对每个有耐久值的柱子进行拆点,然后