hdu 3572 Task Schedule(多源多汇)

建图:多源多汇问题,大白书算法通过体添加超汇和超源

把每个任务和每一天都看做一个点,添加源点和汇点。

源点与每个任务之间连一条边,容量为完成该任务所需处理次数。

若第i个任务可以在Si至Ei天处理,则由该任务向这些天分别连一条边,容量为1,表示此任务每天只能被处理一次。

最后,从每一天连一条到汇点的边,容量为机器数M,表示每天可以处理M个任务。

若求出的最大流等于所有任务需要处理的次数之和,说明能完成任务;否则,不能完成任务。

dinic算法

注意:初始化的时候可能把0连接的边给忘掉

#include <vector>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <queue>
using namespace std;

#define maxn 1100
#define INF 0x7f7f7f7f

struct Edge {
    int from,to,cap,flow;
};

struct  Dinic {
    int n,m,s,t;
    vector<Edge>edges;
    vector<int>G[maxn];
    bool vis[maxn];
    int d[maxn];
    int cur[maxn];

    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);
    }

    void init(){
      edges.clear();
      for(int i=0;i<=maxn;i++) G[i].clear();
    }

    bool Bfs() {
        memset(vis,0,sizeof(vis));
        queue<int>Q;
        while(!Q.empty()) Q.pop();
        Q.push(s);
        d[s]=0;
        vis[s] = 1;
        while(!Q.empty()) {
            int x = Q.front();
            Q.pop();
            for(int i=0; i<G[x].size(); i++) {
                Edge& e = edges[G[x][i]];
                if(!vis[e.to]&&e.cap>e.flow) {
                    vis[e.to] = 1;
                    d[e.to] = d[x]+1;
                    Q.push(e.to);
                }
            }
        }
        return vis[t];
    }

    int dfs(int x,int a) {
        if(x==t||a==0) return a;
        int flow = 0,f;
        for(int &i=cur[x]; i<G[x].size(); i++) {
            Edge& e  = edges[G[x][i]];
            if(d[x]+1==d[e.to]&&(f=dfs(e.to,min(a,e.cap-e.flow)))>0) {
                e.flow += f;
                edges[G[x][i]^1].flow -= f;
                flow += f;
                a -= f;
                if(a==0) break;
            }
        }
        return flow;
    }

    int maxflow(int s,int t) {
        this->s=s;
        this->t=t;
        int flow  = 0;
        while(Bfs()) {
            memset(cur,0,sizeof(cur));
            flow+=dfs(s,INF);
        }
        return flow;
    }
};

int main(){
    int T;
    int n,k;
    Dinic slove;
    int cas = 0;
    scanf("%d",&T);
    while(T--){
        scanf("%d%d",&n,&k);
        slove.init();
        int sum = 0;
        int mx = -1;
        for(int i=1;i<=n;i++){
            int x,y,z;
            scanf("%d%d%d",&x,&y,&z);
            sum+=x;
            slove.addEdge(0,i,x);
            if(mx<z) mx = z;
            for(int j=y;j<=z;j++){
                slove.addEdge(i,n+j,1);
            }
        }
        int ss=0,tt=mx+n+1;
        for(int j=1;j<=mx;j++)
        {
            slove.addEdge(j+n,tt,k);
        }
        printf("Case %d: ",++cas);
        printf(slove.maxflow(ss,tt)==sum?"Yes\n\n":"No\n\n");
    }
}

版权声明:都是兄弟,请随意转载,请注明兄弟是谁

时间: 2025-01-16 08:26:25

hdu 3572 Task Schedule(多源多汇)的相关文章

HDU 3572 Task Schedule(ISAP)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3572 题意:m台机器,需要做n个任务.第i个任务,你需要使用机器Pi天,且这个任务要在[Si  ,  Ei]区间内完成才有效.对于一个任务,只能由一个机器来完成,一个机器同一时间只能做一个任务.当然,一个任务可以分成几段不连续的时间来完成.问,能否做完全部任务. 题意很清晰,也就是判断是否是满流. 对于网络流问题,模板大家都有,关键在于如何建图(详见资料) 思路:今天问了龙哥,对建图有了一定的了解,

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

hdu 3572 Task Schedule(网络流 dinic算法)

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

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 (最大流)

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

hdu 3572 Task Schedule

Task Schedule 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

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 (Dinic模板)

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

HDU 3572 Task Schedule(ISAP模板&amp;amp;&amp;amp;最大流问题)

题目链接:http://acm.hdu.edu.cn/showproblem.php? pid=3572 题意:m台机器.须要做n个任务. 第i个任务.你须要使用机器Pi天,且这个任务要在[Si  ,  Ei]区间内完毕才有效. 对于一个任务,仅仅能由一个机器来完毕.一个机器同一时间仅仅能做一个任务. 当然,一个任务能够分成几段不连续的时间来完毕.问,是否能做完所有任务. 题意非常清晰.也就是推断是否是满流. 对于网络流问题,模板大家都有,关键在于怎样建图(详见资料) 思路:今天问了龙哥,对建图