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.
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。