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.

题意:输入1个n,代表有多少组字符数组。每一个数组都仅仅有7个字符,每2个数组不同字符的个数代表这2个字符串的距离(如果为距离)。如今要你构建一棵树,使得所花费的距离最小

思路:用Prim算法处理。

AC代码:

#include<stdio.h>
#include<string.h>
#define MAXN 100000000
int mark[2014],f[2014],n;
char str[2014][8];
int val(int i,int j) //2个字符串的距离
{
int sum,k;
sum=0;
for(k=0;k<7;k++)
if(str[i][k]!=str[j][k])
sum++;
return sum;
}
void prim()
{
int i,j,sum=0,minn,k;
for(i=0;i<n;i++) //每一个字符串与0的距离
f[i]=val(0,i);
memset(mark,0,sizeof(mark));
f[0]=0;
mark[0]=1; //标记已使用过0
for(i=0;i<n-1;i++) //Prim算法的核心,这个东西必需要自己理解!
{
minn=MAXN;
for(j=0;j<n;j++)
{
if(!mark[j]&&minn>f[j])
{
minn=f[j];
k=j;
}
}
sum+=minn;
mark[k]=1;
for(j=0;j<n;j++) //换一个顶点
if(!mark[j]&&f[j]>val(k,j))
f[j]=val(k,j);
}
printf("The highest possible quality is 1/%d.\n",sum);
}
int main()
{
int i;
while(scanf("%d",&n)!=EOF)
{
if(n==0)break;
for(i=0;i<n;i++)
scanf("%s",str[i]);
prim();
}
return 0;
}

POJ 1798 Truck History的更多相关文章

  1. Kuskal/Prim POJ 1789 Truck History

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

  2. POJ 1789 -- Truck History(Prim)

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

  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【最小生成树简单应用】

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

  5. poj 1789 Truck History 最小生成树

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

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

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

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

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

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

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

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

    题目链接:http://poj.org/problem?id=1789 Advanced Cargo Movement, Ltd. uses trucks of different types. So ...

随机推荐

  1. 明晚8点,捷微团队QQ群公开课,解说jeewx2.0版本号maven环境的搭建入门!

    2014-08-13号晚8点,捷微团队QQ群公开课,解说jeewx2.0版本号maven环境的搭建入门! 讲师:刘强(团队成员) QQ群:287090836 (JAVA版本号微信开源项目) http: ...

  2. HDU 3488Tour(流的最小费用网络流)

    职务地址:hdu3488 这题跟上题基本差点儿相同啊... . 详情请戳这里. 另外我认为有要改变下代码风格了..最终知道了为什么大牛们的代码的变量名都命名的那么长..我决定还是把源点与汇点改成sou ...

  3. PV(访问量)、UV(独立访客)、IP(独立IP) (转)

    网站统计中的PV(访问量):UV(独立访客):IP(独立IP)的定义与区别今天使用了雅虎统计,看到里面就有这个,就说说,其实里面的uv大家可能觉得很新奇,但是和站长统计里的独立访客是一样的嘛.---- ...

  4. 使用SVN clang: error: linker command failed with exit code 1 (use -v to see invocation)

    然后上传到该项目SVN仓库上,例如,下面的错误再次发生再拉到本地编译 ld: library not found for -lxxxxxxxxxxxx clang: error: linker com ...

  5. poj2777--Count Color(线段树,二进制转化)

    Count Color Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 34950   Accepted: 10542 Des ...

  6. 找呀志_通过开源框架引AsyncHttpClient上传文件

    一个.步骤: 1.加入权限(接入网络和可写) 2.获取上传文件的路径和推断是空的 3.如果为空.创建一个异步请求对象 4.创建上传文件路径 5.跑post请求(指定url路径.封装上传參数.新建Asy ...

  7. hdu 4908 BestCoder Sequence 发现M中值是字符串数, 需要预处理

    BestCoder Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  8. Java的JXL操作xls形式

    jxl这是一个韩国的写作java操作excel工具, 源世界中,有两套比較有影响的API可供使用.一个是POI,一个是jExcelAPI.当中功能相对POI比較弱一点.但jExcelAPI对中文支持非 ...

  9. java 调用mysql的存储过程(简单示例)

    首先我在mysql的test数据库里定义了一个student表: create table student4( id   int   primary key, sanme char(5) ); 插入几 ...

  10. [ACM] HDU 2063 过山车 (二分图,匈牙利算法)

    过山车 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submis ...