-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathassignment1_q3.cpp
More file actions
54 lines (51 loc) · 1.25 KB
/
assignment1_q3.cpp
File metadata and controls
54 lines (51 loc) · 1.25 KB
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
#include <bits/stdc++.h>
using namespace std;
// Add edge
void addEdge(vector <pair<int, int>> adj[], int s, int d, int wt) {
adj[s].push_back(make_pair(d, wt));
adj[d].push_back(make_pair(s, wt));
}
int findMinVertex(vector <int>& value, vector<bool>& visited, int V) {
int minimum = INT_MAX;
int vertex;
for (int i=0; i < V; i++) {
if (!visited[i] && value[i]<minimum) {
minimum = value[i];
vertex = i;
}
}
return vertex;
}
int main() {
int V, E;
ifstream file("edges.txt");
file >> V >> E;
int a, b, wt;
int graph[V][V];
memset(graph, 0, sizeof graph);
while (file >> a >> b >> wt) {
graph[a-1][b-1] = wt;
graph[b-1][a-1] = wt;
}
vector<bool> visited(V, false);
vector<int> value(V, INT_MAX);
int parent[V];
parent[0] = -1;
value[0] = 0;
for (int i = 0; i < V-1; i++) {
int d = findMinVertex(value, visited, V);
visited[d] = true;
for (int j = 0; j < V; j++) {
if (!visited[j] && graph[d][j]!=0 && graph[d][j]<value[j]) {
value[j] = graph[d][j];
parent[j] = d;
}
}
}
long long cost = 0;
for (int i = 1; i < V; i++) {
cost += graph[i][parent[i]];
}
cout << cost << endl;
// -3612829
}