POJ 1789Truck History(pirme)

Truck History

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 22648   Accepted: 8781

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.

题意:每输入一个字符串代表一个节点,而节点之间的距离就是不同字符串字对应位置不同的字符个数,然后求最小生成树不同题意的时候完全不知道怎么做,当知道是距离是什么和是求最小生成树的时候就成了水题

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5
 6 using namespace std;
 7 const int INF = 100000000;
 8 int n;
 9 char str[2000 + 10][10];
10 int edge[2000 + 10][2000 + 10],dist[2000 + 10],vis[2000 + 10];
11 int weight(char a[],char b[])
12 {
13     int ans = 0;
14     for(int i = 0; i < 7; i++)
15         if(a[i] != b[i])
16         ans ++;
17     return ans;
18 }
19 void prime()
20 {
21     memset(vis,0,sizeof(vis));
22     for(int i = 2; i <= n; i++)
23     {
24         dist[i] = edge[1][i];
25     }
26     vis[1] = 1;
27     dist[1] = 0;
28     int sum = 0;
29     for(int i = 1; i < n; i++)
30     {
31         int minn = INF,pos;
32         for(int j = 1; j <= n; j++)
33         {
34             if(vis[j] == 0 && dist[j] < minn)
35             {
36                 minn = dist[j];
37                 pos = j;
38             }
39         }
40         sum += minn;
41         vis[pos] = 1;
42         for(int j = 1; j <= n; j++)
43         {
44             if(dist[j] > edge[pos][j])
45                 dist[j] = edge[pos][j];
46         }
47     }
48     printf("The highest possible quality is 1/%d.\n",sum);
49 }
50 int main()
51 {
52     while(scanf("%d", &n) != EOF)
53     {
54         if(n == 0)
55             break;
56         for(int i = 1; i <= n; i++)
57         {
58             scanf("%s", str[i]);
59         }
60         for(int i = 1; i < n; i++)
61         {
62             for(int j = i + 1; j <= n; j++)
63             {
64                 edge[i][j] = edge[j][i] = weight(str[i],str[j]);
65             }
66         }
67         prime();
68     }
69     return 0;
70 }

时间: 2024-11-09 01:48:34

POJ 1789Truck History(pirme)的相关文章

POJ 3414 Pots(罐子)

p.MsoNormal { margin-bottom: 10.0000pt; font-family: Tahoma; font-size: 11.0000pt } h1 { margin-top: 5.0000pt; margin-bottom: 5.0000pt; text-align: left; font-family: 宋体; font-weight: bold; font-size: 24.0000pt } span.10 { font-family: "Times New Rom

poj 3399 Product(模拟)

# include <stdio.h> # include <string.h> # include <algorithm> using namespace std; int cmp(int x,int y) { return x>y; } int main() { int a[110],a1[110],a2[110],ans[110]; int n,k,k1,k2,i,k3; while(~scanf("%d%d",&n,&k

POJ 1840 Eqs(暴力)

Description Consider equations having the following form: a1x13+ a2x23+ a3x33+ a4x43+ a5x53=0 The coefficients are given integers from the interval [-50,50]. It is consider a solution a system (x1, x2, x3, x4, x5) that verifies the equation, xi∈[-50,

POJ 3034 Whac-a-Mole(DP)

题目链接 题意 : 在一个二维直角坐标系中,有n×n个洞,每个洞的坐标为(x,y), 0 ≤ x, y < n,给你一把锤子可以打到地鼠,最开始的时候,你可以把锤子放在任何地方,如果你上一秒在(x1,y1),那下一秒直线移动到的整数点(x2,y2)与这个点的距离小于等于d,并且当锤子移动(x2,y2)这个点时,所有在两点的直线上的整点数都可以打到.例如(0,0)移动到(0,3).如果(0,1),(0,2)有老鼠出现就会被打到.求能够打的最多老鼠. 思路 : Dp[i][j][k]代表点(i,j)

poj 2309 BST(数学题)

BST Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8440   Accepted: 5093 Description Consider an infinite full binary search tree (see the figure below), the numbers in the nodes are 1, 2, 3, .... In a subtree whose root node is X, we c

poj 2485 Highways(最小生成树)

题目链接:http://poj.org/problem?id=2485 Description The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has no public highways. So the traffic is difficult in Flatopia. The Flatopian government is aware of this problem. They're plann

poj 1751 Highways (prim )

Highways Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 9080   Accepted: 2536   Special Judge Description The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has a very poor system of public highways. The Flatopian

History(历史)命令用法15例

导读 如果你经常使用 Linux 命令行,那么使用 history(历史)命令可以有效地提升你的效率,本文将通过实例的方式向你介绍 history 命令的 15 个用法. 使用 HISTTIMEFORMAT 显示时间戳 当你从命令行执行 history 命令后,通常只会显示已执行命令的序号和命令本身,如果你想要查看命令历史的时间戳,那么可以执行: # export HISTTIMEFORMAT='%F %T ' # history | more 1 2008-08-05 19:02:39 ser

poj 1861 Network (kruskal)

Network Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 13633   Accepted: 5288   Special Judge Description Andrew is working as system administrator and is planning to establish a new network in his company. There will be N hubs in the c