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. hdu 3836 强连通+缩点:加边构强连通

    #include<stdio.h>//求出其所有的强连通分量缩点,选出出度和入度最大的那个就是要求的边 #include<string.h> #include<stdli ...

  2. BA-风阀水阀执行器接线图

    220水阀执行器接线图 24V风阀执行器接线图

  3. Oracle里schema理解

    在Oracle中,一个用户就是一个Schema,表都是建立在Schema中的,也可以理解为每个用户拥有不同的表.一个用户想访问另外一个用户,也就是另外一个schema的表的时候,可以用 usernam ...

  4. wifi共享精灵2014.04.25.001已经更新,wifi热点中文名走起!

    五一回来后,有个惊喜,wifi共享精灵有了最新动向.不晓得wifi共享精灵是啥的朋友,我来解释下,它就相当于一个无线路由器.说起来,Wifi共享精灵正式版2014.04.25.001(http://w ...

  5. poi判断一行是隐藏的getZeroHeight()

    poi判断一行是隐藏的 getZeroHeight() boolean isZeroHeight = row.getZeroHeight(); if(isZeroHeight){ // 如果为隐藏行就 ...

  6. memcached—向memcached中保存Java实体需注意的问题

    今天以代码实例的形式总结一下向memcached中保存Java实体需注意的问题: memcached工具类代码: package com.ghj.packageoftool; import java. ...

  7. USACO holstein 超时代码

    /* ID:kevin_s1 PROG:holstein LANG:C++ */第八组数据跪了.半天都不出结果 #include <iostream> #include <cstdi ...

  8. Android圆角Tag控件的另类实现

    一般的圆角标签控件都是用xml设置shape做实现.可是假设我们想要做一个更加强大通用的的圆角控件,不须要使用者去关心圆角,仅仅设置背景就能够了. 应该怎么实现呢?这个就须要把背景先设置成图片,然后再 ...

  9. 在iOS中求数组元素中最大数与最小数

    之前求数组中某个数中的最大值与最小值.还真一个个比較,后来看到这种方法后,我就醉了 NSArray *testArray = [NSArray arrayWithObjects:@"-2.0 ...

  10. Oracle 优化和性能调整

    分析评价Oracle数据库性能主要有数据库吞吐量.数据库用户响应时间两项指标.数据库用户响应时间又可以分为系统服务时间和用户等待时间两项,即:  数据库用户响应时间=系统服务时间+用户等待时间  因此 ...