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

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 <iostream>
#include <cstring> using namespace std;
char str[2001][9];
int d[2001][2001];
int mind[2001];//mind restore the min distance of the point between the tree when the point hasn't been add into
//rather than the point in the tree to all the point not in the tree
bool vis[2001];
int n;
int ans;
int index;
void setd(){
for(int i=0;i<n;i++){
d[i][i]=0;
for(int j=i+1;j<n;j++){
d[i][j]=d[j][i]=0;
for(int k=0;k<7;k++){
if(str[i][k]!=str[j][k]){
d[i][j]++;
}
}
d[j][i]=d[i][j];
}
}
}
void prim(){
memset(mind,0x3f,sizeof(mind));
memset(vis,0,sizeof(vis));
index=1;
vis[0]=true;
ans=0;
int s=0;
while(index<n){
int minnum=0x3f3f;
int mini=0;
for(int i=0;i<n;i++){
if(!vis[i]&&d[s][i]<mind[i]){
mind[i]=d[s][i];//use i to refresh all the point
}
if(!vis[i]&&mind[i]<minnum){
minnum=mind[i];
mini=i;
}
}
ans+=minnum;
vis[mini]=true;
s=mini;
index++;
}
}
int main(){
ios::sync_with_stdio(false);
while(cin>>n&&n){
for(int i=0;i<n;i++){
cin>>str[i];
}
setd();
prim();
cout<<"The highest possible quality is 1/"<<ans<<".\n";
} return 0;
}

  

poj 1789 Truck History 最小生成树 prim 难度:0的更多相关文章

  1. POJ 1789 -- Truck History(Prim)

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

  2. poj 1789 Truck History 最小生成树

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

  3. POJ 1789 Truck History(Prim+邻接矩阵)

    ( ̄▽ ̄)" #include<iostream> #include<cstdio> #include<cstring> #include<algo ...

  4. Kuskal/Prim POJ 1789 Truck History

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

  5. poj 1789 Truck History

    题目连接 http://poj.org/problem?id=1789 Truck History Description Advanced Cargo Movement, Ltd. uses tru ...

  6. POJ 1789 Truck History【最小生成树简单应用】

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

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

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

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

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

  9. POJ 1789 Truck History (Kruskal)

    题目链接:POJ 1789 Description Advanced Cargo Movement, Ltd. uses trucks of different types. Some trucks ...

随机推荐

  1. django通用视图

    通用视图 1. 前言 回想一下,在Django中view层起到的作用是相当于controller的角色,在view中实施的 动作,一般是取得请求参数,再从model中得到数据,再通过数据创建模板,返回 ...

  2. [转载]WorldWind实时确定、更新、初始化和渲染地形和纹理数据

    WorldWind实时确定.更新.初始化和渲染地形和纹理数据 原文链接: http://www.cnblogs.com/rainbow70626/p/5597267.html 当用户点击WorldWi ...

  3. EasyUI Progressbar 进度条

    通过 $.fn.progressbar.defaults 重写默认的 defaults. 进度条(progressbar)提供了一种显示长时间操作进度的反馈.进度可被更新以便让用户知道当前正在执行的操 ...

  4. google浏览器插件安装

    1:安装本地插件,直接将下载好的crx插件拖入到   chrome://extensions/    的空白处 http://www.cnplugins.com/tool/outline-instal ...

  5. HahMap

    HashMap的定义 public class HashMap<K, V> extends AbstractMap<K, V> implements Map<K, V&g ...

  6. CSU 1642 Problem B[难][前缀和]

    Description 已知两个正整数a和b,求在a与b之间(包含a和b)的所有整数的十进制表示中1出现的次数. Input 多组数据(不超过100000组),每组数据2个整数a,b.(1≤a,b≤1 ...

  7. 2.7 The Object Model -- Bindings, Observers, Computed Properties:What do I use when?

    有时候新用户在使用计算属性.绑定和监视者时感到困惑.下面是一些指导方针: 1. 使用computed properties来合成其他属性,以构建新的属性.computed properties不应该包 ...

  8. cocos代码研究(11)ActionManager类学习笔记

    理论部分 ActionManager是一个单例类,管理所有动作. 通常你不需要直接使用这个类.大多情况下,你将使用Node的接口,它提供了更友好的封装 但也有一些情况下,你可能需要使用这个单例. 示例 ...

  9. 关于Oracle误操作--数据被Commit后的数据回退恢复(闪回)

    今天操作Oracle数据库时,做一个Update数据时,不小心少写了个where,看这粗心大意的. 于是乎,把所有的员工工号都给更新成一个同一个工号了.这是一个悲催的故事. 因为工号是Check了好多 ...

  10. Java基础教程:网络编程

    Java基础教程:网络编程 基础 Socket与ServerSocket Socket又称"套接字",网络上的两个程序通过一个双向的通信连接实现数据的交换,这个连接的一端称为一个s ...