poj1789

我去,最后的句号漏掉了,找了半天bug,伤不起啊..........

 1 #include <iostream>
 2 #include <string>
 3 #include <vector>
 4 #include <cstdlib>
 5 #include <cmath>
 6 #include <map>
 7 #include <algorithm>
 8 #include <list>
 9 #include <ctime>
10 #include <set>
11 #include <string.h>
12 #include <queue>
13 #include <cstdio>
14 using namespace std;
15 typedef int typec; // type of cost
16 const int V = 2005;
17 const typec inf = 0x3f3f3f3f; // max of cost
18 #define CLR(arr, what) memset(arr, what, sizeof(arr))
19 int vis[V];
20 typec lowc[V];
21 typec cost[V][V];
22 typec prim(typec cost[][V], int n) {
23     int i, j, p;
24     typec minc, res = 0;
25     CLR(vis,0);
26     vis[0] = 1;
27     for (i = 1; i < n; i++)
28         lowc[i] = cost[0][i];
29     for (i = 1; i < n; i++) {
30         minc = inf;
31         p = -1;
32         for (j = 0; j < n; j++)
33             if (0 == vis[j] && minc > lowc[j]) {
34                 minc = lowc[j];
35                 p = j;
36             }
37         if (inf == minc)
38             return -1; //  原图不连通
39         res += minc;
40         vis[p] = 1;
41         for (j = 0; j < n; j++)
42             if (0 == vis[j] && lowc[j] > cost[p][j])
43                 lowc[j] = cost[p][j];
44     }
45     return res;
46 }
47 int dist(string& a, string& b) {
48     int res = 0;
49     for (int i = 0; i < 7; i++) {
50         res += (a[i] != b[i]);
51     }
52     return res;
53 }
54 int main() {
55     int n;
56     cin >> n;
57     string tmp;
58     while (n) {
59         CLR(cost, inf);
60         vector<string> t;
61         for (int abc = 0; abc < n; abc++) {
62             cin >> tmp;
63             t.push_back(tmp);
64         }
65         for (int a = 0; a < n; a++) {
66             for (int b = 0; b < n; b++) {
67                 cost[a][b] = dist(t[a], t[b]);
68             }
69         }
70         int res = prim(cost, n);
71         cout << "The highest possible quality is 1/" << res <<"."<< endl;
72         cin >> n;
73     }
74     return 0;
75 }

from kakamilan

原文地址:https://www.cnblogs.com/kakamilan/p/3080412.html