Machine Schedule-匈牙利算法/Dinic

4 篇文章 0 订阅
3 篇文章 0 订阅
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;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值