题意:除了第一个车型外。其它的都是由另外的车型派生而来。用一个长度为7的字符串代表一种车型。
两种车型之间的distance为两个字符串不同字符的个数,从一种车型派生到还有一种车型的代价为它们之间的
distance,求n个车型之间派生的总代价最小为多少?
分析:这个题能够转化为最小生成树。每一个车型为图的顶点。两两之间的距离(即不同字符的个数)为权值
kruskal算法
#include<cstdio>
#include<algorithm>
using namespace std;
int f[2010],n,m;
struct stu
{
int a,b,c;
}t[2000000];
int cmp(struct stu x,struct stu y)
{
return x.c<y.c;
}
int find(int x)
{
if(x!=f[x])
f[x]=find(f[x]);
return f[x];
}
int krus()
{
int i,k=0,s=0,x,y;
for(i=1;i<m;i++){
x=find(t[i].a);
y=find(t[i].b);
if(x!=y){
s+=t[i].c;
k++;
if(k==n-1)
break;
f[x]=y;
}
}
return s;
}
int dis(char *s1,char *s2) //求两个字符串不同字符的个数
{
int i,d=0;
for(i=0;s1[i]!='