Truck History(poj 1789)

Descriptionios

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 t o is the original type and t d the type derived from it and d(t o,t d) 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.

Outputide

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 Inputthis

4
aaaaaaa
baaaaaa
abaaaaa
aabaaaa
0

Sample Outputspa

The highest possible quality is 1/3.

 

   須要用普里母算法這種算法讓我非常糾結,努力了一天才勉強搞定,非常傷自尊啊blog

  下面是我同窗的代碼ip

 1 #include<iostream>
 2 #include<cstdio>
 3 using namespace std;
 4 int grah[2100][2100];
 5 int sum=0;
 6 void pim(int n)//用Kruskal會超時
 7 {
 8     int i,j,pos;
 9     int min,v[2100]= {0},d[2100];
10     for(i=1; i<=n; i++)//
11         d[i]=grah[1][i];
12     v[1]=1;
13     for(i=2; i<=n; i++)
14     {
15         min=99999999;
16         for(j=1; j<=n; j++)
17         {
18             if(!v[j]&&min>d[j])
19             {
20                 min=d[j];
21                 pos=j;
22             }
23         }
24         sum+=min;
25         v[pos]=1;
26         for(j=1; j<=n; j++)
27         {
28             if(!v[j])
29             {
30                 if(d[j]>grah[pos][j])
31                     d[j]=grah[pos][j];
32             }
33         }
34     }
35 }
36 int main()
37 {
38     int n,i,j,k,cou;
39     char s[2100][80];
40     while(cin>>n&&n)
41     {
42         getchar();
43         for(i=1; i<=n; i++)
44             cin>>s[i];
45         for(i=1; i<=n; i++)
46         {
47             for(j=i+1; j<=n; j++)
48             {
49                 cou=0;
50                 for(k=0; k<7; k++)
51                 {
52                     if(s[i][k]!=s[j][k])//若是有一個字母不一樣就+1
53                         cou++;
54                 }
55                 grah[i][j]=grah[j][i]=cou;//標出權重
56             }
57             grah[i][i]=0;//本身到本身的距離爲0;
58         }
59         pim(n);
60         printf("The highest possible quality is 1/%d.\n",sum);
61         sum=0;
62     }
63     return 0;
64 }
View Code
相關文章
相關標籤/搜索