HDU 3572 最大流

Task Schedule

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


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
2
4 3
1 3 5
1 1 4
2 3 7
3 5 9
2 2
2 1 3
1 2 2
 
Sample Output
Case 1: Yes
Case 2: Yes
 
Author
allenlowesy
 
Source
 题意:
有n个任务,m台机器,给出每个任务需要耗费的时间以及能做此任务的时间点,每个任务每天只能由一台机器做,一台机器每天只能做一个任务问能否将这些任务做完
输入n,m
输入n行x,y,z表示该任务需要x天,第y天到第z天可以做这个任务
代码:
//从原点连向n个任务容量为该任务需要的时间,n个任务连向500个时间点容量为1(一个任务一天只有一台机器去做),
//时间点连向汇点,容量为机器数m(一台机器一天只能做一个任务),求最大流是否等于完成所有任务的天数。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
using namespace std;
const int maxn=2009;
const int inf=0x7fffffff;
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>edges;
    vector<int>g[maxn];
    bool vis[maxn];
    int d[maxn];
    int cur[maxn];
    void Init(int n){
        this->n=n;
        for(int i=0;i<n;i++) g[i].clear();
        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;
        q.push(s);
        d[s]=0;
        vis[s]=1;
        while(!q.empty()){
            int x=q.front();q.pop();
            for(int i=0;i<(int)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<(int)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;
    }
}dc;
int main()
{
    int t,n,m;
    scanf("%d",&t);
    for(int cas=1;cas<=t;cas++){
        scanf("%d%d",&n,&m);
        dc.Init(n+502);
        int x,y,z,sum=0;
        for(int i=1;i<=n;i++){
            scanf("%d%d%d",&x,&y,&z);
            sum+=x;
            dc.Addedge(0,i,x);
            for(int j=y;j<=z;j++)
                dc.Addedge(i,j+n,1);
        }
        for(int i=1;i<=500;i++)
            dc.Addedge(i+n,501+n,m);
        printf("Case %d: ",cas);
        int tmp=dc.Maxflow(0,501+n);
        if(sum==tmp) printf("Yes

");
        else printf("No

");
    }
    return 0;
}
原文地址:https://www.cnblogs.com/--ZHIYUAN/p/6928027.html