最坏情况的输入操作次数:20000 *200 = 4,000,000
为了写起来方便,直接FLOYD搞定,200^3 = 8, 000,000
[CODE]
#include <stdio.h>
#include <string.h>
const int M = 205 ;
struct CITY {
char name[35] ;
} city[M] ;
int value[M][M], nCount ;
int GetCityPos ( char p[] )
{
int i ;
for ( i = 0; i < nCount; i++ )
{
if ( strcmp ( p, city[i].name ) == 0 )
return i ;
}
strcpy ( city[nCount].name, p ) ;
nCount ++ ;
return nCount - 1 ;
}
int main()
{
int nCityNum, nPathNum, cases = 0 ;
while ( scanf ( "%d %d", &nCityNum, &nPathNum ) && nCityNum )
{
memset ( value, 0, sizeof(value) ) ;
char ACity[35], BCity[35] ;
nCount = 0 ;
int i, j, k, load ;
getchar () ;
for ( i = 0; i < nPathNum; i++ )
{
scanf ( "%s %s %d", ACity, BCity, &load ) ;
getchar() ;
int a = GetCityPos ( ACity ) ;
int b = GetCityPos ( BCity ) ;
value[a][b] = value[b][a] = load ;
}
for ( k = 0; k < nCityNum; k++ )
{
for ( i = 0; i < nCityNum; i++ )
{
for ( j = 0; j < nCityNum; j++ )
{
int temp = value[i][k] > value[k][j] ? value[k][j] : value[i][k] ;
if ( temp > value[i][j] )
value[i][j] = temp ;
}
}
}
scanf ( "%s %s", ACity, BCity ) ;
int a = GetCityPos ( ACity ) ;
int b = GetCityPos ( BCity ) ;
printf ( "Scenario #%d/n", ++cases ) ;
printf ( "%d tons/n/n", value[a][b] ) ;
}
return 0;
}
[/CODE]