POJ 1789:truck History "Prim"

Source: Internet
Author: User

Truck History Time Limit:4000/2000ms (Java/other) Memory limit:131072/65536k (Java/other) total submission (s): 123 Accepted Submission (s): 36Problem descriptionadvanced 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 Tois the original type and TDThe type derived from it and D (toTD) 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.

Inputthe 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.
Outputfor Each test case, your program should output the text "the highest possible quality is 1/q.", where 1/q is the qua Lity of the best derivation plan.
Sample Input
4aaaaaaabaaaaaaabaaaaaaabaaaa0

Sample Output
The highest possible quality is 1/3.


Ac-code:

#include <cstdio> #include <cstring>int n,s[2000][2000]; #define MAX 0x3f3f3f3fint distance (char A[8],char b [8]) {int di=0;for (int i=0;i<7;i++) if (A[i]!=b[i]) Di++;return di;} void Prim () {int I,j,k,vis[2000],dis[2000];memset (vis,0,sizeof (VIS)); for (i=1;i<n;i++) dis[i]=s[0][i];d is[0]=0; Vis[0]=1;for (j=1;j<n;j++) {int min=max;for (i=1;i<n;i++) if (!vis[i]&&dis[i]<min) {k=i;min=dis[i];} Vis[k]=1;for (i=1;i<n;i++) if (!vis[i]&&dis[i]>s[k][i]) dis[i]=s[k][i];} int sum=0;for (i=1;i<n;i++) sum+=dis[i];p rintf ("the highest possible quality is 1/%d.\n", sum);} int main () {int I,j;char str[2000][8];while (scanf ("%d", &n), N) {for (i=0;i<n;i++) for (j=i;j<n;j++) s[i][j]=s[ J][i]=max;for (i=0;i<n;i++) scanf ("%s", Str[i]); for (i=0;i<n;i++) for (j=i;j<n;j++) s[i][j]=s[j][i]=distance (Str[i],str[j]);p rim ();} return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

POJ 1789: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.