-
Notifications
You must be signed in to change notification settings - Fork 0
/
Prim.cpp
59 lines (56 loc) · 1.29 KB
/
Prim.cpp
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
55
56
57
58
59
#include <bits/stdc++.h>
using namespace std;
int findmin(int visited[],int distance[],int v){
int minidx=-1;
for(int i=0;i<v;i++){
if(!visited[i]&&(minidx==-1||distance[i]<distance[minidx])){
minidx=i;
}
}
return minidx;
}
int main()
{
int V, E, tempX, tempY;
cin >> V >> E;
/*
Write Your Code Here
Complete the Rest of the Program
You have to Print the output yourself
*/
int**edge=new int*[V];
for(int i=0;i<V;i++){
edge[i]=new int[V];
for(int j=0;j<V;j++){
edge[i][j]=0;
}
}
for(int i=0;i<E;i++){
int a,b,c;
cin>>a>>b>>c;
edge[a][b]=c;
edge[b][a]=c;
}
int visited[V];
int distance[V];
for(int i=0;i<V;i++){
visited[i]=0;
distance[i]=INT_MAX;
}
distance[0]=0;
for(int i=0;i<V;i++){
int minvertex=findmin(visited,distance,V);
visited[minvertex]=1;
for(int j=0;j<V;j++){
if(!visited[j]&&edge[minvertex][j]!=0){
if(distance[j]>distance[minvertex]+edge[minvertex][j]){
distance[j]=distance[minvertex]+edge[minvertex][j];
}
}
}
}
for(int i=0;i<V;i++){
cout<<i<<" "<<distance[i]<<"\n";
}
return 0;
}