Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <vector>
- #include <cmath>
- #include <queue>
- #include <utility>
- #include <algorithm>
- const std::string nucl = "ATGC";
- unsigned dist(std::string a, std::string b) {
- unsigned d = 0;
- for (unsigned i = 0; i < a.length(); ++i) {
- unsigned curr = (nucl.find(a[i]) > nucl.find(b[i]) ? nucl.find(a[i]) - nucl.find(b[i]) : nucl.find(b[i]) - nucl.find(a[i]));
- d += std::min(curr, 4 - curr);
- }
- return d;
- }
- int main() {
- unsigned n, l;
- std::cin >> n >> l;
- std::vector<std::string> acids(n + 1);
- acids[0] = std::string(l, 'A');
- for (unsigned i = 1; i < n + 1; ++i) {
- std::cin >> acids[i];
- }
- std::vector<std::vector<unsigned>> matrix(n + 1, std::vector<unsigned>(n + 1, 0));
- for (unsigned i = 0; i < n + 1; ++i) {
- for (unsigned j = 0; j < n + 1; ++j) {
- matrix[i][j] = dist(acids[i], acids[j]);
- }
- }
- std::vector<bool> used(n + 1, false);
- unsigned mst_weight = 0;
- std::priority_queue<std::pair<int, int>, std::vector<std::pair<int, int>>, std::greater<std::pair<int, int>>> q;
- q.push({0, 0});
- while (!q.empty()) {
- std::pair<int, int> c = q.top();
- q.pop();
- int dst = c.first, v = c.second;
- if (used[v]) {
- continue;
- }
- used[v] = true;
- mst_weight += dst;
- for (unsigned u = 0; u < n + 1; ++u) {
- unsigned len_vu = matrix[v][u];
- if (!used[u]) {
- q.push({len_vu, u});
- }
- }
- }
- std::cout << mst_weight << std::endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement