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.

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.
时间: 2024-10-24 05:43:39

ZOJ 1364 POJ 1325 -Machine Schedule的相关文章

POJ 1325 Machine Schedule【最小点覆盖】

E - Machine Schedule Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 1325 Appoint description:  System Crawler  (2014-08-10) Description As we all know, machine scheduling is a very classical pr

poj 1325 Machine Schedule 解题报告

题目链接:http://poj.org/problem?id=1325 题目意思:有 k 个作业,机器A有 n 个模式:0 ~ n-1,机器B 有 m 个模式:0~ m-1.每一个作业能运行在 A 的 某一个模式(假设为 i (0 <= i <= n-1 ) )或 B 的某一个模式下(j (0 <= j <= m-1)).多个作业可以同时运行在 A 的某一个 模式下,当然 B 也如此.每对A 或 B 转换一次模式,就要重启一次 A 或者 B,你需要选择A 或 B 的一些模式,使得所

POJ 1325 Machine Schedule 二分图最大匹配

把每一个任务看做一个边,机器的模式看做是一个点,这个其实就是求一个最少点覆盖所有边即最小点覆盖集的问题,因为最小点覆盖集=二分图的最大匹配,所以问题转化成了求二分图最大匹配问题. 第一次写二分图匹配,感觉建模还是相当困难的. #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <climits> #include <strin

POJ 1325 Machine Schedule (二分图最小点集覆盖 匈牙利算法)

Machine Schedule Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 12621   Accepted: 5399 Description As we all know, machine scheduling is a very classical problem in computer science and has been studied for a very long history. Scheduli

POJ 1325 Machine Schedule——S.B.S.

Machine Schedule Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 13731   Accepted: 5873 Description As we all know, machine scheduling is a very classical problem in computer science and has been studied for a very long history. Scheduli

poj 1325 Machine Schedule 题解

Machine Schedule Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 14479   Accepted: 6172 Description As we all know, machine scheduling is a very classical problem in computer science and has been studied for a very long history. Scheduli

poj 1325 Machine Schedule 最小点覆盖

题目链接:http://poj.org/problem?id=1325 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 satis

POJ 1325 Machine Schedule(二分匹配 最小点覆盖)

题目链接:http://poj.org/problem?id=1325 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 m

POJ 1325 Machine Schedule (最小点覆盖 &amp;&amp; 二分图最大匹配)

<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">鏈接: http://poj.org/problem?id=1325</span> Description As we all know, machine scheduling is a very classical problem in computer science a