码迷,mamicode.com
首页 > 系统相关 > 详细

ZOJ 1364 POJ 1325 -Machine Schedule

时间:2015-06-08 00:49:21      阅读:160      评论:0      收藏:0      [点我收藏+]

标签:

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. 

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

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.

 

 

 

ZOJ 1364 POJ 1325 -Machine Schedule

标签:

原文地址:http://www.cnblogs.com/kry-ssw-1314/p/4559702.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!