HDU1150:Machine Schedule

点击打开题目链接

Machine Schedule

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


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
 

Source
Asia 2002, Beijing (Mainland China)
 

Recommend
Ignatius.L
 


=====================================题目大意=====================================


现在有A和B两个机器,机器A有N种工作模式(编号从0至N-1),机器B有M种工作模式(编号从0至M-1),初始时A和B的工作模式都是0。

现在有K件任务,每件任务都可以通过机器A的某个工作模式或者机器B的某个工作模式来完成。

为了完成所有任务,需要不断得更换机器的工作模式,但不幸的是更换机器的工作模式只能通过重启机器来完成。

编程计算完成所有任务所需的最少机器重启次数。


=====================================算法分析=====================================


将机器A和B的工作模式分别作为集合U和集合V,可以用机器A的模式X和机器B的模式Y完成的任务作为连接集合U中的元素X和

集合V中的元素Y的边。

则本题显然就是求二分图的最小点覆盖数,根据二分图最大匹配的König定理“ 最小点覆盖数 = 最大匹配数 ”可知求集合U和集合

V的最大匹配数即可。

另外注意由于机器A和机器B的初始工作模式为0,所以可以用机器A的模式0或机器B的模式0完成的任务应该忽略。


=======================================代码========================================




#include<stdio.h>
#include<string.h>

int N,M,K,Linker[105];

bool Edge[105][105],Vis[105];

bool DFS(int U)
{
	for(int v=0;v<M;++v)
	{
		if(Edge[U][v]&&!Vis[v])
		{
			Vis[v]=true;
			if(Linker[v]==-1||DFS(Linker[v]))
			{
				Linker[v]=U;
				return true;
			}
		}
	}
	return false;
}

int Hungary()
{
	memset(Linker,-1,sizeof(Linker));
	int ans=0;
	for(int u=0;u<N;++u)
	{
		memset(Vis,0,sizeof(Vis));
		if(DFS(u)) { ++ans; }
	}
	return ans;
}

int main()
{
	while(scanf("%d",&N)==1&&N)
	{
		memset(Edge,0,sizeof(Edge));
		scanf("%d%d",&M,&K);
		while(K--)
		{
			int i,x,y;
			scanf("%d%d%d",&i,&x,&y);
			if(x&&y) { Edge[x][y]=true; }
		}
		printf("%d\n",Hungary());
	}
	return 0;
}

你可能感兴趣的:(图论,二分图)