zoukankan      html  css  js  c++  java
  • POJ 1789 Truck History (Kruskal 最小生成树)

    Advanced Cargo Movement, Ltd. uses trucks of different types. Some trucks are used for vegetable delivery, other for furniture, or for bricks. The company has its own code describing each type of a truck. The code is simply a string of exactly seven lowercase letters (each letter on each position has a very special meaning but that is unimportant for this task). At the beginning of company's history, just a single truck type was used but later other types were derived from it, then from the new types another types were derived, and so on. 

    Today, ACM is rich enough to pay historians to study its history. One thing historians tried to find out is so called derivation plan -- i.e. how the truck types were derived. They defined the distance of truck types as the number of positions with different letters in truck type codes. They also assumed that each truck type was derived from exactly one other truck type (except for the first truck type which was not derived from any other type). The quality of a derivation plan was then defined as 
    1/Σ(to,td)d(to,td)

    where the sum goes over all pairs of types in the derivation plan such that t o is the original type and t d the type derived from it and d(t o,t d) is the distance of the types. 
    Since historians failed, you are to write a program to help them. Given the codes of truck types, your program should find the highest possible quality of a derivation plan. 

    Input

    The input consists of several test cases. Each test case begins with a line containing the number of truck types, N, 2 <= N <= 2 000. Each of the following N lines of input contains one truck type code (a string of seven lowercase letters). You may assume that the codes uniquely describe the trucks, i.e., no two of these N lines are the same. The input is terminated with zero at the place of number of truck types.

    Output

    For each test case, your program should output the text "The highest possible quality is 1/Q.", where 1/Q is the quality of the best derivation plan.

    Sample Input

    4
    aaaaaaa
    baaaaaa
    abaaaaa
    aabaaaa
    0
    

    Sample Output

    The highest possible quality is 1/3.

    意思是给出n个长度相同的字符串,一个字符串代表一个点,每两个字符串有多少个字符不同,则不同的个数即为两点之间的距离,要求各个点都连通求quality的最大值
     1 #include <cstdio>
     2 #include <cstring>
     3 #include <algorithm>
     4 using namespace std;
     5 int const MAX=2005;
     6 int fa[MAX];
     7 char s[MAX][10];
     8 int n,m;
     9 struct Edge
    10 {
    11     int u,v,w;
    12 }e[MAX*MAX/2];
    13 int cmp(Edge a,Edge b)
    14 {
    15     return a.w<b.w;
    16 }
    17 void UF_set()
    18 {
    19     for(int i=0;i<MAX;i++)
    20         fa[i]=i;
    21 }
    22 int Find(int x)
    23 {
    24     return x==fa[x]?x:fa[x]=Find(fa[x]);
    25 }
    26 void Union(int a, int b)
    27 {
    28     int r1=Find(a);
    29     int r2=Find(b);
    30     if(r1!=r2)
    31         fa[r2] = r1;
    32 }
    33 void get_map()
    34 {
    35     for(int i=0;i<n;i++){
    36         for(int j=i+1;j<n;j++){
    37             int cnt = 0;
    38             for(int k=0;k<7;k++)
    39                 cnt+=(s[i][k]!=s[j][k]);
    40             e[m].u = i;
    41             e[m].v = j;
    42             e[m++].w = cnt;
    43         }
    44     }
    45 }
    46 int Kruskal()
    47 {
    48     int num=0,sum=0;
    49     UF_set();
    50     for(int i = 0; i < m; i++){
    51         int u = e[i].u;
    52         int v = e[i].v;
    53         if(Find(u) != Find(v)){
    54             Union(u, v);
    55             sum += e[i].w;
    56             num++;
    57         }
    58         if(num>=n-1) break;
    59     }
    60     return sum;
    61 }
    62 int main()
    63 {
    64     while(scanf("%d",&n)!=EOF&&n){
    65         for(int i=0;i<n;i++)
    66             scanf("%s",s[i]);
    67         m=0;
    68         get_map();
    69         sort(e,e+m,cmp);
    70         printf("The highest possible quality is 1/%d.
    ",Kruskal());
    71     }
    72 }
  • 相关阅读:
    Windows中一个22年的漏洞
    关于口令强度等级的设计
    360招聘网络安全攻防技术研究员
    法国出售给阿联酋的卫星可能含有后门组件
    2013年极客范最受欢迎的10篇博文
    利用WPS 2012/2013 0day针对中国政府部门的定向攻击
    Kingsoft Office Writer 2012 8.1.0.3385
    破解Google Gmail的https新思路
    设计自己的密码规则,实现强壮的自我保护
    电信级的RSA加密后的密码的破解方法
  • 原文地址:https://www.cnblogs.com/wydxry/p/7299244.html
Copyright © 2011-2022 走看看