POJ 1325 && 1274:Machine Schedule 匈牙利算法模板题

Machine Schedule
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 12976   Accepted: 5529

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

题意是有两台机器,每个机器有若干个工作状态。有n个工作,每一个工作要求要么是在第一台机器的x状态下才能进行,要么是在第二台机器的y状态下才能进行。机器每切换一种状态要重启,问在给定工作的前提下,最少重启多少次。

二分图的最小点覆盖。发现做这种题最难的不是算法本身,什么匈牙利算法了。而是建图的那个过程,如何建图。这点还需要积累经验。

还有要注意的一点是两台机器的起始状态是0,所以有工作是要在状态0的时候的不用管它,一开始做它们就好了。

代码:

#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#pragma warning(disable:4996)
using namespace std;

int grid[805][805];
int link[805];
int visit[805];
int n,k,V1,V2;
int result;

bool dfs(int x)
{
	int i;
	for(i=0;i<V2;i++)
	{
		if(grid[x][i]==1&&visit[i]==0)
		{
			visit[i]=1;
			if(link[i]==-1||dfs(link[i]))
			{
				link[i]=x;
				return true;
			}
		}
	}
	return false;
}

void Magyarors()
{
	int i;
	
	result=0;
	memset(link,-1,sizeof(link));//!!这里不能是0

	for(i=0;i<V1;i++)
	{
		memset(visit,0,sizeof(visit));
		if(dfs(i))
			result++;
	}
	cout<<n-result/2<<endl;
}

int main()
{
	int i,j,temp1,temp2,temp3;
	while(scanf("%d",&n)!=EOF)
	{
		memset(grid,0,sizeof(grid));
		V1=V2=n;
		for(i=0;i<n;i++)
		{
			scanf("%d: (%d)",&temp1,&temp2);
			for(j=1;j<=temp2;j++)
			{
				scanf("%d",&temp3);
				grid[temp1][temp3]=1;
			}
		}
		Magyarors();
	}
	return 0;
}

与此题相类似的特别多,POJ1274 算一个。

代码:

#include <iostream>  
#include <algorithm>  
#include <cmath>  
#include <vector>  
#include <string>  
#include <cstring>  
#pragma warning(disable:4996)  
using namespace std;  

int grid[805][805];  
int link[805];  
int visit[805];  
int n,k,V1,V2;  
int result;  

bool dfs(int x)  
{  
	int i;  
	for(i=1;i<=V2;i++)  
	{  
		if(grid[x][i]==1&&visit[i]==0)  
		{  
			visit[i]=1;  
			if(link[i]==-1||dfs(link[i]))  
			{  
				link[i]=x;  
				return true;  
			}  
		}  
	}  
	return false;  
}  

void Magyarors()  
{  
	int i;  

	result=0;  
	memset(link,-1,sizeof(link));//!!这里不能是0  

	for(i=1;i<=V1;i++)  
	{  
		memset(visit,0,sizeof(visit));  
		if(dfs(i))  
			result++;  
	}  
	cout<<result<<endl;  
}  

int main()  
{  
	int i,j,temp,temp2,temp3;  
	while(scanf("%d %d",&V1,&V2)!=EOF)  
	{  
		memset(grid,0,sizeof(grid));

		for(i=1;i<=V1;i++)
		{
			scanf("%d",&temp);
			for(j=1;j<=temp;j++)  
			{  
				scanf("%d",&temp2);  
				grid[i][temp2]=1;  
			}
		}
		Magyarors();  
	}  
	return 0;  
}  




版权声明:本文为博主原创文章,未经博主允许不得转载。

posted on 2015-08-11 09:42  光速小子  阅读(215)  评论(0编辑  收藏  举报

导航