POJ 1325 Machine Schedule

首先构造二部图,把A的n个model和B的n个model看作图的顶点,如果某个任务可以在A上的_imodel或者B上的_jmodel上完成,则Ai,Bj链接一条线,建立一个关系。那么本体就转化为求二部图的最小点覆盖集问题了

View Code
 1 #include<iostream>
 2 using namespace std;
 3 #define MAXN 105 
 4 int n1,n2;
 5 int hn;
 6 int g[MAXN][MAXN];
 7 int ans;
 8 int sx[MAXN],sy[MAXN];
 9 int cx[MAXN],cy[MAXN];
10 
11 int path(int u)
12 {
13     sx[u]=1;
14     for(int i=1;i<=n2;i++)
15     {
16             if(g[u][i]>0&&!sy[i])
17             {
18                     sy[i]=1;
19                     if(!cy[i]||path(cy[i]))
20                     {
21                             cx[u]=i;
22                             cy[i]=u;
23                             return 1;    
24                     }    
25             }    
26     }
27     
28     return 0;    
29 }
30 
31 
32 
33 int main()
34 {
35     int a,b,c;
36     while(scanf("%d",&n1))
37     {
38             if(n1==0)
39             break;
40             scanf("%d%d",&n2,&hn);
41             memset(g,0,sizeof(g));
42             
43             for(int i=0;i<hn;i++)
44             {
45                     scanf("%d%d%d",&a,&b,&c);
46                     g[b][c]=1;    
47             }
48             ans=0;
49             memset(cx,0,sizeof(cx));
50             memset(cy,0,sizeof(cy));
51             for(int i=1;i<=n1;i++)
52             {
53                 if(!cx[i])
54                 {
55                     memset(sx,0,sizeof(sx));
56                     memset(sy,0,sizeof(sy));
57                     ans+=path(i);    
58                 }    
59             }
60             
61             printf("%d\n",ans);    
62     }
63         return 0;    
64 }

 

posted on 2012-08-13 23:38  我的ACM之路  阅读(190)  评论(0编辑  收藏  举报

导航