Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission (s): 2718 Accepted Submission (s): 1153
Problem Description
XX star has many cities that communicate with each other through a strange highway SARS (Super Air Roam Structure --- Super Air Roaming Structure, each piece of SARS imposes a fixed Speed limit on the Flycar driving. Meanwhile, XX has special requirements on Flycar's "comfort, that is, the lower the speed difference between the highest speed and the lowest speed, the more comfortable the ride. (It is understood that flycar must speed up/speed down instantly, which is painful ),
However, XX has less requirements on time. You need to find the most comfortable path between cities. (SARS is bidirectional ).
Input
Enter multiple test instances, each of which includes:
The first line has two positive integers n (1 <n <= 200) and m (m <= 1000), indicating that there are N cities and m sars.
The following rows are three positive integers, StartCity, EndCity, and speed. StartCity is converted to EndCity on the surface, and the speed limit is speedSARS. Speed <= 1000000
Then there is a positive integer Q (Q <11), indicating the number of pathfinding.
Next, each row in row Q has two positive integers Start and End, indicating the Start and End of the path.
Output
Print a line for each route seeking. Only one non-negative integer is output to indicate the maximum comfort speed and the lowest speed of the optimal route. If the start and end cannot be reached, the output is-1.
Sample Input
4
1 2 2
2 3 4
1 4 1
3 4 2
2
1 3
1 2
Sample Output
1
0
Import java. io. *; import java. util. *; public class Main {int MIN = Integer. MAX_VALUE; int M = 205, n, m; int patten [] = new int [M]; Node node []; public static void main (String [] args) {new Main (). work ();} void work () {vertex SC = new vertex (new BufferedInputStream (System. in); while (SC. hasNext () {n = SC. nextInt (); m = SC. nextInt (); init (); node = new Node [m]; for (int I = 0; I <m; I ++) {int a = SC. nextInt (); int B = s C. nextInt (); int c = SC. nextInt (); node [I] = new Node (a, B, c); union (a, B);} Arrays. sort (node); // sort int q = SC by weight. nextInt (); for (int I = 0; I <q; I ++) {int a = SC. nextInt (); int B = SC. nextInt (); getDistance (a, B) ;}}// obtain the shortest distance void getDistance (int a, int B) {int min = MIN; for (int I = 0; I <m; I ++) {init (); for (int j = I; j <m; j ++) {union (node [j]. a, node [j]. b); if (find (a) = find (B) min = Math. min (min, node [j]. c-node [I]. c );}} If (min! = MIN) System. out. println (min); else System. out. println (-1);} // merge void union (int a, int B) {int pa = find (a); int pb = find (B ); if (pa = pb) return; patten [pa] = pb;} // query int find (int x) {if (patten [x] = x) return x; patten [x] = find (patten [x]); return patten [x];} // initialize void init () {for (int I = 0; I <= n; I ++) {patten [I] = I ;}} class Node implements Comparable <Node> {int a; int B; int c; Node (int a, int B, int c ){ This. a = a; this. B = B; this. c = c;} public int compareTo (Node o) {return this. c> o. c? 1:-1 ;}} import java. io. *; import java. util. *; public class Main {int MIN = Integer. MAX_VALUE; int M = 205, n, m; int patten [] = new int [M]; Node node []; public static void main (String [] args) {new Main (). work ();} void work () {vertex SC = new vertex (new BufferedInputStream (System. in); while (SC. hasNext () {n = SC. nextInt (); m = SC. nextInt (); init (); node = new Node [m]; for (int I = 0; I <m; I ++) {int a = SC. nextInt (); int B = SC. nextI Nt (); int c = SC. nextInt (); node [I] = new Node (a, B, c); union (a, B);} Arrays. sort (node); // sort int q = SC by weight. nextInt (); for (int I = 0; I <q; I ++) {int a = SC. nextInt (); int B = SC. nextInt (); getDistance (a, B) ;}}// obtain the shortest distance void getDistance (int a, int B) {int min = MIN; for (int I = 0; I <m; I ++) {init (); for (int j = I; j <m; j ++) {union (node [j]. a, node [j]. b); if (find (a) = find (B) min = Math. min (min, node [j]. c-node [I]. c) ;}} if (min! = MIN) System. out. println (min); elseSystem. out. println (-1);} // merge void union (int a, int B) {int pa = find (a); int pb = find (B ); if (pa = pb) return; patten [pa] = pb;} // query int find (int x) {if (patten [x] = x) return x; patten [x] = find (patten [x]); return patten [x];} // initialize void init () {for (int I = 0; I <= n; I ++) {patten [I] = I ;}} class Node implements Comparable <Node> {int a; int B; int c; Node (int a, int B, int c) {this. a = a; this. B = B; this. c = c;} pu Blic int compareTo (Node o) {return this. c> o. c? 1:-1 ;}}}