HDU_3572_Task Schedule(最大流)

来源:互联网 发布:mysql slave设置 编辑:程序博客网 时间:2024/04/30 04:00

Task Schedule

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5803    Accepted Submission(s): 1862



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
24 31 3 5 1 1 42 3 73 5 92 22 1 31 2 2
 

Sample Output
Case 1: Yes Case 2: Yes
 题意:给出N个任务,M个机器,每天每台机器只能处理一件事,接下来N行,每行有p s e,分别表示这个任务要用p天,要在s~e天完成,问你这所有任务能不能完成。
分析:最大流问题。建立最大流模型,然后判断最大流和完成这些任务需要的总时间是否相等。
建图:加入超级源点s,s指向所有的任务,容量为pi;每个任务 i 指向它指点的时间段[Si,Ei]中的每一天,容量为1;加入超级汇点t,使得([S1,E1]) U ([S2,E2]) U …… ([Sn,En])中的每一天都指向t,容量为M,因为每一天都能够有M台机器运行。
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3572
代码清单:
#include<map>#include<set>#include<cmath>#include<queue>#include<stack>#include<ctime>#include<cctype>#include<string>#include<cstdio>#include<cstring>#include<cstdlib>#include<iostream>#include<algorithm>using namespace std;#define end() return 0typedef long long ll;typedef unsigned int uint;typedef unsigned long long ull;const int maxn = 550000 + 5;const int INF = 0x7f7f7f7f;struct Edge{    int from,to,cap,flow;    Edge(int u,int v,int c,int f):from(u),to(v),cap(c),flow(f){}};struct dinic{    int n,m,s,t; //结点数,边数(包括反向弧),源点,汇点    vector<Edge>edge;//边表。edge[e]和edge[e^1]互为反向弧    vector<int>G[maxn];//邻接表。G[i][j]表示结点i的第j条边在e数组的序号    bool vis[maxn]; //bfs用    int d[maxn]; //从起点到i的距离    int cur[maxn]; //当前弧下标    void init(int n,int s,int t){        this -> n = n;        this -> s = s;        this -> t = t;        for(int i=0;i<=n;i++) G[i].clear();        edge.clear();    }    void addEdge(int from,int to,int cap){        edge.push_back(Edge(from,to,cap,0));        edge.push_back(Edge(to,from,0,0));        m=edge.size();        G[from].push_back(m-2);        G[to].push_back(m-1);    }    bool bfs(){        memset(vis,false,sizeof(vis));        queue<int>q;        q.push(s);        d[s]=0;        vis[s]=true;        while(!q.empty()){            int x=q.front();q.pop();            for(int i=0;i<G[x].size();i++){                Edge& e=edge[G[x][i]];                if(!vis[e.to]&&e.cap>e.flow){ //只考虑残量网络中的弧                    vis[e.to]=true;                    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=edge[G[x][i]];            if(d[x]+1==d[e.to]&&(f=dfs(e.to,min(a,e.cap-e.flow)))>0){                e.flow+=f;                edge[G[x][i]^1].flow-=f;                flow+=f;                a-=f;                if(a==0) break;            }        }        return flow;    }    int maxflow(){        int flow=0;        while(bfs()){            memset(cur,0,sizeof(cur));            flow+=dfs(s,INF);        }        return flow;    }};int T;int N,M;int P,S,E;int sump,maxd,tail,cases;dinic dc;struct Node{int p,s,e;}node[505];void input(){    maxd=-1;sump=0;    scanf("%d%d",&N,&M);    for(int i=1;i<=N;i++){        scanf("%d%d%d",&node[i].p,&node[i].s,&node[i].e);        maxd=max(maxd,node[i].e);        sump+=node[i].p;    }}void createGraph(){    tail=N+maxd+1;    dc.init(tail+1,0,tail);    for(int i=1;i<=N;i++){        dc.addEdge(0,i,node[i].p);        for(int j=node[i].s;j<=node[i].e;j++){            dc.addEdge(i,N+j,1);        }    }    for(int i=N+1;i<=tail-1;i++){        dc.addEdge(i,tail,M);    }}void solve(){    createGraph();    printf("Case %d: ",++cases);    int flow=dc.maxflow();    if(flow==sump) puts("Yes");    else puts("No");    printf("\n");}int main(){    scanf("%d",&T);    while(T--){        input();        solve();    }    end();}


0 0
原创粉丝点击