Truck History
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 27335   Accepted: 10634

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个7位字符串,有一个派生到另一个需要花费两个字符串向相应位置不同字母个数,求最小花费

思路:以不同字母个数建边,最小生成树

#include <iostream>
#include<queue>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<set>
using namespace std;
#define LL long long struct node
{
int u,v,w;
} p[10000005];
int n,cnt,pre[2006];
bool cmp(node a,node b)
{
return a.w<b.w;
}
void init()
{
for(int i=0; i<2005; i++)
pre[i]=i;
} int fin(int x)
{
return pre[x]==x?x:pre[x]=fin(pre[x]);
} void kruskal()
{
sort(p,p+cnt,cmp);
init();
int cost=0;
int ans=0;
for(int i=0; i<cnt; i++)
{
int a=fin(p[i].u);
int b=fin(p[i].v);
if(a!=b)
{
pre[a]=b;
cost+=p[i].w;
ans++;
}
if(ans==n-1)
{
break;
}
}
printf("The highest possible quality is 1/%d.\n",cost);
} int main()
{
char a[2005][10];
while(~scanf("%d",&n)&&n)
{ for(int i=0;i<n;i++)
scanf("%s",&a[i]);
cnt=0;
for(int i=0; i<n; i++)
for(int j=i+1; j<n; j++)
{
int cc=0;
for(int k=0;k<7;k++)
if(a[i][k]!=a[j][k])
cc++;
p[cnt].u=i,p[cnt].v=j;
p[cnt++].w=cc;
}
kruskal();
}
return 0;
}

POJ1789 Truck History 2017-04-13 12:02 33人阅读 评论(0) 收藏的更多相关文章

  1. 棋盘问题 分类: 搜索 POJ 2015-08-09 13:02 4人阅读 评论(0) 收藏

    棋盘问题 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 28474 Accepted: 14084 Description 在一 ...

  2. winfrom 截屏、抓屏 分类: WinForm 2014-08-01 13:02 198人阅读 评论(0) 收藏

    截取全屏代码: try { this.Hide(); Rectangle bounds = Screen.GetBounds(Screen.GetBounds(Point.Empty)); Bitma ...

  3. Hdu1969 Pie 2017-01-17 13:12 33人阅读 评论(0) 收藏

    Pie Time Limit : 5000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total Submissio ...

  4. 【Heritrix基础教程之2】Heritrix基本内容介绍 分类: B1_JAVA H3_NUTCH 2014-06-01 13:02 878人阅读 评论(0) 收藏

    1.版本说明 (1)最新版本:3.3.0 (2)最新release版本:3.2.0 (3)重要历史版本:1.14.4 3.1.0及之前的版本:http://sourceforge.net/projec ...

  5. ZOJ3770Ranking System 2017-04-14 12:42 52人阅读 评论(0) 收藏

    Ranking System Time Limit: 2 Seconds      Memory Limit: 65536 KB Few weeks ago, a famous software co ...

  6. ios UIKit动力 分类: ios技术 2015-07-14 12:55 196人阅读 评论(0) 收藏

    UIkit动力学是UIkit框架中模拟真实世界的一些特性. UIDynamicAnimator 主要有UIDynamicAnimator类,通过这个类中的不同行为来实现一些动态特性. 它一般有两种初始 ...

  7. Log4j 2使用教程 分类: B1_JAVA 2014-07-01 12:26 314人阅读 评论(0) 收藏

    转载自 Blog of 天外的星星: http://www.cnblogs.com/leo-lsw/p/log4j2tutorial.html Log4j 2的好处就不和大家说了,如果你搜了2,说明你 ...

  8. Python获取当前时间 分类: python 2014-11-08 19:02 132人阅读 评论(0) 收藏

    Python有专门的time模块可以供调用. <span style="font-size:14px;">import time print time.time()&l ...

  9. Hdu 1009 FatMouse' Trade 2016-05-05 23:02 86人阅读 评论(0) 收藏

    FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tot ...

随机推荐

  1. appium的三种等待方式 (还没实践过,记录在此)

    参考:https://testerhome.com/topics/2576

  2. 学习笔记之PHP

    学习 PHP,第 1 部分: 注册帐户.上传需要批准的文件.并查看和下载已批准的文件 https://www.ibm.com/developerworks/cn/opensource/tutorial ...

  3. web开发的功能备忘录

    总体设计 Web项目开发中,我们可以根据不同的功能,将整个系统的功能进行划分.系统功能总体来说可以划分为两个模块:系统的必备功能和逻辑业务功能. 下面主要是分析一下系统的必备功能,所谓系统的必备功能是 ...

  4. thinkPHP volist标签循环输出多维数组

    <volist name="company" id="vo">{$vo.company_name}<volist name="vo[ ...

  5. probably another instance of uWSGI is running on the same address

    probably another instance of uWSGI is running on the same address 可以用命令杀掉这个端口在重启: /tcp

  6. Linux下安装MATLAB

    Why do I see "Preparing installation files ... Installing ... Finished..." in the terminal ...

  7. BTM事务配置

    请参考原贴:http://thinkdifferent.iteye.com/blog/1450433 Tomcat6上配置BTM 博客分类: practice tomcatjava )去http:// ...

  8. jeesite快速开发平台(二)----环境搭建

    转自:https://blog.csdn.net/u011781521/article/details/54880465

  9. STL string 常用函数(转)

    string类的构造函数: string(const char *s); //用c字符串s初始化 string(int n,char c); //用n个字符c初始化 此外,string类还支持默认构造 ...

  10. 对avalon的类名操作进行升级

    在对SVG元素进行类名操作时,发现有一个坑爹的事情,它的className竟然是一个对象,因此报一系列BUG.第一次想到的方法是添加setClasses, getClasses两个更底层的方法.于是相 ...