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 }
  • 相关阅读:
    c# datagridview 设置某行不可见解决办法
    MessageBox, MessageBoxBurttons, MessageBoxIcon 详细解析
    c# 项目带皮肤一起打包发布解决办法
    Winform DataGridView CheckBoxColumn c# 单选 解决方案
    机器学习 课程笔记
    机器学习-review-1 线性回归
    Office升级到2013版后无法登录微软账号问题
    Address already in use: make_sock: could not bind to address 0.0.0.0:80
    PHP的数组排序函数
    事件与委托例子
  • 原文地址:https://www.cnblogs.com/sdxk/p/4649501.html
Copyright © 2011-2022 走看看