zoukankan      html  css  js  c++  java
  • 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. " ,ans);
        }
        return 0;


    }





  • 相关阅读:
    window redis 安装配置
    mongodb下载及安装配置教程【仅供参考】
    合并对象
    JSON
    函数声明与函数表达式
    mongodb write 【摘自网上,只为记录,学习】
    javascript 操作cookie
    javascript 将多维数组转换为一维数组
    javascript 去除字符串中重复字符
    javascript 获取url参数
  • 原文地址:https://www.cnblogs.com/csnd/p/12062506.html
Copyright © 2011-2022 走看看