|
| 1 | +```java |
| 2 | + |
| 3 | +import java.util.*; |
| 4 | +import java.io.*; |
| 5 | + |
| 6 | +class Main { |
| 7 | + |
| 8 | + // IO field |
| 9 | + static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 10 | + static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); |
| 11 | + static StringTokenizer st = new StringTokenizer(""); |
| 12 | + |
| 13 | + static void nextLine() throws Exception {st = new StringTokenizer(br.readLine());} |
| 14 | + static String nextToken() throws Exception { |
| 15 | + while(!st.hasMoreTokens()) nextLine(); |
| 16 | + return st.nextToken(); |
| 17 | + } |
| 18 | + static int nextInt() throws Exception { return Integer.parseInt(nextToken()); } |
| 19 | + static long nextLong() throws Exception { return Long.parseLong(nextToken()); } |
| 20 | + static double nextDouble() throws Exception { return Double.parseDouble(nextToken()); } |
| 21 | + static void bwEnd() throws Exception {bw.flush();bw.close();} |
| 22 | + |
| 23 | + // Additional field |
| 24 | + |
| 25 | + static int W, P; |
| 26 | + static int[][] Edges; |
| 27 | + |
| 28 | + static List<int[]>[] V; |
| 29 | + static int[] r; |
| 30 | + |
| 31 | + static int f(int x) {return x==r[x] ? x : (r[x]=f(r[x]));} |
| 32 | + |
| 33 | + public static void main(String[] args) throws Exception { |
| 34 | + |
| 35 | + ready(); |
| 36 | + solve(); |
| 37 | + |
| 38 | + bwEnd(); |
| 39 | + |
| 40 | + } |
| 41 | + |
| 42 | + static void ready() throws Exception{ |
| 43 | + |
| 44 | + W = nextInt(); |
| 45 | + P = nextInt(); |
| 46 | + Edges = new int[P][3]; |
| 47 | + for(int i=0;i<P;i++) for(int j=0;j<3;j++) Edges[i][j] = nextInt(); |
| 48 | + |
| 49 | + V = new List[W+1]; |
| 50 | + r = new int[W+1]; |
| 51 | + for(int i=1;i<=W;i++) { |
| 52 | + V[i] = new ArrayList<>(); |
| 53 | + r[i] = i; |
| 54 | + } |
| 55 | + |
| 56 | + } |
| 57 | + |
| 58 | + static void solve() throws Exception{ |
| 59 | + |
| 60 | + |
| 61 | + Arrays.sort(Edges, (a,b) -> a[2]-b[2]); |
| 62 | + |
| 63 | + int mst = 0; |
| 64 | + boolean[] used = new boolean[P]; |
| 65 | + for(int i=0;i<P;i++) { |
| 66 | + int[] e = Edges[i]; |
| 67 | + int a = e[0], b = e[1], c = e[2]; |
| 68 | + int x = f(a), y = f(b); |
| 69 | + if(x == y) continue; |
| 70 | + mst += c; |
| 71 | + V[a].add(new int[] {b,c}); |
| 72 | + V[b].add(new int[] {a,c}); |
| 73 | + r[x] = y; |
| 74 | + used[i] = true; |
| 75 | + } |
| 76 | + |
| 77 | + int min = (int)1e9; |
| 78 | + for(int i=0;i<P;i++) if(!used[i]) { |
| 79 | + int[] e = Edges[i]; |
| 80 | + int a = e[0], b = e[1], c = e[2]; |
| 81 | + |
| 82 | + Queue<int[]> Q = new LinkedList<>(); |
| 83 | + boolean[] vis = new boolean[W+1]; |
| 84 | + |
| 85 | + Q.offer(new int[] {a,0}); |
| 86 | + vis[a] = true; |
| 87 | + while(!Q.isEmpty()) { |
| 88 | + int[] now = Q.poll(); |
| 89 | + int n = now[0], t = now[1]; |
| 90 | + if(n == b) { |
| 91 | + min = Math.min(min, c-t); |
| 92 | + break; |
| 93 | + } |
| 94 | + for(int[] j:V[n]) if(!vis[j[0]]) { |
| 95 | + vis[j[0]] = true; |
| 96 | + Q.offer(new int[] {j[0],Math.max(t, j[1])}); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + } |
| 101 | + bw.write((mst+min)+"\n"); |
| 102 | + } |
| 103 | + |
| 104 | +} |
| 105 | + |
| 106 | +``` |
0 commit comments