poj1789 Truck History

Source: Internet
Author: User

Truck history
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 20768 Accepted: 8045

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.


Code
The basic prim template has nothing to say, just the need to preprocess the string as an adjacency matrix

#include <stdio.h>
#include <string.h>
#include <iostream>
using namespace Std;
int map[2005][2005];
Char str[2005][7];
int vis[2005],dis[2005];
int n;
int prim (int u) {
int sum=0;
for (int i=1;i<=n;i++) {
Dis[i]=map[u][i];
}
Vis[u]=1;
for (int i=1;i<n;i++) {
int tmin=999999999;
int ans;
for (int j=1;j<=n;j++) {
if (Dis[j]<tmin&&!vis[j]) {
TMIN=DIS[J];
Ans=j;
}
}
Sum+=tmin;
Vis[ans]=1;
for (int k=1;k<=n;k++) {
if (Dis[k]>map[ans][k]&&!vis[k])
DIS[K]=MAP[ANS][K];
}
}
return sum;
}
int main () {

while (scanf ("%d", &n)!=eof) {
if (n==0)
Break

memset (map,0,sizeof (map));
memset (str,0,sizeof (str));
memset (dis,0,sizeof (dis));
memset (vis,0,sizeof (VIS));
GetChar ();
for (int i=1;i<=n;i++) {
scanf ("%s", Str[i]);
GetChar ();

}
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 (Str[i][k]!=str[j][k])
sum++;
}
Map[i][j]=sum;
Map[j][i]=sum;
}
}
printf ("The highest possible quality is 1/%d.\n", prim (1));
}
return 0;
}

poj1789 Truck History

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.