标签:des style blog http color strong
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 17271 | Accepted: 6621 |
Description
Input
Output
Sample Input
4 aaaaaaa baaaaaa abaaaaa aabaaaa 0
Sample Output
The highest possible quality is 1/3.
这道题就是一个最小生成树。每条字符串代表了一个类型的卡车,不同卡车之间有个演变的距离,我们define为两个字符串间相同位置上不同字母的个数和。可以把这个距离当作权值,
卡车当作点,所求的Q 就是最少生成树。分析出来后就是一个最小生成树的水题。
1 /*====================================================================== 2 * Author : kevin 3 * Filename : TruckHistory.cpp 4 * Creat time : 2014-07-09 09:16 5 * Description : 6 ========================================================================*/ 7 #include <iostream> 8 #include <algorithm> 9 #include <cstdio> 10 #include <cstring> 11 #include <queue> 12 #include <cmath> 13 #define clr(a,b) memset(a,b,sizeof(a)) 14 #define M 2014 15 #define INF 0x7f7f7f7f 16 using namespace std; 17 int c[M][M],dis[M]; 18 char str[M][10]; 19 void BuildGrap(int n) 20 { 21 for(int i = 0; i < n; i++){ 22 for(int j = i+1; j < n; j++){ 23 int d = 0; 24 for(int t = 0; t < 7; t++){ 25 if(str[i][t] != str[j][t]){ 26 d += 1; 27 } 28 } 29 c[i][j] = c[j][i] = d; 30 } 31 } 32 } 33 int prim(int n) 34 { 35 int i,j,k,sum = 0; 36 bool vis[M]; 37 for(i = 0; i < n; i++){ 38 dis[i] = c[0][i]; 39 vis[i] = false; 40 } 41 vis[0] = true; 42 for(i = 1; i < n; i++){ 43 int _min = INF; 44 j = 0; 45 for(k = 0; k < n; k++){ 46 if(_min > dis[k] && !vis[k]){ 47 _min = dis[k]; 48 j = k; 49 } 50 } 51 vis[j] = true; 52 sum += dis[j]; 53 for(k = 0; k < n; k++){ 54 if(dis[k] > c[j][k] && !vis[k]){ 55 dis[k] = c[j][k]; 56 } 57 } 58 } 59 return sum; 60 } 61 int main(int argc,char *argv[]) 62 { 63 int n; 64 while(scanf("%d",&n)!=EOF && n){ 65 clr(c,0); 66 for(int i = 0; i < n; i++){ 67 getchar(); 68 scanf("%s",str[i]); 69 } 70 BuildGrap(n); 71 int ans = prim(n); 72 printf("The highest possible quality is 1/%d.\n",ans); 73 } 74 return 0; 75 }
poj 1789 -- Truck History,布布扣,bubuko.com
标签:des style blog http color strong
原文地址:http://www.cnblogs.com/ubuntu-kevin/p/3832969.html