• 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;


    }





  • 相关阅读:
    html和css基础
    Chrome的插件使用
    04
    03
    MySQL的下载与安装(超完整)
    IDEA运行单元测试报错:java.lang.NoClassDefFoundError: org/hamcrest/SelfDescribing
    IDEA 快捷键合集
    IDEA + Spring的安装以及入门案例创建(超详细)
    Java NullPointerException异常的原因
    Eclipse 显示 错误:找不到或无法加载主类
  • 原文地址:https://www.cnblogs.com/csnd/p/12062506.html
Copyright © 2020-2023  润新知