Time Limit: 2000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u

Submit
Status

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.

Source

CTU Open 2003

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int map[1010][1010];
int vis[1010];
char str[1010][9];
int n;
int prim()
{
int sum=0;
int minn,k;
for(int i=1;i<n;i++)
{
minn=100000;
for(int j=2;j<=n;j++)
{
if(!vis[j]&&map[1][j]<minn)
{
minn=map[1][j];
k=j;
}
}
vis[k]=1;
sum+=minn;
for(int j=2;j<=n;j++)
{
if(!vis[j]&&map[k][j]<map[1][j])
map[1][j]=map[k][j];
}
}
return sum;
}
int main()
{
while(scanf("%d",&n),n)
{
memset(map,0,sizeof(map));
memset(vis,0,sizeof(vis));
memset(str,'\0',sizeof(str));
for(int i=1;i<=n;i++)
scanf("%s",str[i]);
for(int i=1;i<=n;i++)
{
for(int j=i+1;j<=n;j++)
{
for(int k=0;k<7;k++)
{
if(str[i][k]!=str[j][k])
map[i][j]++;
}
map[j][i]=map[i][j];
}
}
printf("The highest possible quality is 1/%d.\n",prim());
}
return 0;
}

poj--1789--Truck History(prim)的更多相关文章

  1. POJ 1789 -- Truck History(Prim)

     POJ 1789 -- Truck History Prim求分母的最小.即求最小生成树 #include<iostream> #include<cstring> #incl ...

  2. Kuskal/Prim POJ 1789 Truck History

    题目传送门 题意:给出n个长度为7的字符串,一个字符串到另一个的距离为不同的字符数,问所有连通的最小代价是多少 分析:Kuskal/Prim: 先用并查集做,简单好写,然而效率并不高,稠密图应该用Pr ...

  3. poj 1789 Truck History

    题目连接 http://poj.org/problem?id=1789 Truck History Description Advanced Cargo Movement, Ltd. uses tru ...

  4. poj 1789 Truck History 最小生成树 prim 难度:0

    Truck History Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 19122   Accepted: 7366 De ...

  5. POJ 1789 Truck History【最小生成树简单应用】

    链接: http://poj.org/problem?id=1789 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22010#probl ...

  6. POJ 1789 Truck History (Kruskal)

    题目链接:POJ 1789 Description Advanced Cargo Movement, Ltd. uses trucks of different types. Some trucks ...

  7. poj 1789 Truck History 最小生成树

    点击打开链接 Truck History Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 15235   Accepted:  ...

  8. POJ 1789 Truck History (最小生成树)

    Truck History 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/E Description Advanced Carg ...

  9. poj 1789 Truck History【最小生成树prime】

    Truck History Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 21518   Accepted: 8367 De ...

  10. POJ 1789 Truck History(Prim+邻接矩阵)

    ( ̄▽ ̄)" #include<iostream> #include<cstdio> #include<cstring> #include<algo ...

随机推荐

  1. HDU 4305 Contest 1

    感觉是有很多细节要处理的.尤其是求逆元后的运算,应该是存在超范围的情况的. #include <iostream> #include <cstdio> #include < ...

  2. BZOJ 2005 [Noi2010]能量採集 (容斥)

    [Noi2010]能量採集 Time Limit: 10 Sec  Memory Limit: 552 MB Submit: 2324  Solved: 1387 [id=2005"> ...

  3. 晋IT分享成长沙龙集锦

    第一期"晋IT"分享成长沙龙于2014年7月19日圆满结束.下面是相关内容整理和第二期预告. 各位伙伴认真的介绍自己,介绍自己的业务,分析自己眼下存在的问题,大家一起探讨,真诚出谋 ...

  4. Cordova 5 架构学习 Weinre远程调试技术

    手机上的页面不像桌面开发这么方便调试.能够使用Weinre进行远程调试以方便开发.本文介绍windows下的安装与使用. 安装 使用npm安装.能够执行: ###npm config set regi ...

  5. Bootstrap全局CSS样式之表单

    .form-control--将单独的表单控件赋予一些全局样式,如默认宽度width:100%. .form-group--包裹表单控件,获得最好的排列: .form-inline--将表单设置为内联 ...

  6. Checkmarx VisualStudio plugin installation process.

    1-      Configuration of plugin VSTudio Prerequisite: -Your visual studio MUST be up to date with th ...

  7. artTemplate的使用

    引言 腾讯的前端模板引擎,不逊于doT.js 1.引入 <script type="text/javascript" src="../plugin/artTempl ...

  8. 87.node.js操作mongoDB数据库示例分享

    转自:https://www.cnblogs.com/mracale/p/5845148.html 连接数据库   var mongo=require("mongodb"); va ...

  9. js小知识colspan和rowspan

    colspan和rowspan这两个属性用于合并表格的列或者行. colspan是"column  span"(跨列)的缩写,所以colspan属性用在td标签中,用来跨列合并单元 ...

  10. VP相关

    1.485模块电平,如果是集成的IC模块,则发送低电平,接收高电平: 2.阀门程序移植至PLC注意事项: 1) 阀门程序中的变量厘清,移植过程中阀门程序中的模块有些用到了,有些没用到,所以这是变量也很 ...