题目连接: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.
题目大意:
给一堆长度固定为7的只含小写英文字母的字符串,每两个字符串之间的距离定义为他们不同的字母个数,求最小生成树。
解题思路:
很直接的最小生成树,由题意这是一个完全图,所以选prim算法,有多组输入,注意一下初始化即可。
代码如下:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#define MAX 2005
#define INF 14005
using namespace std;

int G[MAX][MAX];
int d[MAX];
};
char a[MAX][MAX];
int n;

int prim(int s)
{
    vis[s]=;
    ;i<n;i++)
        d[i]=G[s][i];
    int now,m;
    ;
    ;i<n;i++)
    {
        m=INF;
        ;j<n;j++)
        {
            if(!vis[j]&&d[j]<m)
            {
                now=j;
                m=d[j];
            }
        }
        vis[now]=;
        res+=m;
        ;j<n;j++)
        {
            if(!vis[j]&&d[j]>G[now][j])
                d[j]=G[now][j];
        }
    }
    return res;
}

void init()
{
    memset(vis,,sizeof(vis));
    ;i<n;i++)
        ;j<n;j++)
            G[i][j]=INF;
}

int getdis(char * a,char * b)
{
    ;
    ;i<;i++)
    {
        if(a[i]!=b[i])
            res++;
    }
    return res;
}

int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        )
            ;
        init();
        ;i<n;i++)
            scanf("%s",a[i]);
        ;i<n;i++)
            ;j<n;j++)
                G[i][j]=getdis(a[i],a[j]);
        printf());
    }
}

poj1789 最小生成树的更多相关文章

  1. 28-Truck History(poj1789最小生成树)

    http://poj.org/problem?id=1789 Truck History Time Limit: 2000MS   Memory Limit: 65536K Total Submiss ...

  2. 最小生成树Prim poj1258 poj2485 poj1789

    poj:1258 Agri-Net Time Limit: 1000 MS Memory Limit: 10000 KB 64-bit integer IO format: %I64d , %I64u ...

  3. POJ1789&amp;ZOJ2158--Truck History【最小生成树变形】

    链接:http://poj.org/problem?id=1789 题意:卡车公司有悠久的历史,它的每一种卡车都有一个唯一的字符串来表示,长度为7,它的全部卡车(除了第一辆)都是由曾经的卡车派生出来的 ...

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

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

  5. poj1789 Truck History最小生成树

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

  6. 最小生成树练习3(普里姆算法Prim)

    风萧萧兮易水寒,壮士要去敲代码.本女子开学后再敲了.. poj1258 Agri-Net(最小生成树)水题. #include<cstdio> #include<cstring> ...

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

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

  8. kuangbin最小生成树专题

    网址:https://vjudge.net/contest/66965#overview 第一题: poj1251 裸最小生成树 #include<iostream> #include&l ...

  9. 最小生成树(Kruskal算法-边集数组)

    以此图为例: package com.datastruct; import java.util.Scanner; public class TestKruskal { private static c ...

随机推荐

  1. AJAX基本演示使用

    Servlet配置 <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="h ...

  2. Charles 抓包发现自动跳转为https 问题梳理

    今天遇到个有点意思的问题.特此记录. 业务场景: 做了一个页面,但是对外是挂载在京东主站上.如:www.jd.com/yifu/123456.html. 现场情况: 在本地/测试环境/预发环境中,每次 ...

  3. BZOJ 2502: 清理雪道 | 有上下界最小流

    #include<cstdio> #include<algorithm> #include<cstring> #include<queue> #defi ...

  4. share-Nothing原理

    Share nothing理论在数据库设计和优化中的实践应用 首先介绍share nothing概念.最早接触它是在 DataBaseManagentSystem一书的并行数据库章节中. 并行数据库要 ...

  5. elemetUi 组件--el-upload

    [需求]实现上传Excel文件,在上传到服务器时,还要附加一个参数,在请求上传文件接口前,先要进行文件格式判断. [知识点] 1.el-upload 官方文档中,主要用到了以下属性: data 可选参 ...

  6. win 7 64 位系统驱动签名

    自己开发未经签名的驱动无法加载,关闭Windows 7系统中的驱动签名强制要求 bcdedit.exe -set loadoptions DDISABLE_INTEGRITY_CHECKS

  7. 【Foreign】树 [prufer编码][DP]

    树 Time Limit: 10 Sec  Memory Limit: 256 MB Description Input Output Sample Input 3 2 2 1 Sample Outp ...

  8. 汕头市队赛 SRM 06 A 撕书

    A 撕书 SRM 06 背景&&描述 游行寺汀正在杀书.         书总共有n页,每页都可以看作是一个小写英文字母,所以我们可以把书看成长度为n的字符串s.         琉璃 ...

  9. bzoj 2668 费用流

    我们可以把初始状态转化为目标状态这一约束转化为将黑子移动到目标状态所需要的最少步数. 除了初始点和目标点之外,剩下的点如果被经过那么就会被交换两次,所以我们将一个点拆成3个点,a,b,c,新建附加源点 ...

  10. jquery hover事件中 fadeIn和fadeOut 效果不能及时停止

    $(".nav ul li").hover(function () { var id = $(this).attr("id"); $(".nav dl ...