ZJU 1952 Heavy Cargo

最壞情況的輸入操作次數: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]

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章