码迷,mamicode.com
首页 > 其他好文 > 详细

Truck History

时间:2015-03-08 15:33:58      阅读:203      评论:0      收藏:0      [点我收藏+]

标签:

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

Source

大意:最小生成树,即把所有定点连起来所需要的最短长度,第一行输入一个整数代表下面货车总数,第一行是源头,不能与其他比,下面n-1行是衍生出来的,把两组串不同的个数看作是路径,要你求从源头开始把所有串起来的最小生成树。
技术分享
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int inf = 0x3f3f3f3f,MAX = 3000;
int map[MAX][MAX],k[MAX];
int m,ans;
void prim (int map[MAX][MAX],int n){
    ans = 0;
    for(int i = 1; i <= n ; i++)
          k[i] = map[1][i];
    for(int i = 2; i <= n ; i++){
        int min1 = inf;
        m = 0;
       for(int j = 2; j <= n; j++){
              if(k[j] < min1&&k[j]!=0){
              min1 = k[j];
              m = j;
              }
       }
       //用来找最小的现有的边
       ans +=min1;
       k[m] = 0;//把拿出的边弄掉
       for(int j = 2; j <= n ; j++){
            if(map[m][j] < k[j] && k[j] !=0){
                    k[j] = map[m][j];//把以当前为顶点的到周围的最小的边拿出来
            }
         }
       }
       printf("The highest possible quality is 1/%d.\n",ans);
}
int main()
{
    int T,i,j;
    char s[2000][8];
    while(~scanf("%d",&T)&&T){
            memset(map,0,sizeof(map));
            memset(s,0,sizeof(s));
    for(int i = 1; i <= T;i++){
          map[i][i] = 0;
            scanf("%s",s[i]);
            for(int j = 1; j <= i;j++){
            int  d = 0;
               for(int k = 0; k < 7; k++){
                    if(s[i][k]!=s[j][k])
                    d++;
               }
            map[i][j] = map[j][i] = d;
          }
       }
      prim(map,T);
    }
    return 0;
}
View Code

最小生成树算法:一个大for两个小for有点像dijkstra算法,不过它的第一个小for是用来的得到以当前点为中心,向四周的最小的路径(其实也不准确,因为一旦该最短路径取出,那么以该最短路径的另一个顶点,来获得最小值),第二个小for就是用来比较该顶点出发的点与原来的点,如果比原来点小就拿进来当然要用if(k[i]!=0)因为两个小for之间已经把该m点变成0了,用ans来记录每一个情况的最小边,最后输出.

Truck History

标签:

原文地址:http://www.cnblogs.com/zero-begin/p/4321687.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!