-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
718 changed files
with
48,247 additions
and
117 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Binary file not shown.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Empty file.
6 changes: 6 additions & 0 deletions
6
oj/bjtu-vjudge/BJTU-2021-讲座题单【基础数据结构模板题】/.vscode/settings.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"files.associations": { | ||
"iostream": "cpp", | ||
"vector": "cpp" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
1 change: 1 addition & 0 deletions
1
oj/bjtu-vjudge/BJTU-2021-讲座题单【基础数据结构模板题】/build/.cmake/api/v1/query/client-vscode/query.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}]} |
Oops, something went wrong.