C语言二分图匹配(2)___Machine Schedule(Hdu 1150)

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
大概题意:目前有K个任务,A,B两台机器,现在K个任务要全部完成(完成顺序不限),然而,第 i 个任务在A机器完成时 A机器的模式为 Ai, 如果在B机器完成时 B机器的模式为Bi,

每个机器模式转化需要代价1,已知初始两个机器的模式为0,问最小代价。


分析:可以把A,B两机器的模式看作是二分图的两部,每个任务建一条边.然后求出最小顶点覆盖数.           公式: 最小顶点覆盖数=最大匹配数.

(注意:因为初始状态为0,那么如果有个任务的Ai或者Bi 那么这个边不用建,因为不需要代价)


#include <cstdio>
#include <cstring>
#include <vector>
#include <iostream>
#include <queue>
#include <stdlib.h>
using namespace std;
int pre[105],visit[105];
vector<int>way[105];
int dfs(int step){
	int i,next;
	for(i=0;i<way[step].size();i++){
		next=way[step][i];
		if(visit[next])continue;
		visit[next]=1;
		if(pre[next]==-1 || dfs(pre[next])){
			pre[next]=step;
			return 1;
		}
	}
	return 0;
}
int main()
{
	int i,j,n,v,m,u,ans,k;
	while(cin>>n){
		for(i=0;i<105;i++)way[i].clear();
		if(n==0)break;
		cin>>m>>k;
		for(i=0;i<k;i++){
			cin>>j>>u>>v;
			if(v&&u)
			way[u].push_back(v);
		}
		memset(pre,-1,sizeof(pre));
		ans=0;
		for(i=0;i<n;i++){
			memset(visit,0,sizeof(visit));
			if(dfs(i)==1)ans++;
		}
		cout<<ans<<endl;
	} 
	return 0;
}




你可能感兴趣的:(C语言二分图匹配(2)___Machine Schedule(Hdu 1150))