hdu3572--Task Schedule(最大流+兩種最佳化方法,dinic)

來源:互聯網
上載者:User

標籤:des   style   color   java   使用   os   io   strong   

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


Problem DescriptionOur 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.
 
InputOn 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.
 
OutputFor 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;}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.