Historically, seven lower-case letters were used to represent each truck model. The gap between the two models was the number of different letters in the string. Now we have provided n Different Types of truck, and asked how to minimize the value of 1/Σ (to, td) d (to, td. (That is, find the shortest path connecting all truck. The typical minimal spanning tree problem is that the Prim algorithm is suitable for dense graphs, and the Kruskal algorithm is suitable for sparse graphs. You can use prim and kruskal methods. This is a dense graph. The most specific path of the minimal spanning tree:
[Csharp]
# Include "stdio. h"
# Include "string. h"
# Define INF 1000000000
Int mark [2001], f [2001], n;
Char str [2, 2001] [8];
Int fun (int I, int j) // The distant between two models is the number of different letters in the string!
{
Int t, k;
T = 0;
For (k = 0; k <7; k ++)
{
If (str [I] [k]! = Str [j] [k])
T ++;
}
Return t;
}
Void prime ()
{
Int I, j, min, k, ans;
Memset (mark, 0, sizeof (mark ));
For (I = 0; I <n; I ++)
F [I] = fun (0, I );
F [0] = 0; mark [0] = 1;
Ans = 0;
For (I = 0; I <n-1; I ++)
{
Min = INF;
For (j = 0; j <n; j ++)
{
If (! Mark [j] & min> f [j])
{
Min = f [j]; k = j;
}
}
Ans + = min;
Mark [k] = 1;
For (j = 0; j <n; j ++)
{
If (! Mark [j] & f [j]> fun (k, j ))
F [j] = fun (k, j );
}
}
Printf ("The highest possible quality is 1/% d. \ n", ans );
}
Int main ()
{
Int I, j;
While (scanf ("% d", & n )! =-1)
{
If (n = 0) break;
For (I = 0; I <n; I ++)
Scanf ("% s", str [I]);
Prime ();
}
Return 0;
}
Author: yyf572132811