hdu3572--Task Schedule(最大流+两种优化方法,dinic)

来源:互联网 发布:剑三少林正太脸型数据 编辑:程序博客网 时间:2024/05/17 04:42

Task Schedule

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


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个任务,给出每一个任务完成需要的时间p,要在s,e时间段内完成,问可不可以完成(每个任务可以分部分做)
一天数来建图,天数1到500,源点到每一个点的容量为n,对于每一个任务与在s到e内的点连线,容量是1,任务与汇点连线,容量是任务完成需要的时间,这样求出的最大流如果等于所有任务的时间和,那么就可以完成,都则完不成。
使用了dinic算法,使用bfs建立层次图,再使用dfs更新增广路,第一个优化在dfs中如果找不到最终的汇点,那么将那个节点的的层次值改为-1,也就是在层次图中删掉该点,第二个优化,使用dfs回溯,通过一次dfs将所有可以更新的增广路全部更新。
 
#include <cstdio>#include <cstring>#include <algorithm>#include <math.h>#include <queue>using namespace std;#define maxn 1100#define INF 0x3f3f3f3fstruct edge{    int v , w ;    int next ;} p[1100000] ;int head[maxn] , cnt , l[maxn] ;queue <int> q ;void add(int u,int v,int w){    p[cnt].v = v ; p[cnt].w = w ;    p[cnt].next = head[u] ; head[u] = cnt++ ;    p[cnt].v = u ; p[cnt].w = 0 ;    p[cnt].next = head[v] ; head[v] = cnt++ ;}int bfs(int s,int t){    int u , v , i ;    memset(l,-1,sizeof(l));    l[s] = 0 ;    while( !q.empty() )        q.pop();    q.push(s) ;    while( !q.empty() )    {        u = q.front();        q.pop();        for(i = head[u] ; i != -1 ; i = p[i].next)        {            v = p[i].v ;            if( l[v] == -1 && p[i].w )            {                l[v] = l[u] + 1 ;                q.push(v) ;            }        }    }    if( l[t] > 0 )        return 1 ;    return 0 ;}int dfs(int s,int t,int min1){    if( s == t )        return min1 ;    int i , v , a , ans = 0 ;    for(i = head[s] ; i != -1 ; i = p[i].next)    {        v = p[i].v ;        if( l[v] == l[s] + 1 && p[i].w && (a = dfs(v,t,min(min1,p[i].w) ) ) )        {            p[i].w -= a ;            p[i^1].w += a ;            ans += a ;            min1 -= a ;            if( !min1 )                break;        }    }    if( ans )        return ans ;    l[s] = -1 ;    return 0;}int main(){    int t , tt , n , m , i , j , num , max_flow ;    scanf("%d", &t);    for(tt = 1 ; tt <= t ; tt++)    {        printf("Case %d: ", tt);        cnt = 0 ; num = 0 ; max_flow = 0 ;        memset(head,-1,sizeof(head));        scanf("%d %d", &m, &n);        int pp , s , e ;        for(i = 1 ; i <= m ; i++)        {            scanf("%d %d %d", &pp, &s, &e);            for(j = s ; j <= e ; j++)            {                add(j,500+i,1);            }            add(500+i,1001,pp);            num += pp ;        }        for(i = 1 ; i <= 500 ; i++)            add(0,i,n);        while( bfs(0,1001) )        {            while( int k = dfs(0,1001,INF) )                max_flow += k ;        }        if( num == max_flow )            printf("Yes\n");        else            printf("No\n");        printf("\n");    }    return 0;}

 
0 0
原创粉丝点击