POJ1789 Truck History【Prim】

Truck History

Time Limit: 2000MS
Memory Limit: 65536K

Total Submissions: 19456
Accepted: 7498

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.

Source

CTU Open 2003

题目大意:给你N个字符串,每个字符串代表一个结点,每个结点之间的距离为字符串中

不同字符的个数。比如:"abaaaaa"和"aabaaaa",第二个和第三个字符不同,两个结点

之间的距离就是2。以此类推,得到所有的结点。求所有结点构成图的最小生成树。

思路:按题意算出各结点之间的距离,存入图中,用Prim算法求解,注意输出格式。

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;

int Map[2010][2010];
char MAP[2010][8];
int low[2010],vis[2010];

int Prim(int N)
{
    memset(vis,0,sizeof(vis));
    int ans = 0;
    vis[1] = 1;
    int pos = 1;
    for(int i = 1; i <= N; ++i)
        if(i != pos)
            low[i] = Map[pos][i];

    for(int i = 1; i < N; ++i)
    {
        int Min = 0xffffff0;
        for(int j = 1; j <= N; ++j)
        {
            if(Min > low[j] && !vis[j])
            {
                 Min = low[j];
                 pos = j;
            }
        }
        ans += Min;
        vis[pos] = 1;
        for(int j = 1; j <= N; ++j)
        {
            if(!vis[j] && Map[pos][j] < low[j])
            {
                low[j] = Map[pos][j];
            }
        }
    }
    return ans;
}

void GetMap(int N)
{
    memset(Map,0xffffff0,sizeof(Map));
    for(int i = 1; i <= N; ++i)
    {
        for(int j = i; j <= N; ++j)
        {
            int sum = 0;
            for(int k = 0; k < 7; ++k)
            {
                if(MAP[i][k] != MAP[j][k])
                    sum++;
            }
            Map[i][j] = Map[j][i] = sum;
            if(i == j)
                Map[i][j] = 0;
        }
    }
}
int main()
{
    int N;
    while(~scanf("%d",&N) && N)
    {
        memset(MAP,0,sizeof(MAP));
        for(int i = 1; i <= N; ++i)
            scanf("%s",MAP[i]);
        GetMap(N);
        int ans = Prim(N);
        printf("The highest possible quality is 1/%d.\n",ans);
    }
    return 0;
}
时间: 2024-10-21 05:57:34

POJ1789 Truck History【Prim】的相关文章

POJ 1789:Truck History 【Prim】

Truck History Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) Total Submission(s) : 123   Accepted Submission(s) : 36 Problem Description Advanced Cargo Movement, Ltd. uses trucks of different types. Some trucks are

POJ1789 Truck History 【最小生成树Prim】

Truck History Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 18981   Accepted: 7321 Description Advanced Cargo Movement, Ltd. uses trucks of different types. Some trucks are used for vegetable delivery, other for furniture, or for brick

1789 Truck History【最小生成树】

Truck History Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 21660   Accepted: 8419 Description Advanced Cargo Movement, Ltd. uses trucks of different types. Some trucks are used for vegetable delivery, other for furniture, or for brick

POJ1789:Truck History(Prim算法)

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. Th

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

链接:http://poj.org/problem?id=1789 题意:卡车公司有悠久的历史,它的每一种卡车都有一个唯一的字符串来表示,长度为7,它的所有卡车(除了第一辆)都是由以前的卡车派生出来的.现在一些砖家想研究这些卡车的历史,他们定义了卡车类型编码的距离:卡车编码字符串(长度7)相同位置字符不同的个数.比如一个卡车编码是aaaaaaa,另一个是bbaaaaa,则他们的距离是2,.他们又定义了派生方案的优劣值:1/Σ(to,td)d(to,td). 其中t0为基类型,td为派生类型,d(

HDOJ 1233 还是畅通工程 【最小生成树】+【prim】

题意:... 策略:最最典型的prim算法. 代码: #include<stdio.h> #include<string.h> #define INF 0x3f3f3f3f #define MAXN 105 int map[MAXN][MAXN], di[MAXN], vis[MAXN]; int n; int prim() { int i, j, min, pos; memset(vis, 0, sizeof(vis)); memset(di, 0, sizeof(di)); p

HDU1162 Eddy&#39;s picture【Prim】

Eddy's picture Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 7231    Accepted Submission(s): 3656 Problem Description Eddy begins to like painting pictures recently ,he is sure of himself to

poj 1789 Truck History 最小生成树 prim

Truck History Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 19122   Accepted: 7366 Description Advanced Cargo Movement, Ltd. uses trucks of different types. Some trucks are used for vegetable delivery, other for furniture, or for brick

POJ 1789 -- Truck History(Prim)

 POJ 1789 -- Truck History Prim求分母的最小.即求最小生成树 1 #include<iostream> 2 #include<cstring> 3 #include<algorithm> 4 using namespace std; 5 const int maxn = 2000 + 10; 6 const int INF = 1000000; 7 int n;//有几个卡车 8 char str[maxn][10]; 9 int d[ma