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

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.

此题难点在于读题,明白题意后就是一个很简单的最小生成树

明白一点就行:将每一个卡车类型代码(truck type codes)作为一个结点,任意两个 卡车类型代码中 相同位置即字符数组a[i],a[i+1] 上 a[i]和a[i+1]为不同字符的位置的个数 做为

这两个结点之间的路径的权值。

读题啊,硬伤啊!

#include <iostream>
#include <stdio.h>
#include <string.h>
#define INF 0x3f3f3f3f
using namespace std;
char a[][];
int map[][];
int n,dis[],v[];
void prim()
{
int min,sum=,k;
for(int i=; i<=n; i++)
{
v[i]=;
dis[i]=INF;
}
for(int i=; i<=n; i++)
dis[i]=map[][i];
v[]=;
for(int j=; j<n; j++)
{
min=INF;
for(int i=; i<=n; i++)
{
if(v[i]==&&dis[i]<min)
{
k=i;
min=dis[i];
}
}
sum+=min;
v[k]=;
for(int i=; i<=n; i++)
{
if(v[i]==&&map[k][i]<dis[i])
{
dis[i]=map[k][i];
}
}
}
cout<<"The highest possible quality is 1/"<<sum<<"."<<endl;
}
int main()
{
int count;
while(scanf("%d",&n)!=EOF&&n!=)
{
for(int i=; i<=n; i++)
scanf("%*c%s",a[i]);
for(int i=; i<=n; i++)
{
for(int j=i+; j<=n; j++)
{
count=;
for(int k=; k<; k++)
if(a[i][k]!=a[j][k])
{
count++;
}
map[i][j]=count;
map[j][i]=count;
}
}
prim();
}
return ;
}

POJ1789:Truck History(Prim算法)的更多相关文章

  1. POJ1789 Truck History(prim)

    题目链接. 分析: 最大的敌人果然不是别人,就是她(英语). 每种代表车型的串,他们的distance就是串中不同字符的个数,要求算出所有串的distance's 最小 sum. AC代码如下: #i ...

  2. POJ1789 Truck History 【最小生成树Prim】

    Truck History Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 18981   Accepted: 7321 De ...

  3. Truck History(prim & mst)

    Truck History Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 19772   Accepted: 7633 De ...

  4. poj1789 Truck History

    Truck History Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 20768   Accepted: 8045 De ...

  5. poj1789 Truck History最小生成树

    Truck History Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 20768   Accepted: 8045 De ...

  6. POJ1789 Truck History 2017-04-13 12:02 33人阅读 评论(0) 收藏

    Truck History Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 27335   Accepted: 10634 D ...

  7. Truck History(prim)

    http://poj.org/problem?id=1789 读不懂题再简单也不会做,英语是硬伤到哪都是真理,sad++. 此题就是一个最小生成树,两点之间的权值是毎两串之间的不同字母数. #incl ...

  8. POJ 1789 -- Truck History(Prim)

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

  9. POJ-1789 Truck History---最小生成树Prim算法

    题目链接: https://vjudge.net/problem/POJ-1789 题目大意: 用一个7位的string代表一个编号,两个编号之间的distance代表这两个编号之间不同字母的个数.一 ...

随机推荐

  1. 虚拟机可以ping同宿主机,宿主机ping不通虚拟机

    虚拟机里能ping同本机,而本机却ping不通虚拟机,或者虚拟机不能ping通本机,可能有如下原因: 如果是桥接模式,那么可能性1:虚拟机防火墙禁ping,请关闭虚拟机防火墙重试:root 状态下se ...

  2. c++ 类前置声明【转】

    [转自 here] 在编写C++程序的时候,偶尔需要用到前置声明(Forward declaration).下面的程序中,带注释的那行就是类B的前置说明.这是必须的,因为类A中用到了类B,而类B的声明 ...

  3. Shell 中的反引号(`),单引号('),双引号(")

    在写shell的时候老是傻傻分不清楚,今天来理一理. 1.反引号位 (`) 位于键盘的Tab键的上方.1键的左方.注意与单引号(')位于Enter键的左方的区别. 在Linux中起着命令替换的作用.命 ...

  4. css3整理--box-shadow

    box-shadow语法:(想法:用阴影来做边框,就不会出现动态改变边框宽度而产生的重排问题) 对象选择器{box-shadow:投影方式 X轴偏移量 Y轴偏移量 阴影模糊半径 阴影扩展半径 阴影颜色 ...

  5. Eclipse 创建和读取yaml文件

    工具和用法: 1. eclipse插件包:org.dadacoalition.yedit_1.0.20.201509041456-RELEASE.jar 用法:将此jar包复制到eclipse-jee ...

  6. 让人一看就懂的excel相对引用和绝对引用案例解析

    http://www.ittribalwo.com/article/2831.html 内容提要:本文的excel相对引用和绝对引用.混合引用的使用方法案例截选自<Excel效率手册 早做完,不 ...

  7. javah生成jni头文件时报错 Error: cannot access android.support...

    javah生成jni头文件时报错: Error: cannot access android.support.v7.app.AppCompatActivity class file for andro ...

  8. 【BZOJ5146】有趣的概率 概率+组合数(微积分)

    [BZOJ5146]有趣的概率 Description "可爱的妹子就像有理数一样多,但是我们知道的,你在数轴上随便取一个点取到有理数的概率总是0,"芽衣在床上自顾自的说着这句充满 ...

  9. ubuntu部署nginx

    先更新本机内置的程序. sudo apt-get updatesudo apt-get upgrade再判断系统是否内置了add-apt-repository命令,如果没有执行下列命令安装 sudo ...

  10. Saltstack实战之无master和多master

    如果不想依赖master可以设置为无master vim /etc/salt/minion 就可以通过minion来安装一个salt-master了(下来查资料做,关闭salt-minion进程因为不 ...