题目连接: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. HTML5<canvas>标签:使用canvas元素在网页上绘制线条和圆(1)

    什么是 Canvas? HTML5 的 canvas 元素使用 JavaScript 在网页上绘制图像. 画布是一个矩形区域,您可以控制其每一像素. canvas 拥有多种绘制路径.矩形.圆形.字符以 ...

  2. Xinetd服务的安装与配置

    Xinetd服务的安装与配置 http://blog.chinaunix.net/uid-21411227-id-1826885.html 1.什么是xinetd xinetd即extended in ...

  3. 【BZOI 1202 狡猾的商人】

    Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 4149  Solved: 1994[Submit][Status][Discuss] Descript ...

  4. 【CF MEMSQL 3.0 D. Third Month Insanity】

    time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...

  5. 【BZOJ 3907】网格 组合数学

    大家说他是卡特兰数,其实也不为过,一开始只是用卡特兰数来推这道题,一直没有怼出来,后来发现其实卡特兰数只不过是一种组合数学,我们可以退一步直接用组合数学来解决,这道题运用组合数的思想主要用到补集与几何 ...

  6. spring中Constructor、@Autowired、@PostConstruct的顺序【转】

    其实从依赖注入的字面意思就可以知道,要将对象p注入到对象a,那么首先就必须得生成对象p与对象a,才能执行注入.所以,如果一个类A中有个成员变量p被@Autowired注解,那么@Autowired注入 ...

  7. The 13th Zhejiang Provincial Collegiate Programming Contest - C

    Defuse the Bomb Time Limit: 2 Seconds      Memory Limit: 65536 KB The bomb is about to explode! Plea ...

  8. [poj 3693]后缀数组+出现次数最多的重复子串

    题目链接:http://poj.org/problem?id=3693 枚举长度L,看长度为L的子串最多能重复出现几次,首先,能出现1次是肯定的,然后看是否能出现两次及以上.由抽屉原理,这个子串出现次 ...

  9. 百度vue服务端渲染(ssr)有感

    前端各种框架工具层次不穷,日新月异,越学越混乱了快 知乎上看到了一段回复,豁然开朗的感觉. Web 2.0时代最大的思想革命本质不是前后端分离,而是把网页当作独立的应用程序(app).前后端分离只是实 ...

  10. ZOJ1450 Minimal Circle

    You are to write a program to find a circle which covers a set of points and has the minimal area. T ...