G. Truck History

來源:互聯網
上載者:User
                                                 G. Truck HistoryTime Limit:2000msCase Time Limit:2000msMemory Limit:65536KB64-bit integer IO format: %lld      Java class name:
Main SubmitStatus

PID: 1908 Font Size: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
4aaaaaaabaaaaaaabaaaaaaabaaaa0
Sample Output
The highest possible quality is 1/3.

解題思路:該題為最短路徑問題,直接用克魯斯卡爾演算法加個並查集即可,只是,前面要預先處理一下。

有很多卡車,他們功能不同,編號不同,每輛卡車的編號是7個小寫字母要求輸出1/Σ(to,td)d(to,td),要最大值,也就是說Σ(to,td)d(to,td)的最小值。公式理解是一大痛點,Σ(to,td)d(to,td)他這裡是要求卡車t0和td編號不同的字母個數,前面的Σ(to,td)是累加,就是t是從t0到td的意思。

也就是說要求各輛卡車編號不同字母的最短路徑,卡車ti到ti+1的路勁長度是這兩輛卡車編號不同的字母個數。所以,我們的預先處理就是要得出卡車(ti,ti+1)的標號的不同字母個數(i=0;i<m-1;i++),直接儲存到結構體數組中備用。後面再用結構體數組中資訊用克魯斯卡爾演算法加並查集求出最短路徑即可。


#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;int n;int t[2002];  //用於並查集,記錄節點i的根節點struct node    //用於並查集,儲存兩輛卡車的編號不同字母情況{    int x;    int y;    int d;    //距離}relat[4000000];void exchang()    //用於預先處理,得出每兩輛卡車的編號不同字母情況{    int trick[2002][8];  //用於預先處理,得出每兩輛卡車的編號    int i,j,cou;    int m;    for(i=0;i<n;i++)    //讀入卡車編號資訊        scanf("%s",trick[i]);    cou=0;    for(i=0;i<n;i++)   //處理得出卡車i和卡車j編號中不同字母的個數        for(j=i+1;j<n;j++)        {            int sum=0;            for(int k=0;k<7;k++)   //每個字母都檢索比較            {                if(trick[i][k]!=trick[j][k])                    sum++;            }            relat[cou].x=i;    //存入結構體數組,用於最短路徑求解            relat[cou].y=j;            relat[cou].d=sum;            cou++;        }}bool cmp(node a,node b)   //排序,把結構體數組按距離d的值從小到大排序{    return a.d<b.d;}int find(int x)    //用於並查集,需找節點x的根節點{    while(x!=t[x])        x=t[x];    return x;}int MST()    //克魯斯卡爾最短路徑演算法{    int m=n*(n-1);    //路徑條數    int sum=0,i;    sort(relat,relat+m,cmp);   //按距離排序    for(i=0;i<n;i++)    //每個節點的根節點都是自己本身        t[i]=i;    for(i=0;i<m;i++)    {        int x_f=find(relat[i].x);        int y_f=find(relat[i].y);        if(x_f!=y_f)    //若兩個節點不再同一集合,則合并這兩個集合        {            sum+=relat[i].d;            t[y_f]=x_f;        }    }    return sum;}int main(){    while(scanf("%d",&n)&&n)    {        exchang();    //預先處理        printf("The highest possible quality is 1/%d.\n",MST());  //求出最短路徑,輸出    }    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.