Follows 125.c (Total 67 lines):

/* @JUDGE_ID:4461XX 125 C */
/* A */
#include<stdio.h>
int map[35][35] , big ;
void initial( void )
{
	int i , j ;
	for( i=0 ; i<35 ; i++ )
		for( j=0 ; j<35 ; j++ ) map[i][j] = 0 ;
}
void warshall1( void )
{
	int i , j , k ;
	for( k=0 ; k<=big ; k++ )
		for( i=0 ; i<=big ; i++ )
			for( j=0 ; j<=big ; j++ )
				if( map[i][k] && map[k][j] )
					if( i==j ) map[i][j] = -1 ;
					else
						if( map[i][k]==-1 || map[k][j]==-1 )
							map[i][j] = -1 ;
						else map[i][j] += map[i][k]*map[k][j] ;
}
void warshall2( void )
{
	int i , j , k ;
	for( k=0 ; k<=big ; k++ )
		for( i=0 ; i<=big ; i++ )
			for( j=0 ; j<=big ; j++ )
				if( map[i][k] && map[k][j] )
					if( map[i][k]==-1 || map[k][j]==-1 )
						map[i][j] = -1 ;
}
void print( int time )
{
	int i , j ;
	printf( "matrix for city %d\n" , time ) ;
	for( i=0 ; i<=big ; i++ ){
		for( j=0 ; j<=big ; j++ ){
			printf( "%d" , map[i][j] ) ;
			if( j+1<=big )
				putchar( ' ' );
		}
		putchar( '\n' ) ;
	}
}
void main( void )
{
	int m , i , from , end , time ;
	for( time=0 ; ; time++ ){
		if( scanf( "%d" , &m ) != 1 ) break ;

		initial() ;

		for( big=i=0 ; i<m ; i++ ){
			scanf( "%d %d" , &from , &end ) ;
			map[from][end]++ ;
			if( from>big ) big = from ;
			if( end>big ) big = end ;
		}

		warshall1() ;
		warshall2() ;
		print( time ) ;
	}
}
/* @END_OF_SOURCE_CODE */

Back to statistics
Ya-Lin Huang (C)
huangyl@gmail.com