void-man

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

题目要求,一个任务可以由两台机器的各一种模式完成,每种机器的同时只能在一种模式下切换,并且每次切换都需要重启机器!合理安排任务的序列,问最后最少需要重启几次

 

我们可以建图,把两个机器的每种模式看作是点,任务是边,连接两台机器上的可以执行的模式,要求的就是用最少的点可以覆盖所有的边,即最小覆盖。

由定理得知,最小覆盖=最大匹配数,然后用匈牙利算法求出最大匹配数即可.

 1 #include <stdio.h>
2 #include <string.h>
3 #define N 201
4 int match[N],map[N][N];
5 int cn,mn,k;
6 bool used[N];
7 int find(int x)
8 {
9 for(int i=1;i<mn;i++)
10 if(!used[i]&&map[x][i])
11 {
12 used[i]=true;
13 if(match[i]==-1||find(match[i]))
14 {
15 match[i]=x;
16 return true;
17 }
18
19 }
20 return false;
21 }
22
23 int Hungry()
24 {
25 int sum=0;
26 for(int i=1;i<cn;i++)
27 {
28 memset(used,false,sizeof(used));
29 if(find(i))sum++;
30 }
31 return sum;
32 }
33
34 int main()
35 {
36 int num,st,x,y;
37 while(~scanf("%d%",&cn)&&cn)
38 {
39 scanf("%d%d",&mn,&k);
40 memset(map,0,sizeof(map));
41 memset(match,-1,sizeof(match));
42 for(int i=1;i<=k;i++)
43 {
44 scanf("%d%d%d",&num,&x,&y);
45 if(x*y!=0)map[x][y]=1;
46
47 }
48 printf("%d\n",Hungry());
49
50 }
51
52 }
posted on 2011-08-28 16:15  void-man  阅读(171)  评论(0)    收藏  举报