#include <iostream>
#include <queue>
#include <vector>
#include <string>
#define N 2002
#define inifite 0x7fffffff
using namespace std;
int edge[N][N],dist[N],flag[N];
char type[N][8];
int numOfTypes;
int getDis(char a[],char b[])
{
int count=0;
for (int i=0;i<7;i++)
{
if(a[i]!=b[i])
++count;
}
return count;
}
void initial()
{
for (int i=0;i<=numOfTypes;++i)
dist[i]=inifite;
dist[1]=0;
memset(flag,0,sizeof(flag));
flag[1]=1;
}
int prim()
{
initial();
int min=inifite,minIndex=1,sum=0;
for (int i=2;i<=numOfTypes;i++)
dist[i]=getDis(type[1],type[i]);
for(int k=1;k<=numOfTypes-1;k++)
{
min=inifite;//每一次都是重新找
minIndex=1;
for (int j=1;j<=numOfTypes;j++)
{
if(!flag[j]&&dist[j]<min)
{
minIndex=j;
min=dist[j];//找最小的时候,虽然用的是下标,但是也要替换啊,这个地方注意一下
}
}
flag[minIndex]=1;
sum=sum+dist[minIndex];
for (int i=1;i<=numOfTypes;i++)
{
if(!flag[i]&&dist[i]>getDis(type[minIndex],type[i]))
dist[i]=getDis(type[minIndex],type[i]);
}
}
return sum;
}
int main()
{
//freopen("in.txt","r",stdin);
while (cin >> numOfTypes && numOfTypes)
{
//there is a blank "enter".It has been read and put in type[N].Attention
int j=1;//therefore j=0;type[0]="";
while (j<=numOfTypes)
scanf("%s",type[j++]);
initial();
int sum=prim();
cout << "The highest possible quality is 1/" << sum << "." <<endl;
}
}
poj1789之Prim
最新推荐文章于 2023-02-20 09:09:49 发布