小记:题目要弄清,之后就简单了。
题意:每两点之间的距离就是两个字符串的不同字符的个数,每个位置与每个位置的比较的不同的个数。
然后就是普里姆算法,求出最小生成树即是Q
代码:
#include <iostream> #include <stdio.h> #include <string.h> #include <math.h> #include <stdlib.h> #include <map> #include <set> #include <vector> #include <stack> #include <queue> #include <algorithm> #include <string> using namespace std; #define mst(a,b) memset(a,b,sizeof(a)) #define REP(a,b,c) for(int a = b; a < c; ++a) #define eps 10e-8 const int MAX_ = 2010; const int N = 100010; const int INF = 0x7fffffff; int n; struct node{ int s, t; }p[MAX_*30]; int g[MAX_][MAX_], tmp[MAX_]; int d[MAX_]; bool vis[MAX_]; int m, cnt, L[5],C[5]; char str[MAX_][MAX_]; int prim() { int sum = 0; REP(i, 0, n){ d[i] = INF; vis[i] = 0; } d[0] = 0; REP(c, 0, n){ int k, mmin = INF; REP(i, 0, n){ if(!vis[i] && mmin > d[i]){ mmin = d[i]; k = i; } } sum += mmin; vis[k] = 1; REP(i, 0, n){ if(!vis[i] && g[k][i] && d[i] > g[k][i]){ d[i] = g[k][i]; } } } return sum; } int main(){ int T, ss, tt, v, Ca = 0, s, t; //scanf("%d", &T); while(scanf("%d",&n), n){ REP(i, 0, n)scanf("%s", str[i]); mst(g, 0); REP(i, 0, n)REP(j, i+1, n){ int len = strlen(str[i]); REP(k, 0, len){ if(str[i][k] != str[j][k]){ g[i][j] ++; g[j][i] ++; } } } printf("The highest possible quality is 1/%d.\n", prim()); } return 0; }