[HDU 3572]Task Schedule[最大流]

題目鏈接:[HDU 3572]Task Schedule[最大流]

題意分析:

N個任務,每個只能在si之後開始執行,得執行pi天,在ei天前得執行完,有M個機器進行作業,問:能否在每個的規定時間之內,把所有任務執行完?任務可以中斷執行,比如第一天做,第二天休息,第三天做。

解題思路:

我們將每天視爲一個點,那麼第i個任務可以在si到ei之間執行,那麼說明這個任務可以和[si,ei]之間的點連一天流量爲1的邊,代表被執行了一天,接着我們將源點與第i個任務連一條容量爲pi的邊,代表這個任務要被執行的次數,最後我們讓每天和匯點之間連一條容量爲m的邊,表示這天最多執行m個任務,如果最大流等於需要被執行的總天數,則Yes,否則No。

個人感受:

哇(⊙0⊙),神奇的建圖。網絡流老想着貪心這個梗估計能接着陪完玩到畢業啊= =。

具體代碼如下:

#include<algorithm>
#include<cctype>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iomanip>
#include<iostream>
#include<map>
#include<queue>
#include<set>
#include<sstream>
#include<stack>
#include<string>
#define lowbit(x) (x & (-x))
#define root 1, n, 1
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1
#define ll long long
#define pr(x) cout << #x << " = " << (x) << '\n';
using namespace std;

const int MAXN = 1010;//點數的最大值
const int MAXM = 1e6;//邊數的最大值
const int INF = 0x3f3f3f3f;

struct Edge{
    int to,next,cap,flow;
}edge[MAXM];//注意是MAXM
int tol;
int head[MAXN];
int gap[MAXN],dep[MAXN],cur[MAXN];

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];
void BFS(int start,int end)
{
    memset(dep,-1,sizeof(dep));
    memset(gap,0,sizeof(gap));
    gap[0] = 1;
    int front = 0, rear = 0;
    dep[end] = 0;
    Q[rear++] = end;
    while(front != rear)
    {
        int u = Q[front++];
        for(int i = head[u]; i != -1; i = edge[i].next)
        {
            int v = edge[i].to;
            if(dep[v] != -1)continue;
            Q[rear++] = v;
            dep[v] = dep[u] + 1;
            gap[dep[v]]++;
        }
    }
}

int S[MAXN];
int sap(int start,int end,int N)
{
    BFS(start,end);
    memcpy(cur,head,sizeof(head));
    int top = 0;
    int u = start;
    int ans = 0;
    while(dep[start] < N)
    {
        if(u == end)
        {
            int Min = INF;
            int inser;
            for(int i = 0;i < top;i++)
            if(Min > edge[S[i]].cap - edge[S[i]].flow)
            {
                Min = edge[S[i]].cap - edge[S[i]].flow;
                inser = i;
            }
            for(int i = 0;i < top;i++)
            {
                edge[S[i]].flow += Min;
                edge[S[i]^1].flow -= Min;
            }
            ans += Min;
            top = inser;
            u = edge[S[top]^1].to;
            continue;
        }
        bool flag = false;
        int v;
        for(int i = cur[u]; i != -1; i = edge[i].next)
        {
            v = edge[i].to;
            if(edge[i].cap - edge[i].flow && dep[v]+1 == dep[u])
            {
                flag = true;
                cur[u] = i;
                break;
            }
        }
        if(flag)
        {
            S[top++] = cur[u];
            u = v;
            continue;
        }
        int Min = N;
        for(int i = head[u]; i != -1; i = edge[i].next)
            if(edge[i].cap - edge[i].flow && dep[edge[i].to] < Min)
            {
                Min = dep[edge[i].to];
                cur[u] = i;
            }
        gap[dep[u]]--;
        if(!gap[dep[u]]) return ans;
        dep[u] = Min + 1;
        gap[dep[u]]++;
        if(u != start)u = edge[S[--top]^1].to;
    }
    return ans;
}

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

int main()
{
    #ifdef LOCAL
    freopen("C:\\Users\\apple\\Desktop\\in.txt", "r", stdin);
    #endif
    int n, m;
    for (int kk, kase = scanf("%d", &kk); kase <= kk; ++kase) {
        init();
        scanf("%d%d", &n, &m);
        int s, p, e;
        int src = 0, des = 0, mx = 0, sum = 0;
        for (int i = 1; i <= n; ++i) {
            scanf("%d%d%d", &p, &s, &e);
            addedge(src, i, p);
            sum += p;
            mx = max(mx, e);
            for (int j = s; j <= e; ++j) {
                addedge(i, j + n, 1);
            }
        }
        des = mx + n + 1;
        for (int i = 1; i <= mx; ++i) {
            addedge(i + n, des, m);
        }

        printf("Case %d: ", kase);
        if (sap(src, des, des + 1) == sum) printf("Yes\n\n");
        else printf("No\n\n");
    }
    return 0;
}


發佈了231 篇原創文章 · 獲贊 18 · 訪問量 15萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章