Poj1789 Truck History (Minimum Spanning Tree)

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.