日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【POJ - 1789】【ZOJ - 2158】【SCU - 1832】Truck History (最小生成树)

發(fā)布時間:2023/12/10 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【POJ - 1789】【ZOJ - 2158】【SCU - 1832】Truck History (最小生成树) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

題干:

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)


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

4 aaaaaaa baaaaaa abaaaaa aabaaaa 0

Sample Output

The highest possible quality is 1/3.

Source

CTU Open 2003

?

題意:

? ? ??給出 n 種卡車,每種卡車的類型是由七個字符組成的,一種卡車可以從另一種卡車派生而來,所需要的代價是兩種卡車類型不同的字符個數(shù),求出這 n 種卡車派生的最小代價, n 種車有一 種是開始就已經(jīng)有的,其余的 n-1 種是派生出來的。

? ? ? 用一個7位的string代表一個編號,兩個編號之間的distance代表這兩個編號之間不同字母的個數(shù)。一個編號只能由另一個編號“衍生”出來,代價是這兩個編號之間相應的distance,現(xiàn)在要找出一個“衍生”方案,使得總代價最小,也就是distance之和最小。

解題報告:

讀入字符串后先將字符串處理一下,并將權值算好后 入node結構體,然后對結構體排序等等 就是最小生成樹裸題了。

ac代碼:

#include<iostream> #include<cstdio> #include<algorithm> using namespace std; const int MAX = 10000 + 5 ;int f[MAX+5]; int n; char s[MAX + 5][10]; int ans; int top; struct Node {int u,v,dist; } node[50000000 + 5];int getf(int v) {return f[v] == v ? v : f[v] = getf(f[v] ); } void merge(int u, int v) {int t1 = getf(u );int t2 = getf(v );if(t1 != t2) {f[t2] =t1;}} bool cmp(const Node & a,const Node & b) {return a.dist<b.dist; } int main() {int sum;while(scanf("%d",&n) && n ) {for(int i = 0 ; i<=n; i++) {f[i]=i;}ans = 0;top = 0;for(int i = 1; i<=n; i++) {scanf("%s",s[i]);}for(int i = 1; i<=n; i++) {for(int j = 1; j<=n; j++) {sum = 0;for(int k = 0 ; k<7; k++) {if(s[i][k]!=s[j][k] ) sum++;}++top;node[top].u = i;node[top].v = j;node[top].dist = sum; // merge(i,j);}}sort(node+1,node+top+1,cmp);int m=0;//代表邊數(shù) for(int i = 1; i<=top; i++) {if(getf(node[i].u) != getf(node[i].v) ) {ans +=node[i].dist;merge(node[i].u,node[i].v);m++;if(m== n-1) break;}}printf("The highest possible quality is 1/%d.\n",ans); }return 0 ;}

總結:

? ? 1.這波太難受了啊。 ?

? ? ? ? ? 1RE。因為讀字符串的時候i<=MAX了!這樣是不對的,因為開的數(shù)組大小是MAX所以這里不能小于等于!所以最好就是讀數(shù)據(jù)的時候寫<=n,別太洋氣的寫<=MAX,如果要這么寫的話,你開數(shù)組就得寫MAX+c了!并且發(fā)現(xiàn)top和ans沒有初始化。

? ? ? ? ? ?2RE。因為數(shù)組開小了。node數(shù)組應該開MAX*MAX+c!

? ? ? ? ? ?3WA。因為輸出格式不對。。。。少了個句點你能信。、。。

? ? ? ? ? ?4AC ? ?

? ?2. 我們來算一下時間復雜度吧。你在讀數(shù)據(jù)后遍歷的時候,?兩層for循環(huán)到n,所以時間復雜度是n^2的還可以。加一個克魯斯科爾 排序 o(eloge)+遍歷o(e)的。

總結

以上是生活随笔為你收集整理的【POJ - 1789】【ZOJ - 2158】【SCU - 1832】Truck History (最小生成树)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。