Machine Schedule-匈牙利算法/Dinic

Machine Schedule

問題來源:Hdu-1150

Problem Description
As we all know, machine scheduling is a very classical problem in computer science and has been studied for a very long history. Scheduling problems differ widely in the nature of the constraints that must be satisfied and the type of schedule desired. Here we consider a 2-machine scheduling problem.
There are two machines A and B. Machine A has n kinds of working modes, which is called mode_0, mode_1, …, mode_n-1, likewise machine B has m kinds of working modes, mode_0, mode_1, … , mode_m-1. At the beginning they are both work at mode_0.
For k jobs given, each of them can be processed in either one of the two machines in particular mode. For example, job 0 can either be processed in machine A at mode_3 or in machine B at mode_4, job 1 can either be processed in machine A at mode_2 or in machine B at mode_4, and so on. Thus, for job i, the constraint can be represent as a triple (i, x, y), which means it can be processed either in machine A at mode_x, or in machine B at mode_y.
Obviously, to accomplish all the jobs, we need to change the machine's working mode from time to time, but unfortunately, the machine's working mode can only be changed by restarting it manually. By changing the sequence of the jobs and assigning each job to a suitable machine, please write a program to minimize the times of restarting machines.
 
Input
The input file for this program consists of several configurations. The first line of one configuration contains three positive integers: n, m (n, m < 100) and k (k < 1000). The following k lines give the constrains of the k jobs, each line is a triple: i, x, y.
The input will be terminated by a line containing a single zero.
 
Output
The output should be one integer per line, which means the minimal times of restarting machine.

Sample Input
5 5 10
0 1 1
1 1 2
2 1 3
3 1 4
4 2 1
5 2 2
6 2 3
7 2 4
8 3 3
9 4 3
0
 
Sample Output
3

源代碼一(匈牙利算法):
#include<iostream>
#include<cstring>
using namespace std;

const int MAX = 102;
int N , M , K , sum;
int link[MAX] , mark[MAX] , map[MAX][MAX];

bool match( int u );

int main( ){
    int i , a , b , c;
    while( cin>>N && N ){
        cin>>M>>K;
        memset( map , 0 , sizeof( map ) );
        memset( link , -1 , sizeof( link ) );
        while( K-- ){
            cin>>c>>a>>b;
            map[a][b] = 1;
        }
        for( i=1 , sum=0 ; i<=N ; i++ ){
            memset( mark , 0 , sizeof( mark ) );    //每次想進行新的匹配,清空mark數組
            if( match( i ) )    //如果匹配成功
                sum++;    //匹配數加一
        }
        cout<<sum<<endl;
    }

    return 0;
}

bool match( int u ){
    int v;
    for( v=0 ; v<MAX ; v++ ){    //遍歷每一個點
        if( map[u][v] && !mark[v] ){    //u來匹配v
            mark[v] = 1;    //標誌
            if( link[v]==-1 || match( link[v] ) ){    //能夠匹配使得邊數增多
                link[v] = u;    //鏈接改變
                return true;    //返回true
            }
        }
    }

    return false;
}

因爲網絡流的最大流等於二分圖的最大匹配數,所以用Dinic算法也是可以AC的:
源代碼二(Dinic算法):
#include<iostream>
#include<cstring>
#include<queue>
using namespace std;

const int MAX = 204;
const int INF = 1<<30;
queue <int> Q;
int N , M , K , S , T;
int map[MAX][MAX] , mark[MAX] , level[MAX];

int Dinic( void );
int bfs( void );
int dfs( int u , int flow );
int Min( int a , int b ){ return a <= b ? a : b; }

int main( ){
    int i , a , b , c;

    while( cin>>N && N ){
        cin>>M>>K;
        S = 0;
        T = N + M + 1;
        memset( map , 0 , sizeof( map ) );
        while( K-- ){
            cin>>c>>a>>b;
            if( a && b )    //題目就是坑,爲什麼要去掉0的情況,題目也沒說
                map[a][b+N] = 1;
        }
        for( i=1 ; i<=N ; i++ )    //建立源點指向的邊
            map[S][i] = 1;
        for( i=1 ; i<=M ; i++ )    //建議指向匯點的邊
            map[i+N][T] = 1;

        cout<<Dinic()<<endl;    //網絡流的最大流等於二分圖的最大匹配數
    }

    return 0;
}

int Dinic( void ){
    int sum=0;

    while( bfs( ) ){
        sum += dfs( S , INF );
    }

    return sum;
}

int bfs( void ){
    int u , v;

    memset( mark , 0 , sizeof( mark ) );
    memset( level , 0 , sizeof( level ) );
    while( !Q.empty() )    Q.pop( );

    Q.push( S );
    mark[S] = level[S] = 1;

    while( !Q.empty() ){
        u = Q.front();
        Q.pop( );
        if( u==T )    return 1;
        for( v=S ; v<=T ; v++ ){
            if( !mark[v] && map[u][v] ){
                mark[v] = 1;
                Q.push( v );
                level[v] = level[u] + 1;
            }
        }
    }

    return 0;
}

int dfs( int u , int flow ){
    int v , flowSum , sum=0;

    if( u==T )
        return flow;

    for( v=S ; v<=T ; v++ ){
        if( map[u][v] && level[v]==level[u]+1 ){
            flowSum = dfs( v , Min( flow , map[u][v] ) );
            map[u][v] -= flowSum;
            map[v][u] += flowSum;
            sum += flowSum;
            flow -= flowSum;
        }
    }

    return sum;
}

代碼分析:代碼的題意可以看出我們需要重啓動最少的機器數目來實現完成所有的項目,即這是一個最小點覆蓋的問題,由定理知最小點覆蓋等於最大匹配數,所以採用了匈牙利算法來實現求最大匹配數.爲了複習一下Dinic算法,我重寫了一遍,以前的博文裏面有Dinic的詳細註釋,所以這次我沒給出註釋.
注意:用匈牙利算法結局我問題的時候,每次的"dfs"需要memset保證以前的數據不會對此次搜索(匹配)造成影響;用Dinic算法解決最大流的時候,需要把每條有向邊都賦值爲容量爲1;
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章