POJ1789 Truck History "minimum spanning tree prim"

Source: Internet
Author: User

Truck history
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 18981 Accepted: 7321

Description

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

4aaaaaaabaaaaaaabaaaaaaabaaaa0

Sample Output

The highest possible quality is 1/3.

Source

field=source&key=ctu+open+2003 "style=" Text-decoration:none ">CTU Open 2003 read two times, Leng did not read, read the discussion of the interpretation of the area is clear. Test Instructions: The number of the given n points, denoted by a 7-bit length string, the distance between the two points is the number of different characters in the corresponding bit of their string, and the MST is obtained.

#include <stdio.h> #include <string.h> #define MAXN 2010#define MAXM MAXN * maxn#define inf 0x3f3f3f3fint head[ MAXN], N, id;struct Node {int V, W, next;} E[maxm];char map[maxn][8];int dis[maxn];bool vis[maxn];int caldis (int x, int y) {int sum = 0;for (int i = 0; i < 7; ++i) if (map[x][i]! = map[y][i]) ++sum;return sum;} void Addedge (int u, int v, int w) {e[id].v = v; E[ID].W = W; E[id].next = Head[u]; Head[u] = id++;} void Getmap () {memset (head,-1, sizeof (int) * n); int I, J, W; id = 0;for (i = 0; i < n; ++i) {scanf ("%s", Map[i]); for (j = 0; J < I; ++J) {w = Caldis (i, j); Addedge (I, J, W); Addedge (J, I, W);}}} int GetNext () {int I, pos =-1, w = inf;for (i = 0; i < n; ++i) if (!vis[i] && w > Dis[i]) {w = dis[i]; pos = i ;} return POS;} int Prim () {int i, u, V, sum = 0;for (i = 0; i < n; ++i) {vis[i] = 0; Dis[i] = inf;} Dis[u = 0] = 0;while (U! =-1) {vis[u] = 1; sum + = dis[u];for (i = head[u]; I! = 1; i = e[i].next) if (!vis[v = e[i].v] & & Dis[v] > E[I].W) dis[v] = E[i].w;u = GetNext ();} return sum;} void Solve () {printf ("the highest possible quality is 1/%d.\n", Prim ());} int main () {while (scanf ("%d", &n), N) {getmap (); Solve ();} return 0;}


Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.

POJ1789 Truck History "minimum spanning tree 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.