题意:给n个字符串,每个字符串从第一个字符串延伸出来,延伸的代价为两个字符串不同字母的个数,求最大的1/总代价。
解法:意思就是求最小的总代价……把字符串看做点,字符串之间的代价看做边,形成一个完全图,跑一下prim,因为边数太多了kruskal太慢。
代码:
#include<stdio.h> #include<iostream> #include<algorithm> #include<string> #include<string.h> #include<math.h> #include<limits.h> #include<time.h> #include<stdlib.h> #include<map> #include<queue> #include<set> #include<stack> #include<vector> #include<iomanip> #define LL long long #define lson l, m, rt << 1 #define rson m + 1, r, rt << 1 | 1 using namespace std; string s[2005]; bool vis[2005]; struct node { int u, step; node(int u, int step) : u(u), step(step) {} node() {} bool operator < (const node &tmp) const { return step > tmp.step; } }; int main() { ios :: sync_with_stdio(false); int n; while(cin >> n && n) { memset(vis, 0, sizeof vis); for(int i = 0; i < n; i++) cin >> s[i]; priority_queue<node> q; q.push(node(0, 0)); int ans = 0; int cnt = 0; while(!q.empty()) { node tmp = q.top(); q.pop(); if(vis[tmp.u] == false) { cnt++; vis[tmp.u] = true; ans += tmp.step; } else continue; if(cnt == n) break; for(int i = 0; i < n; i++) { if(vis[i]) continue; int val = 0; for(int j = 0; j < 7; j++) if(s[tmp.u][j] != s[i][j]) val++; q.push(node(i, val)); } } cout << "The highest possible quality is 1/" << ans << ". "; } return 0; }