POJ 1789 Truck History (最小產生樹)__圖論

來源:互聯網
上載者:User
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) 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

4aaaaaaabaaaaaaabaaaaaaabaaaa0


Sample Output

The highest possible quality is 1/3.


題意

用一個7位的string代表一個編號,兩個編號之間的distance代表這兩個編號之間不同字母的個數。一個編號只能由另一個編號衍生出來,代價是這兩個編號之間相應的distance,現在要找出一個衍生方案,使得所有的編號之間都可以直接或者間接形成轉換,並且總代價最小,也就是distance之和最小。


思路

既然所有的編號之間都有一個轉換權值,那麼把它可以表示成一張完全圖。

要求可以直接或者間接形成轉換,即需要找一個子圖必須連通。

權值最小,即最小產生樹。


AC 代碼

#include <iostream>#include<stdio.h>#include<stdlib.h>#include<string.h>#include<map>#include<algorithm>using namespace std;char str[2005][10];int parent[2005],n,etop;struct eage     //邊的結構體,u、v為兩端點,w為邊權值{    int u, v, w;    eage() {};    eage(int u,int v,int w)    {        this->u=u;        this->v=v;        this->w=w;    }} EG[2005*2005];int jud(int a,int b){    int ans=0;    for(int i=0; i<7; i++)        if(str[a][i]!=str[b][i])            ans++;    return ans;}bool cmp(eage a, eage b)    //排序調用{    return a.w < b.w;}int Find(int x)     //尋找根節點,判斷是否在同一棵樹中的依據{    if(parent[x] == -1) return x;    return Find(parent[x]);}void Kruskal()      //Kruskal演算法,parent能夠還原一棵產生樹,或者森林{    memset(parent, -1, sizeof(parent));    int cnt = n,ans=0;        //初始時根節點數目為n個    sort(EG, EG+etop, cmp);    //按權值將邊從小到大排序    for(int i = 0; i < etop; i++)     //按權值從小到大選擇邊    {        if(cnt == 1) break;     //當根節點只有1個時,跳出迴圈        int t1 = Find(EG[i].u), t2 = Find(EG[i].v);        if(t1 != t2)    //若不在同一棵樹種則選擇該邊,        {            ans += EG[i].w;            parent[t1] = t2;            cnt--;      //每次合并,減少一個根節點        }    }    printf("The highest possible quality is 1/%d.\n",ans);}int main(){    while(~scanf("%d%*c",&n)&&n)    {        etop=0;        for(int i=0; i<n; i++)            gets(str[i]);        for(int i=0; i<n; i++)            for(int j=i+1; j<n; j++)            {                int cnt=jud(i,j);                EG[etop++]=eage(i,j,cnt);                EG[etop++]=eage(j,i,cnt);            }        Kruskal();    }    return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.