此题难点在于读题,明白题意后就是一个很简单的最小生成树
明白一点就行:将每一个卡车类型代码(truck type codes)作为一个结点,任意两个 卡车类型代码中 相同位置即字符数组a[8],b[8] 上 a[i]和b[i]为不同字符的位置的个数 做为
这两个结点之间的路径的权值。
如果没看懂这一点就要看代码:
#include <iostream>
using namespace std;
#define INF 0x7fffffff
#define SIZE 2002
char name[SIZE][8];
int graph[SIZE][SIZE];
bool visit[SIZE];
int dist[SIZE];
int n;
int cal_dis( char * a, char * b )
{
int i,dis = 0;
for ( i = 0; i < 7; i++ )
if ( a[i] != b[i] )
dis++;
return dis;
}
int Prim()
{
int i,k,temp;
int min_value,curr;
memset(visit,false,sizeof(visit));
for ( i = 1; i < n; i++ )
dist[i] = INF;
dist[0] = 0;
min_value = 0;
for ( k = 0; k < n; k++ )
{
temp = INF;
curr = -1;
for ( i = 0; i < n; i++ )
{
if ( dist[i] < temp && !visit[i] )
{
temp = dist[i];
curr = i;
}
}
if ( curr == -1 ) break;
min_value += temp;
visit[curr] = true;
for ( i = 0; i < n; i++ )
{
if ( !visit[i] && graph[curr][i] < dist[i] )
dist[i] = graph[curr][i];
}
}
return min_value;
}
int main()
{
int i,j;
while ( scanf("%d",&n) && n )
{
for ( i = 0; i < n; i++ )
scanf("%s",name[i]);
for ( i = 0; i < n; i++ )
for ( j = 0; j < n; j++ )
{
if ( i < j )
{
graph[i][j] = cal_dis( name[i], name[j] );
graph[j][i] = graph[i][j];
}
if ( i == j )
graph[i][j] = 0;
}
printf("The highest possible quality is 1/%d.\n",Prim());
}
return 0;
}