prim或kruscal都可以
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm>
#define INF 0x3f3f3f3f
using namespace std;
char g[2005][8];
int grid[2005][2005],dist[2005],visit[2005],n;
int dis(int i,int j)
{
int count=0,k;
for(k=0;k<7;k++)
if(g[i][k]!=g[j][k])
count++;
return count;
}
int prime()
{
int num=1,k,i,ans=0,minc;
memset (visit,0,sizeof(visit));
for (i = 1; i <= n; i++)
dist[i] = grid[1][i];
visit[1]=1;
dist[1]=0;
while(num++<n)
{
minc=INF;
for (i=1;i<=n;i++)
if(!visit[i] && dist[i]<minc)
minc=dist[k=i];
visit[k]=1;
ans+=dist[k];
for (i=1;i<=n;i++)
if(!visit[i])
dist[i]=min(dist[i],grid[k][i]);
}
return ans;
}
int main()
{
int i,j;
while ( scanf("%d",&n)!=EOF && n)
{
for(i=1;i<=n;i++)
scanf("%s",g[i]);
for (i=1;i<=n;i++)
for(j=i+1;j<=n;j++)
grid[i][j]=grid[j][i]=dis(i,j);
printf("The highest possible quality is 1/%d.\n",prime());
}
return 0;
}