Truck History prime算法

Problem Description
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.
 

Output
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 Input
4 aaaaaaa baaaaaa abaaaaa aabaaaa 0
 

Sample Output
The highest possible quality is 1/3.
***************************************************************************************************************************

1.将第一个点放入最小生成树的集合中(标记visit[i]=1意思就是最小生成树集合)。

2.从第二个点开始,初始化lowcost[i]为跟1点相连(仅仅相连)的边的权值(lowcost[i]不是这个点的最小权值!在以后会逐步更新)。

3.找最小权值的边。

从第二点开始遍历,如果不是最小生成树的集合的点,则找出从2到n的最小权值(lowcost[j])。

4.将找出来的最小权值的边的顶点加入最小生成树的集合中(标记visit[i] = 1),权值想加。

5.更新lowcost[j]集合。

假设第一次:lowcost[2]代表与1相连的点的权值,现在加入了k点。则比较k点与2点的边map[k][2]和lowcost[2]的大小,若lowcost[2]大,则lowcost[2] = map[k][2]。(关键步骤:实质就是每在最小生成树集合中加入一个点就需要把这个点与集合外的点比较,不断的寻找两个集合之间最小的边)

6.循环上述步骤,指导将全部顶点加入到最小生成树集合为止。

***************************************************************************************************************************

ContractedBlock.gif ExpandedBlockStart.gif
 1 #include<iostream>
 2 #include<string>
 3 #include<cmath>
 4 #include<cstring>
 5 #include<cstdio>
 6 #include<queue>
 7 using namespace std;
 8 int map[2010][2010];
 9 char  str[2010][10];
10 int vis[2010],n;
11 void init()
12 {
13     memset(map,0,sizeof(map));
14     for(int it=0;it<n;it++)
15      for(int jt=it+1;jt<n;jt++)
16       for(int kt=0;kt<7;kt++)
17         if(str[it][kt]!=str[jt][kt])
18          {
19              map[it][jt]++;
20              map[jt][it]++;
21          }
22 }
23 int prime()
24 {
25     int lowcost[2010],lowset[2010];
26     int i,j,k,sum=0;
27     memset(vis,0,sizeof(vis));
28     for(i=0;i<n;i++)//初始化
29     {
30         lowcost[i]=map[0][i];
31         lowset[i]=0;
32     }
33     vis[0]=1;
34     for(i=1;i<n;i++)
35     {
36         j=0;
37         while(vis[j])j++;
38         for(k=0;k<n;k++)//找到最短边
39         {
40             if(!vis[k]&&lowcost[k]<lowcost[j])j=k;
41         }
42         sum+=map[j][lowset[j]];
43         vis[j]=1;
44         for(k=0;k<n;k++)//更新最短边
45         {
46             if(!vis[k]&&lowcost[k]>map[j][k])
47             {
48                 lowcost[k]=map[j][k];
49                 lowset[k]=j;
50             }
51         }
52 
53     }
54     return sum;
55 }
56 int main()
57 {
58     int i,j,k;
59     while(scanf("%d",&n)&&n)
60     {
61         getchar();
62         for(i=0;i<n;i++)
63         {
64             scanf("%s",str[i]);
65         }
66         init();
67         printf("The highest possible quality is 1/%d.\n",prime());
68     }
69     return 0;
70 }
View Code

转载于:https://www.cnblogs.com/sdau--codeants/p/3378756.html

猜你喜欢

转载自blog.csdn.net/weixin_34232744/article/details/93432902