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 where the sum goes over all pairs of types in the derivation plan such that t o is the original type and t d the type derived from it and d(t o,t d) 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. Inputide
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.
Outputthis
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 Inputspa 4 aaaaaaa baaaaaa abaaaaa aabaaaa 0 Sample Outputcode The highest possible quality is 1/3. Sourceblog
大意:最小生成樹,即把全部定點連起來所須要的最短長度,第一行輸入一個整數表明下面貨車總數,第一行是源頭,不能與其餘比,下面n-1行是衍生出來的,把兩組串不一樣的個數看做是路徑,要你求從源頭開始把全部串起來的最小生成樹。
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int inf = 0x3f3f3f3f,MAX = 3000; int map[MAX][MAX],k[MAX]; int m,ans; void prim (int map[MAX][MAX],int n){ ans = 0; for(int i = 1; i <= n ; i++) k[i] = map[1][i]; for(int i = 2; i <= n ; i++){ int min1 = inf; m = 0; for(int j = 2; j <= n; j++){ if(k[j] < min1&&k[j]!=0){ min1 = k[j]; m = j; } } //用來找最小的現有的邊 ans +=min1; k[m] = 0;//把拿出的邊弄掉 for(int j = 2; j <= n ; j++){ if(map[m][j] < k[j] && k[j] !=0){ k[j] = map[m][j];//把以當前爲頂點的到周圍的最小的邊拿出來 } } } printf("The highest possible quality is 1/%d.\n",ans); } int main() { int T,i,j; char s[2000][8]; while(~scanf("%d",&T)&&T){ memset(map,0,sizeof(map)); memset(s,0,sizeof(s)); for(int i = 1; i <= T;i++){ map[i][i] = 0; scanf("%s",s[i]); for(int j = 1; j <= i;j++){ int d = 0; for(int k = 0; k < 7; k++){ if(s[i][k]!=s[j][k]) d++; } map[i][j] = map[j][i] = d; } } prim(map,T); } return 0; } 最小生成樹算法:一個大for兩個小for有點像dijkstra算法,不過它的第一個小for是用來的獲得以當前點爲中心,向四周的最小的路徑(其實也不許確,由於一旦該最短路徑取出,那麼以該最短路徑的另外一個頂點,來得到最小值),第二個小for就是用來比較該頂點出發的點與原來的點,若是比原來點小就拿進來固然要用if(k[i]!=0)由於兩個小for之間已經把該m點變成0了,用ans來記錄每個狀況的最小邊,最後輸出.ip |