天天写算法之Machine Schedule

这个题目:

有两台机器A和B,A机器有n种工作方式,B机器有m种工作方式。共有k个任务。每个任务恰好在一条机器上运行。
如果任务在A机器上运行,就需要转换为模式Xi,如果在B机器上运行,就需要转换为模式Yi
每台机器上的任务可以按照任意顺序执行,但是每台机器每转换一次模式需要重启一次。
请合理为每个任务安排一台机器并合理安排顺序,使得机器重启次数尽量少。

需要注意的是,初始状态都在0节点处,所以这种情况不用考虑。也就是为0的时候,直接跳过就好。
剩下的就变成了最小覆盖问题,最小覆盖=n-最大匹配,所以还是hungary算法。

代码::
#include<iostream>
#include<string.h>
#include<cstdio>
#define MAX 105
using namespace std;

int n ,m , k;
int Map[MAX][MAX];//两个人是否有关系
bool used[MAX];//是否被访问过
int linker[MAX];//查看被连接的那一个集合是否有男生来连接了。

bool dfs(int a){
    for(int j = 0 ; j < m ; j ++)
    {
        if(!used[j]&&Map[a][j])
        {
            used[j]=1 ;
            if(linker[j]==-1||dfs(linker[j]))
            {
                linker[j]=a;
                return true ;
            }
        }
    }
    return false ;
}

int hungary(){
    int ans = 0 ;
    memset(linker,-1,sizeof(linker));
    for(int i = 0 ; i < n ; i ++)
    {
        memset(used,0,sizeof(used));
        if(dfs(i)) ans+=1;
    }
    return ans ;
}
int main(){
    int i , j ,num,a,b;
    while(scanf("%d",&n)&&n)
    {
        scanf("%d%d",&m,&k);
      memset(Map,0,sizeof(Map));
      for( i = 0 ; i < k ; i ++)
      {
          scanf("%d%d%d",&num,&a,&b);
          if(a&&b)//这个是因为,一开始状态为0,那么就不再考虑这种情况了,不需要变换
          Map[a][b]=1 ;
          //Map[b][a]=1 ;//这里不正确,不能加这个,因为加入1 3 有关系,反过来就成 3  1 有关系,因为是两个不同的机器
      }
      printf("%d\n",hungary());
    }
}

阅读更多

求解Machine Schedule

07-13

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.rnrnThere 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.rnrnFor 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.rnrnObviously, 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. rnrnrnInputrnrnThe 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.rnrnThe input will be terminated by a line containing a single zero.rnrnrnOutputrnrnThe output should be one integer per line, which means the minimal times of restarting machine.rnrnrnSample Inputrnrn5 5 10rn0 1 1rn1 1 2rn2 1 3rn3 1 4rn4 2 1rn5 2 2rn6 2 3rn7 2 4rn8 3 3rn9 4 3rn0rnrnrnSample Outputrnrn3rn这道题目好象很典型,但是我就不会,听说是用图,但是我想不明白,请大侠门指点迷津

没有更多推荐了,返回首页