mirror of
https://hub.njuu.cf/TheAlgorithms/C-Plus-Plus.git
synced 2023-10-11 13:05:55 +08:00
commit
250c0c4d0a
@ -3,56 +3,55 @@
|
|||||||
#include <queue>
|
#include <queue>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
const int MAX = 1e4 + 5;
|
using PII = std::pair<int, int>;
|
||||||
typedef std::pair<int, int> PII;
|
|
||||||
|
|
||||||
bool marked[MAX];
|
int prim(int x, const std::vector< std::vector<PII> > &graph) {
|
||||||
std::vector<PII> adj[MAX];
|
|
||||||
|
|
||||||
int prim(int x) {
|
|
||||||
// priority queue to maintain edges with respect to weights
|
// priority queue to maintain edges with respect to weights
|
||||||
std::priority_queue<PII, std::vector<PII>, std::greater<PII> > Q;
|
std::priority_queue<PII, std::vector<PII>, std::greater<PII> > Q;
|
||||||
int y;
|
std::vector<bool> marked(graph.size(), false);
|
||||||
int minimumCost = 0;
|
int minimum_cost = 0;
|
||||||
PII p;
|
|
||||||
|
|
||||||
Q.push(std::make_pair(0, x));
|
Q.push(std::make_pair(0, x));
|
||||||
while (!Q.empty()) {
|
while (!Q.empty()) {
|
||||||
// Select the edge with minimum weight
|
// Select the edge with minimum weight
|
||||||
p = Q.top();
|
PII p = Q.top();
|
||||||
Q.pop();
|
Q.pop();
|
||||||
x = p.second;
|
x = p.second;
|
||||||
// Checking for cycle
|
// Checking for cycle
|
||||||
if (marked[x] == true)
|
if (marked[x] == true) {
|
||||||
continue;
|
continue;
|
||||||
minimumCost += p.first;
|
}
|
||||||
|
minimum_cost += p.first;
|
||||||
marked[x] = true;
|
marked[x] = true;
|
||||||
for (int i = 0; i < adj[x].size(); ++i) {
|
for (const PII &neighbor : graph[x]) {
|
||||||
y = adj[x][i].second;
|
int y = neighbor.second;
|
||||||
if (marked[y] == false)
|
if (marked[y] == false) {
|
||||||
Q.push(adj[x][i]);
|
Q.push(neighbor);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return minimumCost;
|
}
|
||||||
|
return minimum_cost;
|
||||||
}
|
}
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
int nodes, edges, x, y;
|
int nodes = 0, edges = 0;
|
||||||
int weight, minimumCost;
|
|
||||||
|
|
||||||
std::cin >> nodes >> edges; // number of nodes & edges in graph
|
std::cin >> nodes >> edges; // number of nodes & edges in graph
|
||||||
if (nodes == 0 || edges == 0)
|
if (nodes == 0 || edges == 0) {
|
||||||
return 0;
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector< std::vector<PII> > graph(nodes);
|
||||||
|
|
||||||
// Edges with their nodes & weight
|
// Edges with their nodes & weight
|
||||||
for (int i = 0; i < edges; ++i) {
|
for (int i = 0; i < edges; ++i) {
|
||||||
|
int x = 0, y = 0, weight = 0;
|
||||||
std::cin >> x >> y >> weight;
|
std::cin >> x >> y >> weight;
|
||||||
adj[x].push_back(std::make_pair(weight, y));
|
graph[x].push_back(std::make_pair(weight, y));
|
||||||
adj[y].push_back(std::make_pair(weight, x));
|
graph[y].push_back(std::make_pair(weight, x));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Selecting 1 as the starting node
|
// Selecting 1 as the starting node
|
||||||
minimumCost = prim(1);
|
int minimum_cost = prim(1, graph);
|
||||||
std::cout << minimumCost << std::endl;
|
std::cout << minimum_cost << std::endl;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user