-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathElectrification.java
76 lines (59 loc) · 1.91 KB
/
Electrification.java
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import java.util.*;
import java.io.*;
public class Electrification
{
public static void main(String[] argv) {
Scanner in = new Scanner(System.in);
PrintWriter out = new PrintWriter(System.out);
int n,k;
n = in.nextInt();
k = in.nextInt();
int adjacencyMatrix[][] = new int[n][n];
Set<Integer> powers = new HashSet<>();
Set<Integer> cities = new HashSet<>();
int sum = 0;
for (int i = 0; i < k; i++)
powers.add(in.nextInt()-1);
for (int i = 0; i < n ; i++) {
if (!powers.contains(i))
cities.add(i);
for (int j = 0; j < n; j++) {
adjacencyMatrix[i][j]=in.nextInt();
}
}
while (cities.size() > 0) {
int minLen = Integer.MAX_VALUE;
int v = 0;
for (int i : cities) {
for (int j : powers) {
if (adjacencyMatrix[i][j] < minLen) {
minLen = adjacencyMatrix[i][j];
v = i;
}
}
}
sum += minLen;
powers.add(v);
cities.remove(v);
}
System.out.println(sum);
}
public static void floyd(int[][] dist, int[][] next) {
for (int k = 0; k < dist.length; k++) {
for (int i = 0; i < dist.length; i++) {
for (int j = 0; j < dist.length; j++) {
if (dist[i][j] > dist[i][k]+dist[k][j]) {
dist[i][j] = dist[i][k]+dist[k][j];
next[i][j] = k;
}
}
}
}
}
public static void printTable(int[][] table) {
System.out.println("------------------");
for (int i = 0; i < table.length; i++) {
System.out.println(Arrays.toString(table[i]));
}
}
}