杭电1150————最小顶点覆盖

Machine Schedule

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


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
最小顶点覆盖的概念是:
在二分图中选取最少的结点,这些结点关联的所有的边的数目是整个二分图的边的数目(点覆盖边)。
给出定义 最小顶点数目 = 最大匹配的数目。
证明如下 :
1.http://hi.baidu.com/keeponac/item/111e3438988c786b7d034b56
2.http://hi.baidu.com/keeponac/item/111e3438988c786b7d034b56(我当做结论记住了..看不懂..OTZ)
下面只解释为什么这一个题目是最小顶点覆盖的题目。
A机器共有n个模式 0..n-1
B机器共有m个模式 0..m-1
共有k条指令,每条指令可以由A或者B的某一种模式完成,但是每次想要更换模式,就得重新启动机器。
问:最少的重启次数。
问题的描述可以用下边这图片表示:
假设第k条命令可以被A的2模式或者B的k+1模式处理。那么处理完毕之后,A或者B机器得重启一次,换到下一个模式,像这样子。
红线表示第k+1条命令需要的模式。
假设用A处理了第k条命令,那么A重启到n-1模式,B此时还是在k+1模式。
这样不断的操作下去,其实可以想到,就是找最少的模式更换数目,把k条命令全部完成,把k命令看成是“边“,问题就抽象成了一个最小顶点覆盖的问题。
最小顶点 = 最大匹配数目
直接匈牙利算法搞定
<span style="font-family:Microsoft YaHei;font-size:14px;">#include <stdio.h>
#include <string.h>
#define maxn 105

int G[maxn][maxn];//G[i][j]表示A机器的i模式和B机器的j模式处理的命令一样 
int picked[maxn];//是否加入增广路 
int match[maxn];//是否被匹配 

int n,m,k;
void initialize()
{
	memset(G,0,sizeof(G));
	memset(match,0,sizeof(match));
}

bool find(int start)//从start开始找增广路径 
{ 
	for(int i = 0 ; i < m ; ++i)//B机器中 
	{
		if(!picked[i] && G[start][i])
		{
			picked[i] = 1;
			if(match[i] == 0 || find(match[i]))
			{
				match[i] = start;
				return true;
			}
		}
	} 
	return false;
}
void Hungary()//匈牙利算法
{
	int ans = 0;
	//为什么i从一开始呢?
	//A机器初始化为第一条命令的模式
	//B机器初始为第二条命令的模式
	//这就不需要计入重新启动的状态了~这个细节注意不然不AC 
	for(int i = 1 ; i < n ; ++i)//从A机器匹配
	{
		memset(picked,0,sizeof(picked));
		if(find(i))
			ans++;
	} 
	printf("%d\n",ans);
} 
int main()
{
	int jobs,a,b;
	
	while(scanf("%d",&n) != EOF && n)
	{
		scanf("%d%d",&m,&k);
		initialize();
		while(k--){
			scanf("%d%d%d",&jobs,&a,&b);
			G[a][b] = 1; 
		}
		Hungary();
	}
	return 0;
}</span>

PS:这个算法不难,理解了之后写出来还是正常难度吧。最难的是把问题抽象成这种算法,很可能根本想不到。这也是编程里最难的地方。忘记了哪个大牛说的,编程就是对现实生活的抽象。我很赞同,但是这种思维能力并不容易提升,我所知道的办法,就是最笨的办法,多看多练。对于我这种智商堪忧的人来说..只能如此了。



posted @ 2014-08-24 15:17  SixDayCoder  阅读(157)  评论(0编辑  收藏  举报