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

    Truck History
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 17271   Accepted: 6621

    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.
    

    这道题就是一个最小生成树。每条字符串代表了一个类型的卡车,不同卡车之间有个演变的距离,我们define为两个字符串间相同位置上不同字母的个数和。可以把这个距离当作权值,
    卡车当作点,所求的Q 就是最少生成树。分析出来后就是一个最小生成树的水题。

     1 /*======================================================================
     2  *           Author :   kevin
     3  *         Filename :   TruckHistory.cpp
     4  *       Creat time :   2014-07-09 09:16
     5  *      Description :
     6 ========================================================================*/
     7 #include <iostream>
     8 #include <algorithm>
     9 #include <cstdio>
    10 #include <cstring>
    11 #include <queue>
    12 #include <cmath>
    13 #define clr(a,b) memset(a,b,sizeof(a))
    14 #define M 2014
    15 #define INF 0x7f7f7f7f
    16 using namespace std;
    17 int c[M][M],dis[M];
    18 char str[M][10];
    19 void BuildGrap(int n)
    20 {
    21     for(int i = 0; i < n; i++){
    22         for(int j = i+1; j < n; j++){
    23             int d = 0;
    24             for(int t = 0; t < 7; t++){
    25                 if(str[i][t] != str[j][t]){
    26                     d += 1;
    27                 }
    28             }
    29             c[i][j] = c[j][i] = d;
    30         }
    31     }
    32 }
    33 int prim(int n)
    34 {
    35     int i,j,k,sum = 0;
    36     bool vis[M];
    37     for(i = 0; i < n; i++){
    38         dis[i] = c[0][i];
    39         vis[i] = false;
    40     }
    41     vis[0] = true;
    42     for(i = 1; i < n; i++){
    43         int _min = INF;
    44         j = 0;
    45         for(k = 0; k < n; k++){
    46             if(_min > dis[k] && !vis[k]){
    47                 _min = dis[k];
    48                 j = k;
    49             }
    50         }
    51         vis[j] = true;
    52         sum += dis[j];
    53         for(k = 0; k < n; k++){
    54             if(dis[k] > c[j][k] && !vis[k]){
    55                 dis[k] = c[j][k];
    56             }
    57         }
    58     }
    59     return sum;
    60 }
    61 int main(int argc,char *argv[])
    62 {
    63     int n;
    64     while(scanf("%d",&n)!=EOF && n){
    65         clr(c,0);
    66         for(int i = 0; i < n; i++){
    67             getchar();
    68             scanf("%s",str[i]);
    69         }
    70         BuildGrap(n);
    71         int ans = prim(n);
    72         printf("The highest possible quality is 1/%d.
    ",ans);
    73     }
    74     return 0;
    75 }
    View Code
    Do one thing , and do it well !
  • 相关阅读:
    输入adb shell 时 提示error: more than one device and emulator
    增加 addDataScheme("file") 才能收到SD卡插拔事件的原因分析 -- 浅析android事件过滤策略
    Android音乐编程:管理音频焦点
    Android MediaScanner 详尽分析
    MediaScanner与音乐信息扫描==
    Vold工作流程分析学习
    [LeetCode] Majority Element II
    Android 四大组件学习之ContentProvider五
    HDU 1171 Big Event in HDU(01背包)
    Atom编辑器折腾记_(15)JS代码片段补全(插件:javascript-snippets)
  • 原文地址:https://www.cnblogs.com/ubuntu-kevin/p/3832969.html
Copyright © 2011-2022 走看看