forked from Anushka-codergirl/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprims.java
More file actions
61 lines (51 loc) · 1.09 KB
/
Copy pathprims.java
File metadata and controls
61 lines (51 loc) · 1.09 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
55
56
57
58
59
60
61
import java.io.*;
import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int N=sc.nextInt();
int M=sc.nextInt();
int[][] T=new int[N][M];
int min=0;
int[] visited=new int[M];
int result=0;
int v=0;
for(int i=0;i<N;i++)
{ visited[i]=0;
for(int j=0;j<M;j++)
T[i][j]=1000000;
}
for(int i=0;i<M;i++)
{
int a=sc.nextInt();
int b=sc.nextInt();
T[a-1][b-1]=sc.nextInt();
T[b-1][a-1]=T[a-1][b-1];
}
for(int k=1;k<=N;k++)
{
min=1000000;
for(int i=0;i<M;i++)
{
if(visited[i]==1)
for(int j=0;j<M;j++)
{
if(visited[j]==0)
{
if(min>T[i][j])
{
min=T[i][j];
v=j;
}
}
}
}
visited[v]=1;
if(min<1000000)
result +=min;
}
System.out.println(result);
}
}