首页 > 代码库 > HDU 1150 最小点覆盖数

HDU 1150 最小点覆盖数

Machine Schedule

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5614    Accepted Submission(s): 2804


Problem 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
 
 
 
 
 
题目意思:
有两个机器A和B,有K个任务需要在机器上操作,A机器有a个模式,B机器有b个模式,任务在机器上操作时需要对应特定的模式才能操作,比如任务2在A机器上操作时需要3模式而在B机器上操作时需要2模式,可能多个任务在某个机器上对应同一模式,而如果两个任务不同模式时,做完前面任务再做这个任务时需要更换机器模式,每更换一次时间为1,
求任务全部完成用的最小时间。
 
 
思路:
时间只有模式更换时间,那么若想用的总时间最小,需要更换模式的次数最少,假设以A机器的a个模式为行,B机器的b个模式为列建a*b的图,在图上有k个点即对应该行该列的任务,有种操作是覆盖一行或一列,那么问题就转换成用最少的操作使得覆盖图上所有的点,很明显这个模型就是最小覆盖点数。在二分图里,最小覆盖点数=最大二分图匹配数。
 
 
代码:
 1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #include <vector> 5 #include <iostream> 6 using namespace std; 7 #define N 110 8  9 vector<int>ve[N];10 int from[N];11 int visited[N];12 int n, m, k;13 14 15 int mark(int u){16     int i, v;17     for(i=0;i<ve[u].size();i++){18         v=ve[u][i];19         if(!visited[v]){20             visited[v]=1;21             if(from[v]==-1||mark(from[v])){22                 from[v]=u;23                 return 1;24             }25         }26     }27     return 0;28 }29 main()30 {31     int i, j;32     while(scanf("%d",&n)==1&&n){33         scanf("%d %d",&m,&k);34         memset(from,-1,sizeof(from));35         for(i=0;i<=n;i++) ve[i].clear();36         int x, y, z;37         while(k--){38             scanf("%d %d %d",&z,&x,&y);39             ve[x].push_back(y);40         }41         int num=0;42         for(i=1;i<=n;i++){43             memset(visited,0,sizeof(visited));44             if(mark(i))45             num++;46         }47         printf("%d\n",num);48      }49 }