链接:

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. @Transactionl注解

    spring 事务注解 默认遇到throw new RuntimeException("...");会回滚 需要捕获的throw new Exception("...&q ...

  2. fio 测试磁盘

    root@rook-test:/# ceph osd status+----+-----------------------------+-------+-------+--------+------ ...

  3. easyUIDataGrid分页

    package com.cn.eport.util; import java.util.List; /** * * * @author zh * */ public class DataGrid im ...

  4. ubuntu16.04安装virtualbox

    download:download.virtualbox.org/virtualbox/5.0.10/virtualbox-5.0_5.0.10-104061~Ubuntu~trusty_amd64. ...

  5. android mapView

    1.Because the Maps library is not a part of the standard Android library, you must declare it in the ...

  6. 使用ecstore-sdk开发包制作易开店和启明星模板

    前言: 尽管商派官网有模板开发教程,但是诸多方面太过笼统.我等平庸之辈,纵使细心研读,潜心修炼,亦未能品味练功境界,领悟其中真谛. 商派有云,此九阳真经不用您挥刀****本人却感觉此教程令人抓狂,无人 ...

  7. Spring框架的AOP的底层实现之JDK的动态代理(代码了解,理解原理)

    1.创建接口UserDao: package com.huida.demo1; public interface UserDao { public void save(); public void u ...

  8. WCF的例子

    Demo的 “Service端”以本机IIS为宿主,“Client端”以WebForm项目为例. 1.新建项目:WCF>WCF Service Application: 2.删除默认文件ISer ...

  9. C#控制台进度条(Programming Progress bar in C# Consle application)

    以下代码从Stack Overflow,觉得以后会用到就收藏一下,我是辛勤的搬运工,咿呀咿呀哟- 1.showing percentage in .net console application(在. ...

  10. Django之virtualenv下安装xadmin

    1.安装xadmin,通过pip 进入virtualenv pip安装xadmin pyyuc:~ yuchao$ source PycharmProjects/mxvenv/bin/activate ...