A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://TheAlgorithms.github.io/C-Plus-Plus/da/d27/prim_8cpp_source.html below:

TheAlgorithms/C++: graph/prim.cpp Source File

6using

PII = std::pair<int, int>;

8int

prim(

int

x,

const

std::vector<std::vector<PII> > &

graph

) {

10

std::priority_queue<PII, std::vector<PII>, std::greater<PII> > Q;

11

std::vector<bool> marked(

graph

.size(),

false

);

14

Q.push(std::make_pair(0, x));

21 if

(marked[x] ==

true

) {

24

minimum_cost += p.first;

26 for

(

const

PII &neighbor :

graph

[x]) {

27 int

y = neighbor.second;

28 if

(marked[y] ==

false

) {

37 int

nodes = 0, edges = 0;

38

std::cin >> nodes >> edges;

39 if

(nodes == 0 || edges == 0) {

43

std::vector<std::vector<PII> >

graph

(nodes);

46 for

(

int

i = 0; i < edges; ++i) {

47 int

x = 0, y = 0, weight = 0;

48

std::cin >> x >> y >> weight;

49 graph

[x].push_back(std::make_pair(weight, y));

50 graph

[y].push_back(std::make_pair(weight, x));

54 int

minimum_cost = prim(1,

graph

);

55

std::cout << minimum_cost << std::endl;

int main()

Main function.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4