-
Notifications
You must be signed in to change notification settings - Fork 0
/
ACMContestBlackout.java
128 lines (115 loc) · 3.37 KB
/
ACMContestBlackout.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/**
* https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1541
* tag: #prim algorithms run Prim algorithms double times
*/
import java.util.ArrayList;
import java.util.Arrays;
import java.util.PriorityQueue;
import java.util.Scanner;
class ACMContestBlackout {
public static void Prim(ArrayList<ArrayList<Node>> graph, int[] costArr, int[] pathArr, int src) {
int sz = graph.size();
boolean[] visitedArr = new boolean[sz];
Arrays.fill(visitedArr, false);
costArr[src] = 0;
PriorityQueue<Node> pq = new PriorityQueue<>();
Node node;
int id, w;
pq.add(new Node(src, 0));
while (!pq.isEmpty()) {
node = pq.poll();
id = node.id;
visitedArr[id] = true;
for (Node neighbour : graph.get(id)) {
w = neighbour.cost;
if (!visitedArr[neighbour.id] && w < costArr[neighbour.id]) {
costArr[neighbour.id] = w;
pathArr[neighbour.id] = id;
pq.add(new Node(neighbour.id, w));
}
}
}
}
public static long getCost(int[] costArr) {
long ans = 0;
for (int cost : costArr) {
ans += cost;
}
return ans;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int testcases = sc.nextInt();
int n, m, a, b, c;
for (int t = 0; t < testcases; t++) {
n = sc.nextInt();
m = sc.nextInt();
ArrayList<ArrayList<Node>> graph = new ArrayList<>();
int sz = n + 1;
for (int i = 0; i < sz; i++) {
graph.add(new ArrayList<>());
}
for (int i = 0; i < m; i++) {
a = sc.nextInt();
b = sc.nextInt();
c = sc.nextInt();
graph.get(a).add(new Node(b, c));
graph.get(b).add(new Node(a, c));
}
// first time
int[] costArr1 = new int[sz];
int[] pathArr1 = new int[sz];
Arrays.fill(pathArr1, -1);
Arrays.fill(costArr1, Integer.MAX_VALUE);
costArr1[0] = 0;
Prim(graph, costArr1, pathArr1, 1);
int val1 = (int) getCost(costArr1);
// second time
int cost_tmp = -1;
int val2 = Integer.MAX_VALUE;
int[] pathArr2 = new int[sz];
Arrays.fill(pathArr2, -1);
int[] costArr2 = new int[sz];
for (int id = 1; id < sz; id++) {
Arrays.fill(costArr2, Integer.MAX_VALUE);
costArr2[0] = 0;
if (pathArr1[id] != -1) {
cost_tmp = costArr1[id];
for (Node node : graph.get(id)) {
if (node.id == pathArr1[id]) {
node.cost = Integer.MAX_VALUE;
}
}
for (Node node : graph.get(pathArr1[id])) {
if (node.id == id) {
node.cost = Integer.MAX_VALUE;
}
}
Prim(graph, costArr2, pathArr2, 1);
val2 = (int) Math.min(getCost(costArr2), val2);
for (Node node : graph.get(id)) {
if (node.id == pathArr1[id]) {
node.cost = cost_tmp;
}
}
for (Node node : graph.get(pathArr1[id])) {
if (node.id == id) {
node.cost = cost_tmp;
}
}
}
}
System.out.println(val1 + " " + val2);
}
}
}
class Node implements Comparable<Node> {
int id, cost;
public Node(int id, int cost) {
this.id = id;
this.cost = cost;
}
public int compareTo(Node other) {
return this.cost - other.cost;
}
}