Floyd, seeking closure, template title poj3660

poj3660, that is, roughly, there has been some cows order, go to this order to expand the open.
Here closures, according to the existing order of cows, it is determined that all of the cows can be determined sequentially.
For example, we know that:
1 <2
2 <3
us closure operation, the three groups treated to obtain the inequality:
1 <2
2 <3
1 <3

The algorithm point of view, the size relationship between any two points, with floyd could not be better. According to the existing order of cows, all cows to determine the sequence can be determined, and finally determine whether a point, always and all the other points can have a size relationship, then he is available to determine the ranking.
code show as below

package coding;

import java.io.BufferedInputStream;
import java.util.Scanner;

public class poj3660 {
	static private  final int  inf= 0x3f3f3f;
	static int [][]map;
	static int n,m;
	static int  floyd() {
		
		//典型floyd , 咱这里求个闭包
		for(int i=1;i<=n;i++) {
			for(int j= 1;j<=n;j++) {
				for(int k=1;k<=n;k++) {
					if(map[i][j]==1&& map[k][i]==1) map[k][j]=1;
					if(map[i][j]==-1&& map[k][i]==-1) map[k][j]=-1;
				}
			}
		}		
		int res=0; //判断到现在可以确定名次的有谁
		for(int i =1;i<=n;i++) {
			int temp=0;
			for(int j=1;j<=n;j++) {
				if(map[i][j]==1 || map[i][j]== -1 ||map[i][j]==2) temp++;
			}
			if(temp==n) res++;
		}
		return res;
	}
	public static void main(String[] args) {
		Scanner sc = new Scanner (new BufferedInputStream(System.in));
		n = sc.nextInt();
		m = sc.nextInt();
		
		map = new int [n+2][n+2];
		for(int i=0;i<=n;i++)  map[i][i]=2;
	
		for(int i=0;i<m;i++) {
			int a  =  sc.nextInt();
			int b  =  sc.nextInt();
			map[a][b] =  1; 
			map[b][a] = -1;
		}
		System.out.println(floyd());
		
	}
}

Published 35 original articles · won praise 5 · Views 2385

Guess you like

Origin blog.csdn.net/qq_24884193/article/details/104374205