|
| 1 | +// |
| 2 | +// algorithm - some algorithms in "Introduction to Algorithms", third edition |
| 3 | +// Copyright (C) 2018 lxylxy123456 |
| 4 | +// |
| 5 | +// This program is free software: you can redistribute it and/or modify |
| 6 | +// it under the terms of the GNU Affero General Public License as |
| 7 | +// published by the Free Software Foundation, either version 3 of the |
| 8 | +// License, or (at your option) any later version. |
| 9 | +// |
| 10 | +// This program is distributed in the hope that it will be useful, |
| 11 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | +// GNU Affero General Public License for more details. |
| 14 | +// |
| 15 | +// You should have received a copy of the GNU Affero General Public License |
| 16 | +// along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 17 | +// |
| 18 | + |
| 19 | +#ifndef MAIN |
| 20 | +#define MAIN |
| 21 | +#define MAIN_ApproxTSPTour |
| 22 | +#endif |
| 23 | + |
| 24 | +#ifndef FUNC_ApproxTSPTour |
| 25 | +#define FUNC_ApproxTSPTour |
| 26 | + |
| 27 | +#include "utils.h" |
| 28 | + |
| 29 | +#include "MST.cpp" |
| 30 | +#include "SegmentsIntersect.cpp" |
| 31 | + |
| 32 | +template <typename GT, typename T> |
| 33 | +void TreeWalk(GT& G, T current, T* last, std::vector<T>& H) { |
| 34 | + H.push_back(current); |
| 35 | + for (auto i = G.edges_from(current); !i.end(); i++) |
| 36 | + if (!last || i.d() != *last) |
| 37 | + TreeWalk(G, i.d(), ¤t, H); |
| 38 | +} |
| 39 | + |
| 40 | +template <typename GT, typename T, typename WT> |
| 41 | +void ApproxTSPTour(GT& G, umap_WT& c, std::vector<T>& H) { |
| 42 | + T r = *G.V.begin(); |
| 43 | + umap<T, MSTPrimInfo<T, WT>> MST_info; |
| 44 | + MSTPrim(G, c, r, MST_info); |
| 45 | + GT MST_Graph(false); |
| 46 | + for (auto i = G.V.begin(); i != G.V.end(); i++) { |
| 47 | + const MSTPrimInfo<T, WT>& info = MST_info[*i]; |
| 48 | + if (!info.pi_nil) |
| 49 | + MST_Graph.add_edge(*i, info.pi); |
| 50 | + } |
| 51 | + H.reserve(G.V.size() + 1); |
| 52 | + TreeWalk<GT, T>(MST_Graph, r, nullptr, H); |
| 53 | + H.push_back(r); |
| 54 | +} |
| 55 | +#endif |
| 56 | + |
| 57 | +#ifdef MAIN_ApproxTSPTour |
| 58 | +int main(int argc, char *argv[]) { |
| 59 | + const size_t n = get_argv(argc, argv, 1, 200); |
| 60 | + const size_t m = get_argv(argc, argv, 2, 10); |
| 61 | + const bool dir = 0; |
| 62 | + std::vector<int> b; |
| 63 | + random_integers(b, -n, n, m * 2); |
| 64 | + using T = size_t; |
| 65 | + using WT = double; |
| 66 | + GraphAdjList<size_t> G(dir); |
| 67 | + umap_WT c; |
| 68 | + std::vector<Vector<T>> S; |
| 69 | + S.reserve(m); |
| 70 | + for (size_t i = 0; i < m; i++) { |
| 71 | + S.push_back(Vector<T>(b[2 * i + 0], b[2 * i + 1])); |
| 72 | + for (size_t j = 0; j < i; j++) { |
| 73 | + G.add_edge(i, j); |
| 74 | + c[Edge<T>(i, j, false)] = (S[i] - S[j]).Length(); |
| 75 | + } |
| 76 | + } |
| 77 | + std::vector<size_t> H; |
| 78 | + ApproxTSPTour(G, c, H); |
| 79 | + std::cout << "<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"no\"?>" |
| 80 | + << std::endl; |
| 81 | + std::cout << "<svg height=\"" << 2 * n << "\" width=\"" << 2 * n << "\">" |
| 82 | + << std::endl; |
| 83 | + std::cout << "\t<rect fill=\"#ffffff\" x=\"0\" y=\"0\" width=\"" << 2 * n |
| 84 | + << "\" height=\"" << 2 * n << "\"/>" << std::endl; |
| 85 | + std::cout << "\t<polygon stroke=\"#000000\" fill=\"#cccccc\" points=\""; |
| 86 | + for (auto i = H.begin(); i != H.end(); i++) |
| 87 | + std::cout << n + S[*i].x + 1 << "," << n + S[*i].y + 1 << " "; |
| 88 | + std::cout << "\"/>" << std::endl; |
| 89 | + for (size_t i = 0; i < m; i++) |
| 90 | + std::cout << "\t<circle cx=\"" << n + S[i].x + 1 << "\" cy=\"" |
| 91 | + << n + S[i].y + 1 << "\" r=\"2\"/>" << std::endl; |
| 92 | + std::cout << "</svg>" << std::endl; |
| 93 | + return 0; |
| 94 | +} |
| 95 | +#endif |
| 96 | + |
0 commit comments