http://poj.org/problem?id=1789

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 27597   Accepted: 10731

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

别忘了初始化~

 #include <algorithm>
#include <iostream>
#include <cstdio> using namespace std; const int N();
int n,ans;
char s[N][];
int minn,vis[N],d[N],dis[N][N]; void Prime()
{
for(int i=;i<n;i++) d[i]=dis[][i];
d[]=vis[]=;
for(int i=;i<n;i++)
{
minn=;
for(int j=;j<n;j++)
if(!vis[j]&&(!minn||d[minn]<d[j])) minn=j;
vis[minn]=;
for(int j=;j<n;j++)
if(!vis[j]) d[j]=min(d[j],dis[minn][j]);
}
for(int i=;i<n;i++) ans+=d[i];
printf("The highest possible quality is 1/%d.\n",ans); ans=;
} int main()
{
while(scanf("%d",&n)&&n)
{
for(int i=;i<=n;i++)
cin>>s[i];
for(int i=;i<n;i++)
for(int j=i+;j<n;j++)
{
for(int k=;k<;k++)
if(s[i][k]!=s[j][k]) dis[i][j]++;
dis[j][i]=dis[i][j];
}
Prime();
}
return ;
}

POJ——T1789 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. Linux下 利用find命令删除所有.svn目录

    ====================实例============== 删除所有.svn目录 这也是我当初查找 Linux find 命令的目的. 1)  find . -type d -name ...

  2. osEye.Net:离别是为了将来的重逢

    这一时刻已经成为osEye历史..... 在热心网友的关怀和鼓励之下,osEye.net将继续运行着,感谢你们陪伴osEye一起走过.... 与你相知相恋已经有4个年头了,你的成长到成熟都让我历历在目 ...

  3. python3连接Mairadb数据库

    <span style="font-size:18px;">#本代码演示的是python3.3.5下连接Mairadb数据库</span> <span ...

  4. OpenST Basic tool library

    /***************************************************************************** * OpenST Basic tool l ...

  5. QT 随笔

     1. 设置窗体属性,无边框 | 置顶 setWindowFlags(Qt::FramelessWindowHint); setWindowFlags(Qt::FramelessWindowHin ...

  6. Oracle SQL性能优化系列

    1. 选用适合的ORACLE优化器 ORACLE的优化器共有3种: a. RULE (基于规则) b. COST (基于成本) c. CHOOSE (选择性) 设置缺省的优化器,可以通过对init.o ...

  7. [AHOI 2009] 同类分布

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=1799 [算法] 数位DP [代码] #include<bits/stdc++. ...

  8. [jzoj 5661] 药香沁鼻 解题报告 (DP+dfs序)

    interlinkage: https://jzoj.net/senior/#contest/show/2703/0 description: solution: 注意到这本质就是一个背包,只是选了一 ...

  9. flask-alembic数据迁移工具

    alembic是用来做ORM模型与数据库的迁移与映射.alembic使用方式跟git有点类似,表现在两个方面, 第一,alemibi的所有命令都是以alembic开头: 第二,alembic的迁移文件 ...

  10. HD-ACM算法专攻系列(16)——考试排名

    问题描述: 源码: 主要要注意输出格式. #include"iostream" #include"iomanip" #include"algorith ...