Poj 1789 truck history

Source: Internet
Author: User

Minimum Spanning Tree problem.

Give you a set of letter sequences and ask you the most likely evolution, that is, to minimize the cost of connecting all sequences.


The cost of each derivation depends on the number of different letters on two strings.

Then calculate the cost, and then Kruskal calculates the minimum value.


# Include <cstdio> # include <cstring> # include <string> # include <queue> # include <algorithm> # include <queue> # include <map> # include <stack> # include <iostream> # include <list> # include <set> # include <cmath> # define INF 0x7fffffff # define EPS 1e-6using namespace STD; int n, m; int Fa [2001]; struct lx {int U, V, Len;} l [2000001]; int father (int x) {If (X! = Fa [x]) Fa [x] = Father (Fa [x]); Return Fa [X];} bool CMP (lx a, lx B) {return. len <B. len;} Char STR [2001] [8]; int getlen (char * a, char * B) {int ans = 0; For (INT I = 0; I <7; I ++) if (a [I]! = B [I]) ans ++; return ans;} int main () {While (scanf ("% d", & N), n) {for (INT I = 1; I <= N; I ++) {Fa [I] = I; scanf ("% s", STR [I]);} int COT = 0; For (INT I = 1; I <= N; I ++) {for (Int J = I + 1; j <= N; j ++) {L [cot]. U = I; L [cot]. V = J; L [cot ++]. len = getlen (STR [I], STR [J]) ;}} sort (L, L + cot, CMP); int ans = 0; For (INT I = 0; I <cot; I ++) {int Fu = Father (L [I]. u); int FV = Father (L [I]. v); If (Fu = FV) continue; Fa [FV] = Fu; ans + = L [I]. len;} printf ("the highest possible quality is 1/% d. \ n ", ANS );}}


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.