网络流男女配对问题java实现

package test;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;

public class ReadFromFile {
	public static boolean hasPath(int rGraph[][], int s, int t, int path[],int V) {
    	boolean visited[] = new boolean[V];
        Queue queue = new LinkedList();
        queue.add(s);
        visited[s] = true;
        //标准的BFS算法
        while(queue.size() > 0){
            int top = queue.poll();
            for(int i=0; i 0){
                    queue.add(i);
                    visited[i] = true;
                    path[i] = top;
                }
           }
        }
        return visited[t] == true;
    }
	
	 private static int maxFlow(int[][] graph,int s, int t,int V) {
	    	
	        int rGraph[][]=new int[V][V];
	        for(int i=0; i> readFileByLines(String filename) {
		File file=new File(filename);
		BufferedReader reader=null;
		ArrayList> list=new ArrayList>();
		
		System.out.println("已字节为单位读取内容,一次读一行:");
		try {
			reader=new BufferedReader(new FileReader(filename));
			String tempstring=null;
			int line=1;
			
			while ((tempstring=reader.readLine())!=null) {
				//System.out.println("line"+line+":"+tempstring);
				ArrayListlist2=new ArrayList();
				String[] str=null;
				str=tempstring.split(" ");
				for (int i = 0; i < str.length; i++) {
					list2.add(Integer.parseInt(str[i]));
				}
				list.add(list2);
				line++;
			}
			
			reader.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}

	public static void FindAllMaxFlow(ArrayList> list) {
		
		while(list.size()!=0){
			ArrayListlist1;
			list1=list.get(0);
			list.remove(0);
			int a=list1.get(0);
			int b=list1.get(1);
			int[][] temp=new int[a+b+2][a+b+2];
			for (int k=1;k<=a;k++){
				temp[0][k]=1;
			}
			for(int k=1;k<=b;k++)
				temp[a+k][a+b+2-1]=1;
			
			for (int k = 0; k < a; k++) {
				list1=list.get(0);
				for (int r = 0; r < list1.get(0); r++) {
					int m=list1.get(r+1);
					temp[1+k][a+m]=1;				
				}	
				list.remove(0);
			}
			int V=temp.length;
            System.out.println(maxFlow(temp, 0, V-1, V));
		}
		
	}
	
	public static void main(String[] args) {
        ArrayList>list=new ArrayList<>();
        list=readFileByLines("problem1.data");
		FindAllMaxFlow(list);
	}
}


你可能感兴趣的:(算法)