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. hadoop的理解

    知乎上有个人写的感觉挺好理解的,不知道对不对,先截图存着

  2. Linux-经常用到的几个命令

    -- |" 拷贝本地到远程 scp /serverdata/server/tomcat-uaac/webapps/dm.war root@172.16.7.123:/serverdata/s ...

  3. [Windows Server]安装系统显示“缺少计算机所需的介质驱动程序”解决方案

    1.把电脑上插着的硬盘拔了 2.重试 3.修复计算机找到dos命令行 4.然后进入我们放置解压了的系统的那个符盘,(我这里放在D盘)输入:d:       找到刚才我们解压了的系统文件,进入sourc ...

  4. 关于心理的二十五种倾向(查理&#183;芒格)-2

    5)避免不一致倾向避免不一致倾向实际上就是人天生就害怕改变.相同是由于人类大脑的生理机制决定的.由于这样的倾向能够带来节省运算空间和能量的优点.这样的抗改变模式的形成,可能的原因例如以下:A) 迅速作 ...

  5. 【IOI 2011】Race

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=2599 [算法] 点分治 [代码] #include<bits/stdc++.h ...

  6. jqueryui slider

    <!doctype html><html lang="en"><head> <meta charset="utf-8" ...

  7. 不用copy代码--eclipse使用git提交项目-转

    原文地址:http://blog.csdn.net/u014079773/article/details/51595127 准备工作: 目的:eclipse使用git提交本地项目,提交至远程githu ...

  8. 视图 Model转集合

    @{    Layout = null;}@using MvcApplication2.Models <!DOCTYPE html> <html><head>    ...

  9. 2017.7.15清北夏令营精英班Day1解题报告

    成绩: 预计分数:20+10+40 实际分数:100+10+40. 一百三十多人的比赛全场rand7还水了个鼠标+键盘 unbelievable! 考试题目链接: https://www.luogu. ...

  10. thinkphp连接数据库,会有大量的sleep连接

    show processlist; 说明各列的含义和用途, id列:一个标识,你要kill 一个语句的时候很有用. user列: 显示当前用户,如果不是root,这个命令就只显示你权限范围内的sql语 ...