Question link: http://poj.org/problem? Id = 1789
Given n rows, each line contains 7 characters, you can calculate the difference between all rows and rows (that is, at the same position, the letters are not the same), one location is 1, accumulate in sequence. Ask the minimum difference.
I didn't understand the meaning of the question. This is a complete graph, that is, each edge is connected to all edges except it. The weight of an edge is calculated based on the difference.
1 #include <iostream> 2 #include <cstdio> 3 using namespace std; 4 5 const int maxn = 2000 + 10; 6 const int INF = 1e9; 7 int truck[maxn][maxn]; 8 int dist[maxn], vis[maxn]; 9 char input[maxn][maxn];10 int ans, N;11 12 void prim()13 {14 int k;15 for (int i = 1; i <= N; i++)16 {17 vis[i] = 0;18 dist[i] = INF;19 }20 dist[1] = 0;21 for (int i = 1; i <= N; i++)22 {23 int tmp = INF;24 for (int j = 1; j <= N; j++)25 {26 if (!vis[j] && dist[j] < tmp)27 {28 tmp = dist[j];29 k = j;30 }31 }32 vis[k] = 1;33 ans += tmp;34 for (int j = 1; j <= N; j++)35 {36 if (!vis[j] && dist[j] > truck[k][j])37 dist[j] = truck[k][j];38 }39 }40 }41 42 int main()43 {44 while (scanf("%d", &N) != EOF && N)45 {46 for (int i = 1; i <= N; i++)47 scanf("%s", &input[i]);48 int cnt;49 for (int i = 1; i < N; i++)50 {51 for (int j = i+1; j <= N; j++)52 {53 cnt = 0;54 for (int k = 0; k < 7; k++)55 {56 if (input[i][k] != input[j][k])57 cnt++;58 }59 truck[i][j] = truck[j][i] = cnt;60 }61 }62 ans = 0;63 prim();64 printf("The highest possible quality is 1/%d.\n", ans);65 }66 return 0;67 }