Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
npofsi committed Aug 8, 2021
1 parent d0b5f92 commit 3e5f702
Show file tree
Hide file tree
Showing 718 changed files with 48,247 additions and 117 deletions.
28 changes: 0 additions & 28 deletions main/Makefile.win

This file was deleted.

51 changes: 0 additions & 51 deletions main/main.dev

This file was deleted.

Binary file removed main/main.exe
Binary file not shown.
7 changes: 0 additions & 7 deletions main/main.in

This file was deleted.

3 changes: 0 additions & 3 deletions main/main.layout

This file was deleted.

Empty file removed main/main.out
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"files.associations": {
"iostream": "cpp",
"vector": "cpp"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
cmake_minimum_required(VERSION 3.0.0)
project(main VERSION 0.1.0)

include(CTest)
enable_testing()

add_executable(main D_Kruskal.cpp)

set(CPACK_PROJECT_NAME ${PROJECT_NAME})
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
include(CPack)
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
const int N = 55;
const int inf = 2147483647;
typedef struct Edge
{
int from, to;
int weight;
int checked;
} Edge;
typedef struct Graph
{
vector<Edge> edges;
int parent[N];
int childC[N];
int pointC;
int treeW;
} Graph;

void createGraph(Graph &graph, int pointCount, int edgeCount)
{
graph.pointC = pointCount;
for (int i = 0; i <= pointCount; i++)
{
graph.parent[i] = i;
graph.childC[i] = 0;
}
graph.edges.clear();
for (int i = 0; i < edgeCount; i++)
{
int from, to, weight;
cin >> from >> to >> weight;
Edge e = {from, to, weight, false};
//Edge ex = {
// to, from, weight};
graph.edges.push_back(e);
//graph.edges.push_back(ex);
}
}

bool cmpEdge(const Edge &a, const Edge &b)
{
return ((a).weight - (b).weight) < 0;
}

void printEdgeV(Graph &graph)
{
for (int i = 0; i < graph.edges.size(); i++)
{
cout << graph.edges[i].weight << " ";
}
}

int findRoot(Graph &graph, int child)
{
if (graph.parent[child] == child)
{
return child;
}
graph.parent[child] = findRoot(graph, graph.parent[child]);
return graph.parent[child];
}

bool unionTree(Graph &graph, Edge e)
{
int root1 = findRoot(graph, e.from);
int root2 = findRoot(graph, e.to);
if (root1 != root2)
{
if (graph.childC[root1] > graph.childC[root2])
{
graph.childC[root1] += graph.childC[root2];
graph.parent[root2] = root1;
}
else
{
graph.childC[root2] += graph.childC[root1];
graph.parent[root1] = root2;
}
return true;
}
return false;
}

void genMinimalTree(Graph &graph)
{

sort(graph.edges.begin(), graph.edges.end(), cmpEdge);
int checkC = 0;
int n = graph.edges.size();
if (n == 0)
{
return;
}
for (int i = 0; i < n; i++)
{
if (unionTree(graph, graph.edges[i]))
{
graph.edges[i].checked = true;
checkC++;
graph.treeW += graph.edges[i].weight;
}
if (checkC == graph.pointC - 1)
{
break;
}
}
if (checkC != graph.pointC - 1)
{
}
}

int main()
{
while (1)
{
int pc, ec;
cin >> pc;
if (pc != 0)
{
cin >> ec;
Graph g;
g.treeW = 0;
createGraph(g, pc, ec);

genMinimalTree(g);
//printEdgeV(g);
cout << g.treeW << endl;
}
else
{
break;
}
}
return 0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#include <iostream>
#include <vector>
#include <string.h>
using namespace std;
//最小生成树·Prim算法
const int N = 55;
const int inf = 2147483647;
typedef struct
{
int matx[N][N];
int edgeC;
int pointC;
vector<int> U;
int treeW = 0;
} Graph;
void createGraph(Graph &graph, int pointCount, int edgeCount)
{
graph.edgeC = edgeCount;
graph.pointC = pointCount;
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
{
graph.matx[i][j] = -1;
}
}
for (int i = 0; i < edgeCount; i++)
{
int from, to, weight;
cin >> from >> to >> weight;
graph.matx[from][to] = weight;
graph.matx[to][from] = weight;
}
return;
}

void genMinimalTree(Graph &graph)
{
if (graph.edgeC == 0)
{
graph.treeW = 0;
}

graph.U.push_back(1);
while (graph.U.size() != graph.pointC)
{
int n = graph.U.size();
int min = inf;
int to = -1;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < N; j++)
{
int ew = graph.matx[graph.U[i]][j];
if (ew != -1 && ew < min)
{
min = ew;
to = j;
}
}
}

graph.treeW += min;
graph.U.push_back(to);
}
}

int main(int, char **)
{
while (1)
{
int pc, ec;
cin >> pc;
if (pc != 0)
{
cin >> ec;
Graph g;
createGraph(g, pc, ec);
genMinimalTree(g);
cout << "gen:";
cout << g.treeW << endl;
}
else
{
break;
}
}
return 0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"requests":[{"kind":"cache","version":2},{"kind":"codemodel","version":2},{"kind":"toolchains","version":1}]}
Loading

0 comments on commit 3e5f702

Please sign in to comment.