POJ 1789题
//重点理解:关键抽象成最小生成树问题及二维字符串数组的使用
#include <stdio.h>
#include <string.h>
#define arraysize 2001
char truck[arraysize][8]; //定义字符串二维数组
int dis[arraysize][arraysize];
bool final[arraysize];
int d[arraysize];
int n;
int distance(char a[],char b[]) //两个字符串不同字符的个数为两个车号之间的距离
{
int num = 0; //别忘了初始化为0
for(int i=0;i<7;++i)
{
if(a[i]!=b[i])
num++;
}
return num;
}
void prim()
{
int i,j;
int v;
int min;
int sum = 0;
memset(final,0,sizeof(final));
for(i=1;i<n+1;++i)
{
d[i] = dis[1][i];
}
final[1] = true;
d[1] = 0;
for(i=1;i<n;++i)
{
min = 0x7fffffff;
for(j=1;j<n+1;j++)
{
if(!final[j] && d[j]<min)
{
v = j;
min = d[j];
}
}
sum += min;
final[v] = true;
for(j=1;j<n+1;++j)
{
if(!final[j] && d[j]>dis[v][j])
d[j] = dis[v][j];
}
}
printf("The highest possible quality is 1/%d.\n",sum);
}
int main()
{
//freopen("1.txt","r",stdin);
int i,j;
while(scanf("%d",&n)!=EOF)
{
if(n==0)
break;
for(i=1;i<n+1;++i)
{
scanf("%s",truck[i]); //二维字符串数组
}
for(i=1;i<n+1;++i)
{
for(j=1;j<n+1;++j)
{
dis[i][j] = distance(truck[i],truck[j]); //传递字符串
}
}
prim();
}
return 0;
}