【POJ - 1789】【ZOJ - 2158】【SCU - 1832】Truck History (最小生成树)
題干:
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 0Sample Output
The highest possible quality is 1/3.Source
CTU Open 2003
?
題意:
? ? ??給出 n 種卡車,每種卡車的類型是由七個(gè)字符組成的,一種卡車可以從另一種卡車派生而來(lái),所需要的代價(jià)是兩種卡車類型不同的字符個(gè)數(shù),求出這 n 種卡車派生的最小代價(jià), n 種車有一 種是開(kāi)始就已經(jīng)有的,其余的 n-1 種是派生出來(lái)的。
? ? ? 用一個(gè)7位的string代表一個(gè)編號(hào),兩個(gè)編號(hào)之間的distance代表這兩個(gè)編號(hào)之間不同字母的個(gè)數(shù)。一個(gè)編號(hào)只能由另一個(gè)編號(hào)“衍生”出來(lái),代價(jià)是這兩個(gè)編號(hào)之間相應(yīng)的distance,現(xiàn)在要找出一個(gè)“衍生”方案,使得總代價(jià)最小,也就是distance之和最小。
解題報(bào)告:
讀入字符串后先將字符串處理一下,并將權(quán)值算好后 入node結(jié)構(gòu)體,然后對(duì)結(jié)構(gòu)體排序等等 就是最小生成樹(shù)裸題了。
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 ;}總結(jié):
? ? 1.這波太難受了啊。 ?
? ? ? ? ? 1RE。因?yàn)樽x字符串的時(shí)候i<=MAX了!這樣是不對(duì)的,因?yàn)殚_(kāi)的數(shù)組大小是MAX所以這里不能小于等于!所以最好就是讀數(shù)據(jù)的時(shí)候?qū)?lt;=n,別太洋氣的寫<=MAX,如果要這么寫的話,你開(kāi)數(shù)組就得寫MAX+c了!并且發(fā)現(xiàn)top和ans沒(méi)有初始化。
? ? ? ? ? ?2RE。因?yàn)閿?shù)組開(kāi)小了。node數(shù)組應(yīng)該開(kāi)MAX*MAX+c!
? ? ? ? ? ?3WA。因?yàn)檩敵龈袷讲粚?duì)。。。。少了個(gè)句點(diǎn)你能信。、。。
? ? ? ? ? ?4AC ? ?
? ?2. 我們來(lái)算一下時(shí)間復(fù)雜度吧。你在讀數(shù)據(jù)后遍歷的時(shí)候,?兩層for循環(huán)到n,所以時(shí)間復(fù)雜度是n^2的還可以。加一個(gè)克魯斯科爾 排序 o(eloge)+遍歷o(e)的。
總結(jié)
以上是生活随笔為你收集整理的【POJ - 1789】【ZOJ - 2158】【SCU - 1832】Truck History (最小生成树)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 网传华为奇瑞联手“造车”官方回应:暂无相
- 下一篇: 12个月限免开卡:苹果Apple Pay