|
| 1 | +package dijkstra; |
| 2 | + |
| 3 | +import java.io.BufferedReader; |
| 4 | + |
| 5 | +import java.io.IOException; |
| 6 | + |
| 7 | +import java.io.InputStream; |
| 8 | + |
| 9 | +import java.io.InputStreamReader; |
| 10 | + |
| 11 | +import java.util.StringTokenizer; |
| 12 | + |
| 13 | + |
| 14 | + |
| 15 | +/** Class for buffered reading int and double values */ |
| 16 | + |
| 17 | +class Reader { |
| 18 | + |
| 19 | + static BufferedReader reader; |
| 20 | + |
| 21 | + static StringTokenizer tokenizer; |
| 22 | + |
| 23 | + |
| 24 | + |
| 25 | + /** call this method to initialize reader for InputStream */ |
| 26 | + |
| 27 | + static void init(InputStream input) { |
| 28 | + |
| 29 | + reader = new BufferedReader( |
| 30 | + |
| 31 | + new InputStreamReader(input) ); |
| 32 | + |
| 33 | + tokenizer = new StringTokenizer(""); |
| 34 | + |
| 35 | + } |
| 36 | + |
| 37 | + |
| 38 | + |
| 39 | + /** get next word */ |
| 40 | + |
| 41 | + static String next() throws IOException { |
| 42 | + |
| 43 | + while ( ! tokenizer.hasMoreTokens() ) { |
| 44 | + |
| 45 | + //TODO add check for eof if necessary |
| 46 | + |
| 47 | + tokenizer = new StringTokenizer( |
| 48 | + |
| 49 | + reader.readLine() ); |
| 50 | + |
| 51 | + } |
| 52 | + |
| 53 | + return tokenizer.nextToken(); |
| 54 | + |
| 55 | + } |
| 56 | + |
| 57 | + |
| 58 | + |
| 59 | + static int nextInt() throws IOException { |
| 60 | + |
| 61 | + return Integer.parseInt( next() ); |
| 62 | + |
| 63 | + } |
| 64 | + |
| 65 | + static long nextLong() throws IOException { |
| 66 | + |
| 67 | + return Long.parseLong( next() ); |
| 68 | + |
| 69 | + } |
| 70 | + |
| 71 | + static double nextDouble() throws IOException { |
| 72 | + |
| 73 | + return Double.parseDouble( next() ); |
| 74 | + |
| 75 | + } |
| 76 | + |
| 77 | +} |
| 78 | + |
| 79 | + |
| 80 | +class dijkstra { |
| 81 | + |
| 82 | + |
| 83 | + |
| 84 | + long[][] array; |
| 85 | + |
| 86 | + long dist[]=new long[11000]; |
| 87 | + |
| 88 | + int parent[]=new int[11000]; |
| 89 | + |
| 90 | + dijkstra (int v) { |
| 91 | + |
| 92 | + array=new long[v+1][v+1]; |
| 93 | + } |
| 94 | + |
| 95 | + void addedge(int src, int dest, long weight) { |
| 96 | + array[src][dest]=weight; |
| 97 | + array[dest][src]=weight; |
| 98 | + } |
| 99 | + |
| 100 | + |
| 101 | + int findmin(long [] dist,int size, boolean[] visited) { |
| 102 | + |
| 103 | + int low=0; |
| 104 | + |
| 105 | + long min=Integer.MAX_VALUE; |
| 106 | + |
| 107 | + for (int i=1;i<size;i++) { |
| 108 | + |
| 109 | + if (dist[i]<min&&visited[i]==false) { |
| 110 | + |
| 111 | + min=dist[i]; |
| 112 | + |
| 113 | + low=i; |
| 114 | + |
| 115 | + } |
| 116 | + |
| 117 | + } |
| 118 | + |
| 119 | + return low; |
| 120 | + |
| 121 | + } |
| 122 | + |
| 123 | + |
| 124 | + |
| 125 | + int[] DJK(int s){ |
| 126 | + |
| 127 | + |
| 128 | + |
| 129 | + for (int i=1;i<array.length;i++) { |
| 130 | + |
| 131 | + dist[i]=Integer.MAX_VALUE; |
| 132 | + |
| 133 | + } |
| 134 | + |
| 135 | + dist[s]=0; |
| 136 | + |
| 137 | + parent[s]=0; |
| 138 | + |
| 139 | + boolean[] visited=new boolean[array.length]; |
| 140 | + |
| 141 | + for (int i=1;i<array.length;i++) { |
| 142 | + |
| 143 | + int min=findmin(dist,array.length,visited); |
| 144 | + visited[min]=true; |
| 145 | + |
| 146 | + for (int x=1;x<array[min].length;x++) { |
| 147 | + |
| 148 | + if (array[min][x]!=0 && !visited[x] ) { |
| 149 | + |
| 150 | + if(array[min][x]<dist[x]) { |
| 151 | + |
| 152 | + dist[x]=array[min][x]; |
| 153 | + parent[x]=min; |
| 154 | + |
| 155 | + } |
| 156 | + |
| 157 | + } |
| 158 | + |
| 159 | + } |
| 160 | + |
| 161 | + } |
| 162 | + |
| 163 | + return parent; |
| 164 | + |
| 165 | + } |
| 166 | + |
| 167 | + |
| 168 | + |
| 169 | + public static void main(String[] args) throws IOException { |
| 170 | + |
| 171 | + // TODO Auto-generated method stub |
| 172 | + Reader.init(System.in); |
| 173 | + int test=Reader.nextInt(); |
| 174 | + for (int t=1;t<=test;t++) { |
| 175 | + |
| 176 | + int v=Reader.nextInt(); |
| 177 | + dijkstra obj=new dijkstra(v); |
| 178 | + int e=Reader.nextInt(); |
| 179 | + long sum=0; |
| 180 | + for (int x=0;x<e;x++) { |
| 181 | + int a=Reader.nextInt(); |
| 182 | + int b=Reader.nextInt(); |
| 183 | + long c=Reader.nextLong(); |
| 184 | + sum+=c; |
| 185 | + obj.addedge(a,b,c); |
| 186 | + } |
| 187 | + int[] ans=obj.DJK(1); |
| 188 | + for(int o=2;o<=v;o++) { |
| 189 | + sum-=obj.array[o][ans[o]]; |
| 190 | + } |
| 191 | + System.out.println(sum); |
| 192 | + } |
| 193 | + |
| 194 | + } |
| 195 | + |
| 196 | +} |
| 197 | + |
0 commit comments