标签:
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 21535 | Accepted: 8380 |
Description
Input
Output
Sample Input
4 aaaaaaa baaaaaa abaaaaa aabaaaa 0
Sample Output
The highest possible quality is 1/3.
Source
1 #include <cstdio> 2 #include <cstring> 3 #include <iostream> 4 using namespace std; 5 int map[2020][2020], dis[2020]; bool vis[2020]; 6 const int INF = 0x3f3f3f3f; 7 char str[2020][8]; 8 int n; 9 int Deal(int a, int b) //求权值; 10 { 11 int m = 0; 12 for(int i = 0; i < 7; i++) 13 if(str[a][i] != str[b][i]) 14 m++; 15 return m; 16 } 17 void Prim() 18 { 19 int sum = 0; 20 memset(vis, true, sizeof(vis)); 21 for(int i = 1; i <= n; i++) 22 dis[i] = map[1][i]; 23 vis[1] = false; 24 for(int i = 1; i < n; i++) 25 { 26 int temp, min = INF; 27 for(int j = 1; j <= n; j++) 28 { 29 if(vis[j] && dis[j] < min) 30 { 31 min = dis[j]; 32 temp = j; 33 } 34 } 35 if(min == INF) 36 return ; 37 vis[temp] = false; 38 sum += min; 39 for(int j = 1; j <= n; j++) 40 if(vis[j] && dis[j] > map[temp][j]) 41 dis[j] = map[temp][j]; 42 } 43 printf("The highest possible quality is 1/%d.\n", sum); 44 } 45 int main() 46 { 47 while(~scanf("%d", &n), n) 48 { 49 for(int i = 1; i <= n; i++) 50 scanf("%s", str[i]); 51 for(int i = 1; i <= n; i++) 52 { 53 map[i][i] = 0; 54 for(int j = 1; j < i; j++) 55 map[i][j] = map[j][i] = Deal(i, j); 56 } 57 Prim(); 58 } 59 return 0; 60 }
标签:
原文地址:http://www.cnblogs.com/fengshun/p/4722125.html