0
点赞
收藏
分享

微信扫一扫

POJ1789简单小生成树

题意:
      给你一些车牌号,然后另一两个车牌号之间的权值就是这两个字符串之间相同位置不同字母的个数,然后求最小生成树。


思路:
      裸题,不解释了。

#include<stdio.h>

#include<algorithm>



using namespace std;



typedef struct

{

int a ,b ,c;

}EDGE;



EDGE edge[2000*2000/2+10];

int mer[2000+5];

char str[2000+5][10];





bool camp(EDGE a ,EDGE b)

{

return a.c < b.c;

}



int finds(int x)

{

return x == mer[x] ? x : mer[x] = finds(mer[x]);

}



int main ()

{

int n ,i ,j ,ans;

while(~scanf("%d" ,&n) && n)

{

for(i = 1 ;i <= n ;i ++)

scanf("%s" ,str[i]);

int nowid = 0;

for(i = 1 ;i <= n ;i ++)

{

for(j = i + 1 ;j <= n ;j ++)

{

++nowid;

edge[nowid].c = 0;

edge[nowid].a = i ,edge[nowid].b = j;

for(int k = 0 ;k < 7 ;k ++)

if(str[i][k] != str[j][k])

edge[nowid].c ++;

}

mer[i] = i;

}

sort(edge + 1 ,edge + nowid + 1 ,camp);

ans = 0;

for(i = 1 ;i <= nowid ;i ++)

{

int x = finds(edge[i].a);

int y = finds(edge[i].b);

if(x == y) continue;

ans += edge[i].c;

mer[x] = y;

}

printf("The highest possible quality is 1/%d.\n" ,ans);

}

return 0;



}








举报

相关推荐

0 条评论