POJ 1789 Truck History

解题思路:prim算法求解最小生成树

  
    
#include < iostream >
using namespace std;

#define MaxNum 1e8

inline
int distance( char * a, char * b)
{
int t = 0 ;
for ( int i = 0 ; i < 7 ; i ++ )
if (a[i] != b[i])
t
++ ;
return t;
}
int main()
{
int n, min, start, tempS, totalDist;
char input[ 2001 ][ 8 ];
int dist[ 2001 ];
bool visited[ 2001 ];

while (cin >> n && n)
{
dist[
0 ] = 0 ;
start
= 0 ;

fill(
& dist[ 1 ], & dist[n], MaxNum);
memset(visited,
0 , sizeof ( bool ) * 2001 );

for ( int i = 0 ; i < n; i ++ )
cin
>> input[i];

while (start != - 1 )
{
visited[start]
= true ;
tempS
= - 1 ;
min
= MaxNum;

for ( int i = 0 ; i < n; i ++ )
{
if ( ! visited[i])
{
if (dist[i] != 1 )
{
int l = distance(input[start], input[i]);
if (dist[i] > l)
dist[i]
= l;
}
if (min > dist[i])
{
min
= dist[i];
tempS
= i;
}
}
}
start
= tempS;
}
totalDist
= 0 ;
for ( int i = 0 ; i < n; i ++ )
totalDist
+= dist[i];
cout
<< " The highest possible quality is 1/ " << totalDist << " . " << endl;
}
return 0 ;
}

 

你可能感兴趣的:(history)