테스트 테스트
2018. 3. 12. 01:45
ㅇ나아아아
ㅔㅌ스트
컬러 스크립터 써보는 중
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 | #include <queue> #include "WGraph.h" #include "VertexSets.h" #include <functional> using namespace std; using vertex = std::tuple<int, int, int>; class WGraphMST : public WGraph { public: void Kruskal() { cout << "Kruskal : \n"; priority_queue<vertex, vector<vertex>, greater<vertex>> q; for (int i = 0; i < size - 1; ++i) { for (int j = i + 1; j < size; ++j) { if (hasEdge(i, j)) q.push(make_tuple(getEdge(i, j), i, j)); } } VertexSets set(size); while (!set.isOne()) { auto e = q.top(); q.pop(); int uset = set.findSet(get<1>(e)); int vset = set.findSet(get<2>(e)); if (uset != vset) { cout << "간선 추가 : " << getVertexName(get<1>(e)) << " - " << getVertexName(get<2>(e)) << " 비용 : " << get<0>(e) << endl; set.unionSets(uset, vset); } } } int getMinVtxIdx(bool selected[], int dist[]) { int minVertexIndex = 0; int mindist = INF; for (int i = 0; i< size; ++i) { if (!selected[i] && dist[i] < mindist) { mindist = dist[i]; minVertexIndex = i; } } return minVertexIndex; } void Prim(int s) { cout << "Prim : \n"; bool selected[MAX_VTXS]; int dist[MAX_VTXS]; for (int i = 0; i < size; ++i) { dist[i] = INF; selected[i] = false; } dist[s] = 0; for (int i = 0; i < size; ++i) { int index = getMinVtxIdx(selected, dist); selected[index] = true; if (dist[index] == INF) return; cout << getVertexName(index) << " "; for (int i = 0; i < size; ++i) if (getEdge(index, i) != INF) if (!selected[i] && getEdge(index, i) < dist[i]) dist[i] = getEdge(index, i); } cout << endl; } }; | cs |
'잡담' 카테고리의 다른 글
long time no see (0) | 2021.02.16 |
---|---|
시간 (0) | 2018.03.17 |
유용한 링크 정리 (0) | 2018.03.12 |
syntax highlighter (0) | 2018.03.12 |