本文主要是介绍POJ1789(最小生成树),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
POJ1789(最小生成树)
Truck History
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.
题意
历史上,曾用7个小写字母来表示每种truck的型号,每两种型号之间的差距为字母串中不同字母的个数。现在给出n种不同型号的truck,问怎样使
**1/Σ(to,td)d(to,td)**的值最小。
思路
对于每一行输入,看作为一个点。和其他输入的不同的字母个数为到其他点的距离。任意两点之间都存在路径,是一个完全图。
在完全图中,权重为不同字母的个数。求最小生成树。
代码
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;public class TruckHistory {public static void main(String[] args) {Scanner sc = new Scanner(System.in);while (true) {int n = sc.nextInt();if (n == 0) {break;}List<String> strs = new ArrayList<String>();for (int i = 0; i < n; i++) {String str = sc.next();strs.add(str);}int[][] graph = new int[n][n];for (int i = 0; i < n; i++) {for (int j = i; j < n; j++) {if (i == j) {graph[i][j] = 0;} else {char[] a = strs.get(i).toCharArray();char[] b = strs.get(j).toCharArray();int w = 0;for (int k = 0; k < a.length; k++) {if (a[k] != b[k]) {w++;}}graph[i][j] = w;graph[j][i] = w;}}}int sum = prim(graph, 0);System.out.println("The highest possible quality is 1/" + sum + ".");}}public static int prim(int[][] graph, int startPoint) {int result = 0;int[] lowcost = new int[graph.length];int[] closest = new int[graph.length];int minDis = Integer.MAX_VALUE;for (int i = 0; i < lowcost.length; i++) {//lowcost和closest的初始值lowcost[i] = graph[startPoint][i];closest[i] = startPoint;}for (int i = 0; i < lowcost.length - 1; i++) {//除了startPoint之外的n-1个点minDis = Integer.MAX_VALUE;int index = startPoint;for (int j = 0; j < lowcost.length; j++) {if (lowcost[j] != 0 && lowcost[j] < minDis) {minDis = lowcost[j];index = j;}}result = result + minDis;lowcost[index] = 0;for (int j = 0; j < lowcost.length; j++) {//更新lowcost和closestif (graph[index][j] != 0 && graph[index][j] < lowcost[j]) {lowcost[j] = graph[index][j];closest[j] = index;}}}return result;}
}
这篇关于POJ1789(最小生成树)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!