POJ 1789
#include <iostream> #include <string> #define MAXN 2005 #define inf 123456789 using namespace std; int pre[MAXN]; int _m[MAXN][MAXN]; int low_cost[MAXN]; string str[MAXN]; int prime(int n); int cmp(string s_1,string s_2) { int i; int len = s_2.length(); int ans = 0; for(i = 0; i < len; ++ i) { if(s_1[i] != s_2[i]) { ++ ans; } } return ans; } int main() { //freopen("acm.acm","r",stdin); int n; int i; int j; while(cin>>n,n) { for(i = 0; i < n; ++ i) { cin>>str[i]; } for(i = 0; i < n; ++ i) { for(j = i+1; j < n; ++ j) { _m[i][j] = cmp(str[i],str[j]); _m[j][i] = _m[i][j]; } } cout<<"The highest possible quality is 1/"<<prime(n)<<"."<<endl; } } int prime(int n) { int i; int j; int k; int sum = 0; int min; for(i = 1; i < n; ++ i) { low_cost[i] = _m[0][i]; pre[i] = 0; } for(i = 1; i < n; ++ i) { min = inf; for(j = 1; j < n; ++ j) { if(low_cost[j]&&low_cost[j] < min) { k = j; min = low_cost[j]; } } sum += low_cost[k]; low_cost[k] = 0; for(j = 1; j < n; ++ j) { if(_m[k][j] < low_cost[j] && low_cost[j]) { low_cost[j] = _m[k][j]; pre[j] = k; } } } return sum; }
关注我的公众号,当然,如果你对Java, Scala, Python等技术经验,以及编程日记,感兴趣的话。
技术网站地址: vmfor.com