zoukankan      html  css  js  c++  java
  • ZOJ 1364 POJ 1325 -Machine Schedule

    Time Limit:1000MS    Memory Limit:10000K

    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. 

       众所周知,机器调度是计算机科学中非常典型的一个问题,已经被研究很长时间了。各种机器调度问题在一下方面差别很大:必须满足的约束条件,以及期望得到的调度时间表。现在考虑一个针对两台机器的机器调度问题。

       假设有两台机器,A和B。机器A有n种工作模式,分别称为mode_0,mode_1,...,mode_n-1。同时机器B有m种工作模式,分别为mode_0,mode_1,...,mode_m-1.刚开始时,A和B都工作在模式mode_0.

       给定K个作业,每个作业可以工作在任何一个机器的特定模式下。例如,作业0可以工作在机器A的模式mode_3或者机器B的mode_4模式;作业1可以工作在机器A的模式mode_2或者工作在机器B的模式mode_4等。因此,对于工作j,调度中的约束条件可以表述成一个三元组(i,x,y),意思是工作i可以在机器A的mode_x模式或者机器B的mode_y模式。

       很显然的是,为了完成所有的作业,必须时不时切换机器的工作模式,但不幸的是,机器工作模式的切换只能通过手动重启机器完成。试编写程序实现:改变机器的顺序,给每个作业分配合适的作业,使得重启机器的次数最少。

    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.

       输入文件包含多个测试数据,每个测试数据的第一行为3个整数:n,m和k,其中n,m<100,k<1000,。接下来有k行给出了k个作业的约束,每一行为一个三元组:i,x,y。

       输入文件的最后一行为一个0,表示输入结束。

    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

    Source

     Beijing 2002

            首先构造二分图:把A的n个mode和B的m个mode看作图的顶点,如果某个人物可以在A的mode_i或B的mode_j上完成,则Ai到Bj连接一条边,这样构造了一个二分图。

        本题要求二分图的最小点覆盖集问题,即求最小的顶点集合,“覆盖”住所有的边。转换成求二分图的最大匹配问题。

        另外,机器A和机器B最初工作在模式_0,所以对于那些可以工作在机器A的模式_0或者机器B的模式_0的作业,在完成这些工作时是不需要重启机器的。

     

        代码:

    var
      c,e,n,m,i,s,t,k:longint;
      ans,inf:int64;
      h,d,f,g:array[0..500]of longint;
      ot,cap,ne:array[0..10000]of longint;
    
    procedure addedge(x,y,z:longint);
    begin
      ot[e]:=y; ne[e]:=g[x]; cap[e]:=z; g[x]:=e; inc(e);
      ot[e]:=x; ne[e]:=g[y]; cap[e]:=0; g[y]:=e; inc(e);
    end;
    
    function min(a,b:int64):int64;
    begin
      if a<b then exit(a)
             else exit(b);
    end;
    
    function bfs:boolean;
    var
      l,r,p:int64;
    begin
      for i:=s to t do d[i]:=t+10;
      l:=0; r:=1; h[1]:=s; d[s]:=0;
      while l<r do
        begin
          inc(l);
          p:=g[h[l]];
          while p<>-1 do
            begin
              if (cap[p]<>0)and(d[ot[p]]>d[h[l]]+1) then
                begin
                  inc(r);
                  h[r]:=ot[p];
                  d[ot[p]]:=d[h[l]]+1;
                end;
              p:=ne[p];
            end;
        end;
      exit(d[t]<>t+10);
    end;
    
    function dfs(x,flow:int64):int64;
    var
      p,tmp:int64;
    begin
      if x=t then exit(flow);
      p:=f[x]; dfs:=0;
      while (p<>-1)and(dfs<flow) do
        begin
          if (cap[p]<>0)and(d[ot[p]]=d[x]+1) then
            begin
              tmp:=dfs(ot[p],min(flow-dfs,cap[p]));
              dec(cap[p],tmp);
              inc(cap[p xor 1],tmp);
              inc(dfs,tmp);
            end;
          p:=ne[p];
        end;
      f[x]:=p;
    end;
    
    begin
      inf:=high(int64);
      read(n);
      while n<>0 do begin
      readln(m,k);
      e:=0;
      fillchar(g,sizeof(g),255);
      for i:=1 to n do addedge(0,i,1);
      for i:=1 to m do addedge(n+i,m+n+1,1);
      for i:=1 to k do
        begin
          readln(s,t,c); inc(t); inc(c);
          if (t<>1)and(c<>1) then addedge(t,n+c,1);
        end;
      s:=0; t:=n+m+1; ans:=0;
      while bfs do
        begin
          for i:=s to t do
            f[i]:=g[i];
          inc(ans,dfs(s,inf));
        end;
      writeln(ans);
      read(n); end;
    end.

     

     

  • 相关阅读:
    MVC4中常用的短句及配置归结(部分)
    结合EF5.0讲MVC4(四)将我们的程序改成数据库优先模式
    结合EF5.0讲MVC4(二)为先前程序添加查询及主外键关系
    【译】《Pro ASP.NET MVC4 4th Edition》第二章(一)
    XtraReport应用(1)(XtraReport From File)
    结合EF5.0讲MVC4(一)创建一个MVC4应用程序
    Scrum实际应用(一)
    结合EF5.0讲MVC4(三)为我们的程序添加过滤器
    C# LINQ详解(一)
    如何在 Windows Server 2008 上打开 SQL Server 防火墙端口
  • 原文地址:https://www.cnblogs.com/kry-ssw-1314/p/4559702.html
Copyright © 2011-2022 走看看