zoukankan      html  css  js  c++  java
  • Truck History

     
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 20884   Accepted: 8075

    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 <algorithm>
     3 using namespace std;
     4 #define INF 0x0fffffff;
     5 char str[2000][7];
     6 int map[2000][2000];
     7 int vis[2000];
     8 int d[2000];
     9 int n;
    10 int Prim(){
    11     for(int i=0;i<n;i++){
    12         d[i]=INF;
    13         vis[i]=0;
    14     }
    15     d[0]=0;
    16     int sum=0;
    17     for(int i=0;i<n;i++){
    18         int mi=INF;
    19         int p;
    20         for(int j=0;j<n;j++){
    21             if(!vis[j]&&mi>d[j]){
    22                 mi=d[j];
    23                 p=j;
    24             }
    25         }
    26         sum+=mi;
    27         vis[p]=1;
    28         for(int j=0;j<n;j++){
    29             if(!vis[j]){
    30                 d[j]= min(d[j],map[p][j]);
    31             }
    32         }
    33     }
    34     return sum;
    35 }
    36 int main() {
    37     cin>>n;
    38     while(n){
    39     for(int i=0;i<n;i++){
    40         for(int j=0;j<7;j++){
    41             cin>>str[i][j];
    42         }
    43     }
    44     for(int i=0;i<n;i++){
    45         for(int j=i+1;j<n;j++){
    46             int sum=0;
    47             for(int k=0;k<7;k++){
    48                 if(str[i][k]!=str[j][k]){
    49                     sum++;
    50                 }
    51             }
    52             map[i][j]=map[j][i]=sum;
    53         }
    54     }
    55     int sum=Prim();
    56     cout<<"The highest possible quality is 1/"<<sum<<"."<<endl;
    57     cin>>n;
    58     }
    59     return 0;
    60 }
  • 相关阅读:
    VS2010版快捷键
    Win7旗舰版中的IIS配置asp.net的运行环境
    实现软件自动在线升级的原理
    view_countInfo
    C#尝试读取或写入受保护的内存。这通常指示其他内存已损坏。
    error: 40
    SQL Server 2008 阻止保存要求重新创建表的更改问题的设置方法
    继承实现圆柱体面积体积的计算
    圆柱模板价格计算器V1.0版本
    python3.7内置函数整理笔记
  • 原文地址:https://www.cnblogs.com/sdxk/p/4649501.html
Copyright © 2011-2022 走看看