首页 > 专题系列 > Java解POJ > POJ 2239 Selecting Courses [解题报告] Java
2013
11-10

POJ 2239 Selecting Courses [解题报告] Java

Selecting Courses

问题描述 :

It is well known that it is not easy to select courses in the college, for there is usually conflict among the time of the courses. Li Ming is a student who loves study every much, and at the beginning of each term, he always wants to select courses as more as possible. Of course there should be no conflict among the courses he selects.

There are 12 classes every day, and 7 days every week. There are hundreds of courses in the college, and teaching a course needs one class each week. To give students more convenience, though teaching a course needs only one class, a course will be taught several times in a week. For example, a course may be taught both at the 7-th class on Tuesday and 12-th class on Wednesday, you should assume that there is no difference between the two classes, and that students can select any class to go. At the different weeks, a student can even go to different class as his wish. Because there are so many courses in the college, selecting courses is not an easy job for Li Ming. As his good friends, can you help him?

输入:

The input contains several cases. For each case, the first line contains an integer n (1 <= n <= 300), the number of courses in Li Ming's college. The following n lines represent n different courses. In each line, the first number is an integer t (1 <= t <= 7*12), the different time when students can go to study the course. Then come t pairs of integers p (1 <= p <= 7) and q (1 <= q <= 12), which mean that the course will be taught at the q-th class on the p-th day of a week.

输出:

For each test case, output one integer, which is the maximum number of courses Li Ming can select.

样例输入:

5
1 1 1
2 1 1 2 2
1 2 2
2 3 2 3 3
1 3 3

样例输出:

4

解题代码:

//* @author: 
import java.io.*;
import java.util.*;

public class Main{
 static boolean[][] g=new boolean[305][100];
 static int[] xm=new int[305];
 static int[] ym=new int[100];
 static boolean[] chk=new boolean[100];
 static int un,vn;
 public static boolean search(int u){
   int v;
   for(v=0;v< vn;v++){
    if(g[u][v]&&!chk[v]){
	chk[v]=true;
	if(ym[v]==-1||search(ym[v])){
	  ym[v]=u;
	  xm[u]=v;
	  return true;
	}			
     }
   }
  return false;
 }

public static int match(){
 int u,ans=0;
 Arrays.fill(xm, -1);
 Arrays.fill(ym, -1);
 for(u=0;u< un;u++)
   if(xm[u]==-1){
    Arrays.fill(chk, false);
    if(search(u)) ans++;
    }
  return ans;
 }

 public static void main(String[] args){
  Scanner cin=new Scanner(new BufferedInputStream(System.in));
  while(cin.hasNext()){
   int n=cin.nextInt();
   un=n;
   vn=84;
   for(int i=0;i< g.length;i++)
	Arrays.fill(g[i], false);
   for(int i=0;i< n;i++){
	int t=cin.nextInt();
	for(int j=0;j< t;j++){
        int p=cin.nextInt();
	 int q=cin.nextInt();
	 g[i][(p-1)*12+q-1]=true;
	}
    }
    System.out.println(match());
  }
 }
}

  1. 如果两个序列的最后字符不匹配(即X [M-1]!= Y [N-1])
    L(X [0 .. M-1],Y [0 .. N-1])= MAX(L(X [0 .. M-2],Y [0 .. N-1]),L(X [0 .. M-1],Y [0 .. N-1])
    这里写错了吧。

  2. Excellent Web-site! I required to ask if I might webpages and use a component of the net web website and use a number of factors for just about any faculty process. Please notify me through email regardless of whether that would be excellent. Many thanks

  3. 算法是程序的灵魂,算法分简单和复杂,如果不搞大数据类,程序员了解一下简单点的算法也是可以的,但是会算法的一定要会编程才行,程序员不一定要会算法,利于自己项目需要的可以简单了解。