zoukankan      html  css  js  c++  java
  • Truck History(poj 1789)

    Description

    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 to is the original type and td the type derived from it and d(to,td) 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.
    

       需要用普里母算法这种算法让我很是纠结,努力了一天才勉强搞定,很是伤自尊啊

      下面是我同学的代码

     1 #include<iostream>
     2 #include<cstdio>
     3 using namespace std;
     4 int grah[2100][2100];
     5 int sum=0;
     6 void pim(int n)//用Kruskal会超时
     7 {
     8     int i,j,pos;
     9     int min,v[2100]= {0},d[2100];
    10     for(i=1; i<=n; i++)//
    11         d[i]=grah[1][i];
    12     v[1]=1;
    13     for(i=2; i<=n; i++)
    14     {
    15         min=99999999;
    16         for(j=1; j<=n; j++)
    17         {
    18             if(!v[j]&&min>d[j])
    19             {
    20                 min=d[j];
    21                 pos=j;
    22             }
    23         }
    24         sum+=min;
    25         v[pos]=1;
    26         for(j=1; j<=n; j++)
    27         {
    28             if(!v[j])
    29             {
    30                 if(d[j]>grah[pos][j])
    31                     d[j]=grah[pos][j];
    32             }
    33         }
    34     }
    35 }
    36 int main()
    37 {
    38     int n,i,j,k,cou;
    39     char s[2100][80];
    40     while(cin>>n&&n)
    41     {
    42         getchar();
    43         for(i=1; i<=n; i++)
    44             cin>>s[i];
    45         for(i=1; i<=n; i++)
    46         {
    47             for(j=i+1; j<=n; j++)
    48             {
    49                 cou=0;
    50                 for(k=0; k<7; k++)
    51                 {
    52                     if(s[i][k]!=s[j][k])//如果有一个字母不同就+1
    53                         cou++;
    54                 }
    55                 grah[i][j]=grah[j][i]=cou;//标出权重
    56             }
    57             grah[i][i]=0;//自己到自己的距离为0;
    58         }
    59         pim(n);
    60         printf("The highest possible quality is 1/%d.
    ",sum);
    61         sum=0;
    62     }
    63     return 0;
    64 }
    View Code
  • 相关阅读:
    rabbitmq无用使用guest用户远程连接
    SpringMVC 过滤器
    Spring MVC的Controller统一异常处理:HandlerExceptionResolver
    springMVC之mvc:interceptors拦截器的用法
    spring中排除某个类
    mysql中的CURRENT_TIMESTAMP
    [QT]Qt+VS2012+Win8 64Bit安装
    UOJ#55. 【WC2014】紫荆花之恋
    CodeChef SADPAIRS:Chef and Sad Pairs
    BZOJ4771: 七彩树
  • 原文地址:https://www.cnblogs.com/kongkaikai/p/3256540.html
Copyright © 2011-2022 走看看