POJ1789 Truck History "Prim"

Source: Internet
Author: User

Truck HistoryTime limit:2000ms Memory Limit:65536ktotal submissions:19456Accepted:7498description

Advanced Cargo Movement, Ltd. uses trucks of different types. Some trucks is used for vegetable delivery, and for furniture, or for bricks. The company have its own code describing each type of a truck. The code is simply a string of exactly seven lowercase letters (all letter in each position have a very special meaning bu T is the unimportant for this task). At the beginning of company's history, just a single truck type is used but later other types were derived from it, then From the new types another types were derived, and so on.

Today, ACM is a rich enough to pay historians to study it history. One thing historians tried to find out are 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 is derived from exactly one other truck type (except for the first truck type whic H is not derived from any other type). The quality of a derivation plan was and then defined as
1/σ (TO,TD) d (TO,TD)

Where the sum goes over all pairs of types in the derivation plan such, that's the original type and TD the type derive D from it and D (TO,TD) is the distance of the types.
Since historians failed, you is to the write a program to the 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). Assume that the codes uniquely describe the trucks, i.e., No. Of these N lines is 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 was 1/q.", where 1/q is the quality O f the best derivation plan.


Sample Input

4
Aaaaaaa
Baaaaaa
Abaaaaa
Aabaaaa
0


Sample Output

The highest possible quality is 1/3.


Source

CTU Open 2003


To give you a string of n, each string represents a node, and the distance between each node is the string

The number of different characters. For example: "abaaaaa" and "aabaaaa", the second and third characters are different, two nodes

The distance between is 2. And so on, get all the nodes. The minimum spanning tree for all nodes to form the graph.

Ideas: According to test instructions calculate the distance between the nodes, deposited in the diagram, with the prim algorithm to solve, pay attention to the output format.


#include <iostream> #include <algorithm> #include <cstdio> #include <cstring>using namespace    Std;int Map[2010][2010];char map[2010][8];int low[2010],vis[2010];int Prim (int N) {memset (vis,0,sizeof (VIS));    int ans = 0;    VIS[1] = 1;    int pos = 1;    for (int i = 1; I <= N; ++i) if (i! = pos) Low[i] = Map[pos][i];        for (int i = 1; i < N; ++i) {int Min = 0XFFFFFF0;  for (int j = 1; j <= N; ++j) {if (Min > Low[j] &&!vis[j]) {min                 = Low[j];            pos = j;        }} ans + = Min;        Vis[pos] = 1;                for (int j = 1; j <= N; ++j) {if (!vis[j] && map[pos][j] < Low[j]) {            LOW[J] = Map[pos][j]; }}} return ans;}    void Getmap (int N) {memset (map,0xffffff0,sizeof (MAP));       for (int i = 1, i <= N; ++i) {for (int j = i; j <= N; ++j) {     int sum = 0;            for (int k = 0; k < 7; ++k) {if (map[i][k]! = map[j][k]) sum++;            } Map[i][j] = map[j][i] = sum;        if (i = = j) Map[i][j] = 0;    }}}int Main () {int N;        while (~SCANF ("%d", &n) && N) {memset (map,0,sizeof (MAP));        for (int i = 1; I <= N; ++i) scanf ("%s", Map[i]);        Getmap (N);        int ans = Prim (N);    printf ("The highest possible quality is 1/%d.\n", ans); } return 0;}


POJ1789 Truck History "Prim"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.