题目连接: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. hdu DIY FLIGHT GAME (dfs)

    FLIGHT GAME Time Limit : 3000/1000ms (Java/Other)   Memory Limit : 65535/32768K (Java/Other) Total S ...

  2. 在.cs代码文件中无法识别控件

    原因:由于直接复制别人的网页文件到项目. 解决方案,自己右键,新建网页,再把控件代码复制到 aspx和 cs

  3. 【题解】CQOI2017老C的方块

    网络流真的是一种神奇的算法.在一张图上面求感觉高度自动化的方案一般而言好像都是网络流的主阵地.讲真一开始看到这道题也有点懵,题面很长,感觉很难的样子.不过,仔细阅读了题意之后明白了:我们所要做的就是要 ...

  4. [Leetcode] Swap nodes in pairs 成对交换结点

    Given a linked list, swap every two adjacent nodes and return its head. For example,Given1->2-> ...

  5. 【BZOJ 2241 打地鼠】

    Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 1430  Solved: 908[Submit][Status][Discuss] Descripti ...

  6. TCP ------ TCP创建服务器中出现的套接字

    在服务器端,socket()返回的套接字用于监听(listen)和接受(accept)客户端的连接请求.这个套接字不能用于与客户端之间发送和接收数据. accept()接受一个客户端的连接请求,并返回 ...

  7. iebackground+icon图标兼容

    <!DOCTYPE > <html> <head> <title>zepto</title> <meta name="nam ...

  8. springmvc4+hibernate4+activiti5.18(Maven)

    项目下载地址: http://files.cnblogs.com/files/walk-the-Line/springmvc_activiti5.18_hibernate4.zip

  9. GDSOI2015的某道题目

    分析: 看到这个$3^i$就觉得很奇怪的样子...为什么一定要是$3^i$...而且不能重复使用... 不能重复使用就代表不会产生进位,那么一定是若干个$3^i$相加减的式子... 仔细观察,我们发现 ...

  10. 【BZOJ】1691: [Usaco2007 Dec]挑剔的美食家

    [算法]扫描线+平衡树(set) [题解]很明显的二维偏序数点,排序后扫描线,现加点后查询答案. 则问题转化为一维偏序,显然贪心找第一个比当前大的最优,所以用平衡树维护. 记得开multiset!!! ...