Truck History
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 22648   Accepted: 8781

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.

题意:每输入一个字符串代表一个节点,而节点之间的距离就是不同字符串字对应位置不同的字符个数,然后求最小生成树
不同题意的时候完全不知道怎么做,当知道是距离是什么和是求最小生成树的时候就成了水题
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm> using namespace std;
const int INF = ;
int n;
char str[ + ][];
int edge[ + ][ + ],dist[ + ],vis[ + ];
int weight(char a[],char b[])
{
int ans = ;
for(int i = ; i < ; i++)
if(a[i] != b[i])
ans ++;
return ans;
}
void prime()
{
memset(vis,,sizeof(vis));
for(int i = ; i <= n; i++)
{
dist[i] = edge[][i];
}
vis[] = ;
dist[] = ;
int sum = ;
for(int i = ; i < n; i++)
{
int minn = INF,pos;
for(int j = ; j <= n; j++)
{
if(vis[j] == && dist[j] < minn)
{
minn = dist[j];
pos = j;
}
}
sum += minn;
vis[pos] = ;
for(int j = ; j <= n; j++)
{
if(dist[j] > edge[pos][j])
dist[j] = edge[pos][j];
}
}
printf("The highest possible quality is 1/%d.\n",sum);
}
int main()
{
while(scanf("%d", &n) != EOF)
{
if(n == )
break;
for(int i = ; i <= n; i++)
{
scanf("%s", str[i]);
}
for(int i = ; i < n; i++)
{
for(int j = i + ; j <= n; j++)
{
edge[i][j] = edge[j][i] = weight(str[i],str[j]);
}
}
prime();
}
return ;
}

POJ 1789Truck History(pirme)的更多相关文章

  1. History(历史)命令用法

    如果你经常使用 Linux 命令行,那么使用 history(历史)命令可以有效地提升你的效率.本文将通过实例的方式向你介绍 history 命令的用法. 使用 HISTTIMEFORMAT 显示时间 ...

  2. POJ 2431 Expedition(探险)

    POJ 2431 Expedition(探险) Time Limit: 1000MS   Memory Limit: 65536K [Description] [题目描述] A group of co ...

  3. History(历史)命令用法15例

    导读 如果你经常使用 Linux 命令行,那么使用 history(历史)命令可以有效地提升你的效率,本文将通过实例的方式向你介绍 history 命令的 15 个用法. 使用 HISTTIMEFOR ...

  4. POJ 3414 Pots(罐子)

    POJ 3414 Pots(罐子) Time Limit: 1000MS    Memory Limit: 65536K Description - 题目描述 You are given two po ...

  5. POJ 3281 Dining (网络流)

    POJ 3281 Dining (网络流) Description Cows are such finicky eaters. Each cow has a preference for certai ...

  6. POJ 1847 Tram (最短路径)

    POJ 1847 Tram (最短路径) Description Tram network in Zagreb consists of a number of intersections and ra ...

  7. [转] Linux History(历史)命令用法 15 例

    [From]https://linuxtoy.org/archives/history-command-usage-examples.html 如果你经常使用 Linux 命令行,那么使用 histo ...

  8. History(历史)命令用法 15 例

    如果你经常使用 Linux 命令行,那么使用 history(历史)命令可以有效地提升你的效率.本文将通过实例的方式向你介绍 history 命令的 15 个用法. 使用 HISTTIMEFORMAT ...

  9. Backbone源码解析(五):Route和History(路由)模块

    今天是四月十二号,距离上次写博已经将近二十天了.一直忙于工作,回家被看书的时间占用了.连续两个礼拜被频繁的足球篮球以及各种体育运动弄的精疲力竭,所以很少抽时间来写技术博客.今天抽出时间把backbon ...

随机推荐

  1. Python-面向对象编程

    概述: 面向过程:根据业务逻辑从上到下写代码. 函数式:将某功能代码封装到函数中,以后便无需重复编写,进调用函数即可. 面向对象:对函数进行分类和封装,让开发“更快更好更强” 创建类和对象 面向对象编 ...

  2. ping提示小结

    1,Win7 ping 不存在的地址(请求超时) 因为路由器不理睬他. 2,R1-R2-R3 R1有默认路由,R1 ping不存在的地址(目标不可达) 3,R1-R2 R1ping本网段中不存在的地址 ...

  3. C语言 日常小结

    1.当数组当作函数参数的时候会退化为指针 #include<stdio.h> #include<stdlib.h> void sort(int a[]){ int num = ...

  4. 将Html文档整理为规范XML文档

    有多种方式可以在.NET 平台进行HTML文件解析.数据提取,其中最简单.稳妥的办法是先使用工具将Html文档整理成XML文档,再通过XML Dom模型或XPath灵活地进行数据处理.SGML便是一个 ...

  5. Java Concurrency in Practice 读书笔记 第十章

    粗略看完<Java Concurrency in Practice>这部书,确实是多线程/并发编程的一本好书.里面对各种并发的技术解释得比较透彻,虽然是面向Java的,但很多概念在其他语言 ...

  6. Spring系列: 使用aop报错:nested exception is java.lang.NoClassDefFoundError: org/aspectj/weaver/reflect/ReflectionWorld$Refle

    写了个最简单的aop例子 配置文件如下 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns ...

  7. Javascript系列: Google Chrome调试js代码(zz)

    你 是怎么调试 JavaScript 程序的?最原始的方法是用 alert() 在页面上打印内容,稍微改进一点的方法是用 console.log() 在 JavaScript 控制台上输出内容.嗯~, ...

  8. 20155301-滕树晨 第二次随笔作业--从现有技能获取的经验应用于JAVA中

    第二次随笔--从现有技能获取的经验应用于JAVA中 你有什么技能比大多人(超过90%以上)更好? 这个想了半天,有一个是我乒乓球还是比较擅长的,在学校里可能比百分之90的人要强,在外面肯定是不如了.再 ...

  9. diff: /../Podfile.lock: No such file or directory

    从github上下载源码运行会报错:问题1描述: diff: /../Podfile.lock: No such file or directory diff: /Manifest.lock: No ...

  10. lumia 520无法开机

    拿出尘封已久的lumia 520,发现其开机困难,现象如下: 1.拿掉电池再放回去有几率开机 2.轻轻地用手机砸向桌面时手机会重启 因为手机在更新WP8.1之后就出问题了,所以先得定位问题,在黑屏的时 ...