poj 1325 Machine Schedule 最小点覆盖

时间:2021-04-28 05:56:56

题目链接: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 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种模式(0~n-1),B机器有m种模式(0~m-1),目前AB机器都处于模式0。给出k个工作,每个工作要么使机器A改变模式为i,要么使机器B改变模式为j。机器每次改变模式都需要重启一次,问k个工作完成之后,最少的重启次数。

算法分析:首先知道这是一个二分图。

给出这样一组数据:k=n=m=3

工作1可以改变A为1,改变B为2;工作2改变A为1,改变B为1;工作3改变A为1,改变B为0。

那么我们只需要一次重启就可以了:把A从0改变为1。

于是就想到了这道题可以用最小点覆盖来做。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<cstdlib>
 5 #include<cmath>
 6 #include<algorithm>
 7 #define inf 0x7fffffff
 8 using namespace std;
 9 const int maxn=100+10;
10 
11 int n,m,k;
12 int g[maxn][maxn],linker[maxn];
13 int vis[maxn];
14 
15 int dfs(int u)
16 {
17     for (int v=1 ;v<m ;v++) if (g[u][v])
18     {
19         if (!vis[v])
20         {
21             vis[v]=1;
22             if (linker[v]==-1 || dfs(linker[v]))
23             {
24                 linker[v]=u;
25                 return 1;
26             }
27         }
28     }
29     return 0;
30 }
31 
32 int hungary()
33 {
34     int ans=0;
35     for (int i=1 ;i<n ;i++)
36     {
37         memset(vis,0,sizeof(vis));
38         if (dfs(i)) ans++;
39     }
40     return ans;
41 }
42 
43 int main()
44 {
45     while (scanf("%d",&n)!=EOF && n)
46     {
47         scanf("%d%d",&m,&k);
48         memset(g,0,sizeof(g));
49         memset(linker,-1,sizeof(linker));
50         g[0][0]=1;
51         int j,a,b;
52         while (k--)
53         {
54             scanf("%d%d%d",&j,&a,&b);
55             g[a][b]=1;
56         }
57         printf("%d\n",hungary());
58     }
59     return 0;
60 }