zoukankan      html  css  js  c++  java
  • POJ 1789 Truck History

    Truck History

    Time Limit: 2000ms
    Memory Limit: 65536KB
    This problem will be judged on PKU. Original ID: 1789
    64-bit integer IO format: %lld      Java class name: Main
     
     
    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.
    

    Source

     
    解题:最小生成树。任意两个串的距离是这两个串有多少个对应位置上的字符不相等。
     
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cmath>
     5 #include <algorithm>
     6 #include <climits>
     7 #include <vector>
     8 #include <queue>
     9 #include <cstdlib>
    10 #include <string>
    11 #include <set>
    12 #include <stack>
    13 #define LL long long
    14 #define pii pair<int,int>
    15 #define INF 0x3f3f3f3f
    16 using namespace std;
    17 int n,mp[2010][2010],d[2010];
    18 char s[2010][10];
    19 bool done[2010];
    20 priority_queue< pii,vector< pii >,greater< pii > >q;
    21 int calc(int i,int j){
    22     int tmp = 0;
    23     for(int k = 0; k < 7; k++)
    24         if(s[i][k] != s[j][k]) tmp++;
    25     return tmp;
    26 }
    27 int prim(){
    28     int ans = 0;
    29     for(int i = 0; i <= n; i++){
    30         done[i] = false;
    31         d[i] = INF;
    32     }
    33     while(!q.empty()) q.pop();
    34     d[1] = 0;
    35     q.push(make_pair(d[1],1));
    36     while(!q.empty()){
    37         int u = q.top().second;
    38         int w = q.top().first;
    39         q.pop();
    40         if(done[u]) continue;
    41         done[u] = true;
    42         ans += w;
    43         for(int i = 1; i <= n; i++){
    44             if(d[i] > mp[u][i]){
    45                 d[i] = mp[u][i];
    46                 q.push(make_pair(d[i],i));
    47             }
    48         }
    49     }
    50     return ans;
    51 }
    52 int main() {
    53     int i,j;
    54     while(scanf("%d",&n),n){
    55         for(i = 0; i <= n; i++){
    56             for(j = 0; j <= n; j++)
    57                 mp[i][j] = INF;
    58         }
    59         for(i = 1; i <= n; i++)
    60             scanf("%s",s[i]);
    61         for(i = 1; i <= n; i++){
    62             for(j = i+1; j <= n; j++)
    63                 mp[i][j] = mp[j][i] = calc(i,j);
    64         }
    65         printf("The highest possible quality is 1/%d.
    ",prim());
    66     }
    67     return 0;
    68 }
    View Code
  • 相关阅读:
    订单管理 练习
    简单的表单校验例子
    简单的js表单验证框架
    js的正则表达式
    Part2-HttpClient官方教程-Chapter2-连接管理
    Java爬取网易云音乐民谣并导入Excel分析
    Java爬取网易云音乐民谣并导入Excel分析
    Part2-HttpClient官方教程-Chapter1-基础
    Part2-HttpClient官方教程-Chapter1-基础
    Part1-HttpClient快速入门案例
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/3940796.html
Copyright © 2011-2022 走看看