1
- /** https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1541
2
- * tag: #prim algorithms
3
- * run Prim algorithms double times
1
+ /**
2
+ * https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1541
3
+ * tag: #prim algorithms run Prim algorithms double times
4
4
*/
5
-
6
- import java .util .Scanner ;
7
5
import java .util .ArrayList ;
8
6
import java .util .Arrays ;
9
7
import java .util .PriorityQueue ;
8
+ import java .util .Scanner ;
10
9
11
10
class ACMContestBlackout {
12
- public static void Prim (ArrayList <ArrayList <Node >> graph , int [] costArr , int [] pathArr , int src ) {
13
- int sz = graph .size ();
14
- boolean [] visitedArr = new boolean [sz ];
15
- Arrays .fill (visitedArr , false );
16
- costArr [src ] = 0 ;
17
-
18
- PriorityQueue <Node > pq = new PriorityQueue <>();
19
- Node node ;
20
- int id , w ;
21
- pq .add (new Node (src , 0 ));
22
- while (!pq .isEmpty ()) {
23
- node = pq .poll ();
24
- id = node .id ;
25
- visitedArr [id ] = true ;
26
- for (Node neighbour : graph .get (id )) {
27
- w = neighbour .cost ;
28
- if (!visitedArr [neighbour .id ] && w < costArr [neighbour .id ]) {
29
- costArr [neighbour .id ] = w ;
30
- pathArr [neighbour .id ] = id ;
31
- pq .add (new Node (neighbour .id , w ));
32
- }
33
- }
11
+ public static void Prim (ArrayList <ArrayList <Node >> graph , int [] costArr , int [] pathArr , int src ) {
12
+ int sz = graph .size ();
13
+ boolean [] visitedArr = new boolean [sz ];
14
+ Arrays .fill (visitedArr , false );
15
+ costArr [src ] = 0 ;
16
+
17
+ PriorityQueue <Node > pq = new PriorityQueue <>();
18
+ Node node ;
19
+ int id , w ;
20
+ pq .add (new Node (src , 0 ));
21
+ while (!pq .isEmpty ()) {
22
+ node = pq .poll ();
23
+ id = node .id ;
24
+ visitedArr [id ] = true ;
25
+ for (Node neighbour : graph .get (id )) {
26
+ w = neighbour .cost ;
27
+ if (!visitedArr [neighbour .id ] && w < costArr [neighbour .id ]) {
28
+ costArr [neighbour .id ] = w ;
29
+ pathArr [neighbour .id ] = id ;
30
+ pq .add (new Node (neighbour .id , w ));
34
31
}
32
+ }
35
33
}
34
+ }
36
35
37
- public static long getCost (int [] costArr ) {
38
- long ans = 0 ;
39
- for (int cost : costArr ) {
40
- ans += cost ;
41
- }
42
- return ans ;
36
+ public static long getCost (int [] costArr ) {
37
+ long ans = 0 ;
38
+ for (int cost : costArr ) {
39
+ ans += cost ;
43
40
}
41
+ return ans ;
42
+ }
43
+
44
+ public static void main (String [] args ) {
45
+ Scanner sc = new Scanner (System .in );
46
+ int testcases = sc .nextInt ();
47
+ int n , m , a , b , c ;
48
+ for (int t = 0 ; t < testcases ; t ++) {
49
+ n = sc .nextInt ();
50
+ m = sc .nextInt ();
51
+ ArrayList <ArrayList <Node >> graph = new ArrayList <>();
52
+ int sz = n + 1 ;
53
+ for (int i = 0 ; i < sz ; i ++) {
54
+ graph .add (new ArrayList <>());
55
+ }
56
+ for (int i = 0 ; i < m ; i ++) {
57
+ a = sc .nextInt ();
58
+ b = sc .nextInt ();
59
+ c = sc .nextInt ();
60
+ graph .get (a ).add (new Node (b , c ));
61
+ graph .get (b ).add (new Node (a , c ));
62
+ }
63
+
64
+ // first time
65
+ int [] costArr1 = new int [sz ];
66
+ int [] pathArr1 = new int [sz ];
67
+ Arrays .fill (pathArr1 , -1 );
68
+ Arrays .fill (costArr1 , Integer .MAX_VALUE );
69
+ costArr1 [0 ] = 0 ;
70
+ Prim (graph , costArr1 , pathArr1 , 1 );
71
+ int val1 = (int ) getCost (costArr1 );
72
+
73
+ // second time
74
+ int cost_tmp = -1 ;
75
+ int val2 = Integer .MAX_VALUE ;
76
+ int [] pathArr2 = new int [sz ];
77
+ Arrays .fill (pathArr2 , -1 );
78
+ int [] costArr2 = new int [sz ];
79
+ for (int id = 1 ; id < sz ; id ++) {
44
80
45
- public static void main (String [] args ) {
46
- Scanner sc = new Scanner (System .in );
47
- int testcases = sc .nextInt ();
48
- int n , m , a , b , c ;
49
- for (int t =0 ; t < testcases ; t ++) {
50
- n = sc .nextInt ();
51
- m = sc .nextInt ();
52
- ArrayList <ArrayList <Node >> graph = new ArrayList <>();
53
- int sz = n +1 ;
54
- for (int i =0 ; i < sz ; i ++) {
55
- graph .add (new ArrayList <>());
81
+ Arrays .fill (costArr2 , Integer .MAX_VALUE );
82
+ costArr2 [0 ] = 0 ;
83
+
84
+ if (pathArr1 [id ] != -1 ) {
85
+ cost_tmp = costArr1 [id ];
86
+ for (Node node : graph .get (id )) {
87
+ if (node .id == pathArr1 [id ]) {
88
+ node .cost = Integer .MAX_VALUE ;
56
89
}
57
- for (int i =0 ; i < m ; i ++) {
58
- a = sc .nextInt ();
59
- b = sc .nextInt ();
60
- c = sc .nextInt ();
61
- graph .get (a ).add (new Node (b , c ));
62
- graph .get (b ).add (new Node (a , c ));
90
+ }
91
+ for (Node node : graph .get (pathArr1 [id ])) {
92
+ if (node .id == id ) {
93
+ node .cost = Integer .MAX_VALUE ;
63
94
}
95
+ }
64
96
65
- // first time
66
- int [] costArr1 = new int [sz ];
67
- int [] pathArr1 = new int [sz ];
68
- Arrays .fill (pathArr1 , -1 );
69
- Arrays .fill (costArr1 , Integer .MAX_VALUE );
70
- costArr1 [0 ] = 0 ;
71
- Prim (graph , costArr1 , pathArr1 , 1 );
72
- int val1 = (int )getCost (costArr1 );
73
-
74
- // second time
75
- int cost_tmp = -1 ;
76
- int val2 = Integer .MAX_VALUE ;
77
- int [] pathArr2 = new int [sz ];
78
- Arrays .fill (pathArr2 , -1 );
79
- int [] costArr2 = new int [sz ];
80
- for (int id = 1 ; id < sz ; id ++) {
81
-
82
- Arrays .fill (costArr2 , Integer .MAX_VALUE );
83
- costArr2 [0 ] = 0 ;
84
-
85
- if (pathArr1 [id ] != -1 ) {
86
- cost_tmp = costArr1 [id ];
87
- for (Node node : graph .get (id )) {
88
- if (node .id == pathArr1 [id ]) {
89
- node .cost = Integer .MAX_VALUE ;
90
- }
91
- }
92
- for (Node node : graph .get (pathArr1 [id ])) {
93
- if (node .id == id ) {
94
- node .cost = Integer .MAX_VALUE ;
95
- }
96
- }
97
+ Prim (graph , costArr2 , pathArr2 , 1 );
98
+ val2 = (int ) Math .min (getCost (costArr2 ), val2 );
97
99
98
- Prim (graph , costArr2 , pathArr2 , 1 );
99
- val2 = (int )Math .min (getCost (costArr2 ), val2 );
100
-
101
- for (Node node : graph .get (id )) {
102
- if (node .id == pathArr1 [id ]) {
103
- node .cost = cost_tmp ;
104
- }
105
- }
106
- for (Node node : graph .get (pathArr1 [id ])) {
107
- if (node .id == id ) {
108
- node .cost = cost_tmp ;
109
- }
110
- }
111
- }
100
+ for (Node node : graph .get (id )) {
101
+ if (node .id == pathArr1 [id ]) {
102
+ node .cost = cost_tmp ;
103
+ }
104
+ }
105
+ for (Node node : graph .get (pathArr1 [id ])) {
106
+ if (node .id == id ) {
107
+ node .cost = cost_tmp ;
112
108
}
113
- System . out . println ( val1 + " " + val2 );
109
+ }
114
110
}
111
+ }
112
+ System .out .println (val1 + " " + val2 );
115
113
}
116
-
114
+ }
117
115
}
118
116
119
117
class Node implements Comparable <Node > {
120
- int id , cost ;
121
- public Node (int id , int cost ) {
122
- this .id = id ;
123
- this .cost = cost ;
124
- }
125
- public int compareTo (Node other ) {
126
- return this .cost - other .cost ;
127
- }
128
- }
118
+ int id , cost ;
119
+
120
+ public Node (int id , int cost ) {
121
+ this .id = id ;
122
+ this .cost = cost ;
123
+ }
124
+
125
+ public int compareTo (Node other ) {
126
+ return this .cost - other .cost ;
127
+ }
128
+ }
0 commit comments