链接:
http://poj.org/problem?id=1789
代码:
#include <cstdio> #include <cstring> #include <cstdlib> #include <cmath> #include <iostream> #include <algorithm> using namespace std; const int N = 2005; const int INF = 0xfffffff; int n; int J[N][N], dist[N]; char s[N][10]; bool vis[N]; int Len(char a[], char b[]) { int ans=0; for(int i=0; i<7; i++) { if(a[i]!=b[i]) ans++; } return ans; } int Prim() { int i, j, ans=0; memset(vis, 0, sizeof(vis)); vis[1]=1; dist[1]=0; for(i=1; i<=n; i++) dist[i]=J[1][i]; for(i=1; i<n; i++) { int MIN=INF; int index=1; for(j=1; j<=n; j++) { if(!vis[j] && dist[j]<MIN) { index=j; MIN=dist[j]; } } vis[index]=1; if(MIN==INF) break; ans += MIN; for(j=1; j<=n; j++) { if(!vis[j] && dist[j]>J[index][j] && J[index][j]>0 ) dist[j]=J[index][j]; } } return ans; } int main () { while(scanf("%d", &n), n) { int i, j; memset(s, 0, sizeof(s)); for(i=1; i<=n; i++) scanf("%s", s[i]); for(i=1; i<=n; i++) for(j=1; j<=i; j++) { J[i][j]=J[j][i]=Len(s[i], s[j]); } int ans=Prim(); printf("The highest possible quality is 1/%d.\n", ans); } return 0; }
时间: 2024-12-15 02:00:53