链接:

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

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 22133   Accepted: 8581

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 <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <algorithm>
using namespace std; const int N = ;
const int INF = 0xfffffff; int n;
int J[N][N], dist[N];
char s[N][];
bool vis[N]; int Len(char a[], char b[])
{
int ans=; for(int i=; i<; i++)
{
if(a[i]!=b[i])
ans++;
}
return ans;
} int Prim()
{
int i, j, ans=; memset(vis, , sizeof(vis));
vis[]=; dist[]=;
for(i=; i<=n; i++)
dist[i]=J[][i]; for(i=; i<n; i++)
{
int MIN=INF;
int index=;
for(j=; j<=n; j++)
{
if(!vis[j] && dist[j]<MIN)
{
index=j;
MIN=dist[j];
}
}
vis[index]=;
if(MIN==INF)
break;
ans += MIN; for(j=; j<=n; j++)
{
if(!vis[j] && dist[j]>J[index][j] && J[index][j]> )
dist[j]=J[index][j];
}
} return ans;
} int main ()
{ while(scanf("%d", &n), n)
{
int i, j; memset(s, , sizeof(s));
for(i=; i<=n; i++)
scanf("%s", s[i]); for(i=; i<=n; i++)
for(j=; j<=i; j++)
{
J[i][j]=J[j][i]=Len(s[i], s[j]);
} int ans=Prim();
printf("The highest possible quality is 1/%d.\n", ans);
}
return ;
}

(最小生成树)Truck History --POJ -- 1789的更多相关文章

  1. Truck History - poj 1789 (Prim 算法)

      Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 20884   Accepted: 8075 Description Ad ...

  2. Truck History POJ - 1789

    题目链接:https://vjudge.net/problem/POJ-1789 思路: 题目意思就是说,给定一些长度为7的字符串,可以把字符串抽象为一个点, 每个点之间的距离就是他们本身字符串与其他 ...

  3. F - Truck History - poj 1789

    有一个汽车公司有很多年的汽车制造历史,所以他们会有很多的车型,现在有一些历史学者来研究他们的历史,发现他们的汽车编号很有意思都是有7个小写字母组成的,而且这些小写字母具有一些特别的意义,比如说一个汽车 ...

  4. Truck History POJ - 1789 板子题

    #include<iostream> #include<cstring> #include<algorithm> #include<stdio.h> u ...

  5. poj 1789 prime

    链接:Truck History - POJ 1789 - Virtual Judge  https://vjudge.net/problem/POJ-1789 题意:先给出一个n,代表接下来字符串的 ...

  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(prim&amp;&amp;最小生成树)

    id=1789">Truck History Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 17610   ...

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

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

  9. poj 1789 Truck History 最小生成树

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

随机推荐

  1. 向数据库添加学生信息。存放在REQUEST对象里

    代码前几天已经发过了,但是程序一直还没运行出来,今天重新建立了一个数据库,才可以,下面补充上数据截图

  2. 打地鼠Demo

    using UnityEngine; using System.Collections; public enum MoleStates { NormalState,// 初始状态 UpState,// ...

  3. Validate the date format

    Validate the date format function checkdate(input) { var validformat = /^\d{2}\/\d{2}\/\d{4}$/; //Ba ...

  4. Linux初学时的一些常用命令(3)

    管道 |  重要的一个概念,其作用是将一个命令的输出用作另一个命令的输入 例如:  在ifconfig的结果里查找 192.168字符串  ifconfig | grep 192.168 查找和jav ...

  5. eclipse UTF-8

    1. 你本地开发环境IDE,默认配置也是gbk,改为utf82. 检查你tomcat等服务器中间件GBK改成UTF8eclipse工作空间的编码设置成UTF-8,具体操作是:windows---pre ...

  6. 超薄二维Mo2C晶体

    记者今天从中国科学院金属研究所获悉,该所沈阳材料科学国家(联合)实验室先进炭材料研究部任文才研究组在大尺寸高质量二维过渡族金属碳化物晶体的制备与物性研究方面取得了重要突破.相关成果日前在<自然— ...

  7. cdoj第13th校赛初赛A - AC Milan VS Juventus 【枚举】

    http://acm.uestc.edu.cn/#/contest/show/54 A - AC Milan VS Juventus Time Limit: 3000/1000MS (Java/Oth ...

  8. Volley下载图片存放在data/data下 networkImageView lrucache

    networkImageView 设置图片的方法  (有效) imageView.setImageUrl("https://www.baidu.com/img/bd_logo1.png&qu ...

  9. Loitor_产品(一)

    源码:https://github.com/loitor-vis/vi_sensor_sdk 注意:以下要一直在管理员权限 1.C++ 示例程序的编译步骤 先确认你的系统已经成功安装了OpenCV. ...

  10. Spring框架之log日志的使用

    1.Spring框架也需要引入日志相关的jar包 * 在spring-framework-3.0.2.RELEASE-dependencies/org.apache.commons/com.sprin ...