Poj1325 Machine Schedule

Poj1325 Machine Schedule 解题报告
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 13502   Accepted: 5766

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
首先声明我的程序是全poj第一快的O(∩_∩)O~,如下
Run ID User Problem Result Memory Time Language Code Length Submit Time
15157626 710395641 1325 Accepted 404K 0MS G++ 895B 2016-02-13 14:44:34

分析:

        二分图模板题。

        首先把A0...An-1放在左侧,再把B0...Bm-1放在右侧,如果一个任务是(i,x,y),那么就在Ax和By之间连一条边,连得这条边就代表了这个任务。每一条边代表的任务都可以在这条边的左端点或右端点上完成,为了完成所有的任务,我们需要选择一些点来覆盖所有的边,而题目要求最少的点数目,于是就转到了求二分图的最小点覆盖,由konig定理知二分图最小点覆盖=最大匹配,求出最大匹配就行了。

        有一个问题需要注意(查错查了一天),就是一个任务可能在0节点上完成,而我们知道机器初始时就在0节点,所以凡是能在0状态上完成的任务,我们就用0的花费让它在0节点上完成就行了,也就相当于这些任务和没有一样,因此只要碰到有0的情况,我们干脆跳过就好了。

代码(全poj最快哦):

//poj1325 Machine Schedule 二分图最小点覆盖 
#include <cstdio>
#include <cstring>
#define maxn 250
#define maxk 2500
using namespace std;
int n, m, k, head[maxn], next[maxk], to[maxk], tot, ans, link[maxn];
bool in[maxn];
void add(int a, int b)
{to[++tot]=b;next[tot]=head[a];head[a]=tot;}
bool dfs(const int x)
{
	int p, t;
	for(p=head[x];p;p=next[p])
	{
		if(!in[t=to[p]])
		{
			in[t]=true;
			if(link[t]==0||dfs(link[t]))
			{
				link[t]=x;link[x]=t;
				return true;
			}
		}
	}
	return false;
}
int main()
{
	int i, j , a, b, c;
	while(scanf("%d%d%d",&n,&m,&k),n)
	{
		tot=0;
		memset(head,0,sizeof(head));
		memset(next,0,sizeof(next));
		memset(link,0,sizeof(link));
		for(i=1;i<=k;i++)
		{
			scanf("%d%d%d",&a,&b,&c);
			if(b==0||c==0)continue;
			add(b,c+n);add(c+n,b);
		}
		ans=0;
		for(i=1;i<n;i++)
		{
			memset(in,0,sizeof(in));
			ans+=dfs(i);
		}
		printf("%d\n",ans);
	}
	return 0;
}


你可能感兴趣的:(Poj1325 Machine Schedule)