poj 2239 Selecting Courses

确定用二分图算法,就找好对应关系,本题对应关系为一周的所有课节N=7*12,和每一科的对应。

 1 //1274,2239,2584,2536,2446
 2 //http://wenku.baidu.com/view/9962910590c69ec3d5bb75da.html    贪心最大二分匹配 
 3 //匈牙利树和增广轨 
 4 
 5 #include <cstdio>
 6 #include <cstring>
 7 
 8 int used[610]; //是否在覆盖点中
 9 
10 int nmap[610][610];
11 
12 int path[610];//前一个
13 
14 int P,N;
15 
16 int cross(int k)
17 {
18   
19   int i;
20   for(i=1;i<=N;i++)
21   {
22     if(!nmap[k][i] || used[i])  continue;
23 
24     used[i] = 1;
25     if(path[i] == -1 || cross(path[i]))
26     {
27       path[i] = k;
28       return 1;
29     }
30   }
31   return 0;
32 }
33 
34 int hungray()
35 {
36   int i;
37   int nret = 0;
38   memset(path,-1,sizeof(path));
39   for(i=1;i<=P;i++)
40   {
41     if(cross(i))  nret++;
42     memset(used,0,sizeof(used));
43   }
44   return nret;
45 }
46 
47 int main()
48 {
49   //freopen("in.txt","r",stdin);
50   //freopen("out.txt","w",stdout);
51   int tcase,i,j,a,b,c,z;
52   N = 12 * 7;
53   //scanf("%d",&tcase);
54   while(~scanf("%d",&P))
55   {
56     memset(nmap,0,sizeof(nmap));
57     for(i=1;i<=P;i++)
58     {
59       scanf("%d",&c);
60       for(j=0;j<c;j++)
61       {
62         scanf("%d%d",&a, &b);
63         z = (a-1)*12+b;
64         nmap[i][z] = 1;
65       }
66     }
67     printf("%d\n",hungray());
68   }
69   return 0;
70 }
posted on 2012-08-08 11:01  BFP  阅读(158)  评论(0编辑  收藏  举报