poj 1789 Truck History
题目连接:http://poj.org/problem?id=1789
每条边的权值为任意两truck不相等字母的数量,求最小生成树
代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <iostream> 2 #include <stdio.h> 3 #include <string.h> 4 #define max 1000000 5 using namespace std; 6 int map[2005][2005]; 7 int ans; 8 void prim(int n) 9 { 10 int i,pre,j,min; 11 int vis[2005] = {0}; 12 int d[2005]; 13 for(i = 1;i < n;i++) 14 d[i] = map[0][i]; 15 16 vis[0] = 1; 17 18 for(i = 1;i < n;i++) 19 { 20 min = max; 21 for(j = 1;j < n;j++) 22 { 23 if(min > d[j] && !vis[j]) 24 min = d[j],pre = j; 25 } 26 ans += min; 27 vis[pre] = 1; 28 for(j = 1;j < n;j++) 29 { 30 if(d[j] > map[pre][j] && !vis[j]) 31 d[j] = map[pre][j]; 32 } 33 34 } 35 return ; 36 } 37 int main() 38 { 39 int n,i,j; 40 41 char s[2005][10]; 42 while(scanf("%d",&n)&&n) 43 { 44 for(i = 0;i < n;i++) 45 cin>>s[i]; 46 47 for(i = 0;i < n;i++) 48 { 49 for(j = 0;j < n;j++) 50 { 51 int w; 52 w = 0; 53 for(int k = 0;k < 7;k++) 54 if(s[i][k] != s[j][k]) 55 w++; 56 map[i][j] = w; 57 } 58 } 59 60 ans = 0; 61 prim(n); 62 cout << "The highest possible quality is 1/"<< ans << "."<<endl; 63 } 64 return 0; 65 }