File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ P11509import java .util .*;
2+ class Road implements Comparable <Road >{
3+ int from ,to ,cost ;
4+
5+ public Road (int from ,int to , int cost ) {
6+ this .from =from ;
7+ this .to =to ;
8+ this .cost = cost ;
9+ }
10+
11+ @ Override
12+ public int compareTo (Road r ){
13+ return this .cost -r .cost ;
14+ }
15+ }
16+ public class P14950 {
17+ public static void main (String [] args ) {
18+ Scanner sc = new Scanner (System .in );
19+ int n , m , t ;
20+
21+ n = sc .nextInt ();
22+ m = sc .nextInt ();
23+ t = sc .nextInt ();
24+
25+ boolean visited [] = new boolean [n + 1 ];
26+ ArrayList <ArrayList <Road >> road = new ArrayList <>();
27+ for (int i = 0 ; i <= n ; i ++) road .add (new ArrayList <Road >());
28+ for (int i = 0 ; i < m ; i ++) {
29+ int u = sc .nextInt ();
30+ int v = sc .nextInt ();
31+ int cost = sc .nextInt ();
32+ road .get (u ).add (new Road (u , v , cost ));
33+ road .get (v ).add (new Road (v , u , cost ));
34+ }
35+
36+ PriorityQueue <Road > pq = new PriorityQueue <>();
37+
38+ int sum = 0 ;
39+ int gap = 0 ;
40+
41+ pq .addAll (road .get (1 ));
42+ visited [1 ] = true ;
43+
44+ while (!pq .isEmpty ()) {
45+ Road r = pq .poll ();
46+
47+ if (!visited [r .to ]) {
48+ if (visited [r .from ] && visited [r .to ]) continue ;
49+ visited [r .from ] = visited [r .to ] = true ;
50+ pq .addAll (road .get (r .to ));
51+ sum += (r .cost + gap );
52+ gap += t ;
53+ }
54+ }
55+ System .out .println (sum );
56+ }
57+ }
You can’t perform that action at this time.
0 commit comments