Truck History

Truck History
Time Limit: 2000MS  Memy Limit: 65536K 
Total Submissions: 9712  Accepted: 3579

Description


Advanced Cargo Movement, Ltd. uses trucks of different types. Some trucks are used f vegetable delivery, other f furniture, f 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 unimptant f this task). At the beginning of company""s histy, just a single truck type was used but later other types were derived it, then the new types another types were derived, so on. 

Today, ACM is rich enough to pay histians to study its histy. One thing histians 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 exactly one other truck type (except f the first truck type which was not derived any other type). The quality of a derivation plan was then defined as 
1/Σ(to,tdd(to,td

where the sum goes over all pairs of types in the derivation plan such that t o is the iginal type t d the type derived it d(t o,t d) is the distance of the types. 
Since histians 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


F 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.

题目大意是就是给出n个长度为7的字符串,每个字符串代表一个车,定义车的距离是两个字符串间不同字母的个数,题目要求的数是不同的车的距离的最小值,即所求的就是最小生成树

关于数据的输入和输出详见样例,要注意输出完数据后还有个""."",这题是一个稠密图,用Prim算法比较好。 
转载的~~~:

#include <stdio.h> 
#include <memory.h> 
#include <algorithm> 
#include <iostream> 
using namespace std; 
// 最大值 
#define INF 99999999 
// 最多的边数 
#define N 2000 
// 路径储存 
int road[N][N]; 
char type[N][8]; 
int main() 
{ 
    int n,m,count; 
    int i,j,k,l; 
    int mink,min,res; 
    while(true) { 
        scanf("%d",&n); 
        if(!n) 
            break; 
// 以下是将字符串转换为邻接矩阵 
        getchar(); 
        for(i = 0; i < n; i++) 
            gets(type[i]); 
        for(i = 0; i < n; i++) { 
            road[i][i] = -1; 
            for(j = i + 1; j < n;j++) { 
                count = 0; 
                for(k = 0; k < 7; k++) 
                    if(type[i][k] != type[j][k]) 
                        count++; 
                road[i][j] = road[j][i] = count; 
            } 
        } 
//Prim最小生成树 
        res = 0; 
        int lesscost[N]; 
        for(i = 0; i < n;i++) 
            lesscost[i] = road[0][i]; //初始化
        for( i = 1; i < n ;i++) { 
            min = INF; 
            for(j = 0; j < n; j++) 
                if(lesscost[j] >= 0 && lesscost[j] < min) { 
                    min = lesscost[j]; 
                    mink = j; 
                }    //lesscost中最小边位置mink
            res += min; 
// 将刚加入集合的节点的所有边和现有边做一个比较,保留小的边 
            for(j = 0; j < n;j++) 
                if(lesscost[j] > road[mink][j]) //比较lesscost中值与第mink行值,取较小值更新losscost
                    lesscost[j] = road[mink][j]; 
        } 
        printf("The highest possible quality is 1/%d.\n",res); 
    } 
}

aaaaaaa    -1 1 1 1
baaaaaa    1 -1 2 2
abaaaaa    1 2 -1 2
aabaaaa    1 2 2 -1


lesscost:
   -1 1 1 1

1              //①lesscost中最小边位置k=1   (初始为找到到点a距离最小的点k,后为使dlmin的k')
res= 1         //②更新res+=lesscost[k]      (加入边长d(a,k))
   -1 -1 1 1   //③比较lesscost中值与第k行值,取较小值更新losscost,再做①
  (记录剩下各点k'到边d(a,k)两端a,k距的最小距d(l,k')=min{d(a,k'),d(k,k')},存入dl)
2  -1 -1 -1 1  
3  -1 -1 -1 -1

res= 1 + 1 + 1


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值