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. ZOJ 2705

    这题,找找规律,可以发现一个斐波那契数列.按照斐波那契数列求和,知道, SUM=Fn+2-F1,于是,该长度为Fn+2的倍数.因为斐波那契数列不一定是从1开始的,而从2开始的每个数都是从1开始的倍数. ...

  2. Mosquito的优化——epoll优化(七)

    本文由逍遥子撰写,转发请标注原址: http://blog.csdn.net/houjixin/article/details/46413583 或 http://houjixin.blog.163. ...

  3. lscript.ld 链接器脚本

    sumary选项卡 lscript.ld是这个应用程序的链接器脚本. 这是实用的作为一个报告 看看内存是针相应用程序. 它也能够被编辑以改变应用程序的位置.双击Hello_Zynqàsrcà lscr ...

  4. Linux 设备文件的创建和mdev

    引子 本文是嵌入式企鹅圈开篇--<linux字符设备驱动剖析>的姐妹篇,在上述文章里面我们具体描写叙述了字符设备驱动框架涉及的驱动注冊.通过设备文件来訪问驱动等知识.并明白通过device ...

  5. Matplotlib画图可选颜色

    经常会有画图找不到合适颜色的尴尬情景, 在网上找到一张matplotlib可以用的颜色图:

  6. sql server 查询某个表一直显示"正在执行中..."的问题

    问题描述:只是单纯的执行了"select count(*) from 某表":数据表中只有一两条数据,能查询其他表,唯独这个表不能进行任何操作: 经百度搜索实验,发现应该是某个进程 ...

  7. Django shortcut functions

    django.shortcuts package提供提供帮助类和函数可以更便捷的操作MVC中的每一部分,包含: render(request, template_name,[dictionary],[ ...

  8. (转载) Android RecyclerView 使用完全解析 体验艺术般的控件

    Android RecyclerView 使用完全解析 体验艺术般的控件 标签: Recyclerviewpager瀑布流 2015-04-16 09:07 721474人阅读 评论(458) 收藏  ...

  9. SpringMVC学习(二)——SpringMVC架构及组件(及其运行原理)-转载

    相信大家通过前文的学习,已经对SpringMVC这个框架多少有些理解了.还记得上一篇文章中SpringMVC的处理流程吗?  这个图大致描述了SpringMVC的整个处理流程,这个流程图还是相对来说比 ...

  10. xml中单词下面有提示下划线

    xml中单词下面有提示下划线,表示单词拼写错误或者大小写错误