hqt

Binary Search

hqt
Sep 18th, 2013
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.10 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.io.InputStreamReader;
  7. import java.math.BigInteger;
  8. import java.util.ArrayList;
  9. import java.util.LinkedList;
  10. import java.util.Queue;
  11. import java.util.StringTokenizer;
  12.  
  13. /**
  14.  * <b> Algorithm on Codeforces. Problem E div 2 </b> </br>
  15.  *
  16.  * @author Huynh Quang Thao
  17.  *
  18.  */
  19. public class Main {
  20.  
  21.     public static void main(String[] args) throws Exception {
  22.         Main main = new Main();
  23.         main.run();
  24.     }
  25.  
  26.     public void run() throws Exception {
  27.         InputReader sc = null;
  28.  
  29.         sc = new InputReader(System.in);
  30.         sc = new InputReader(new FileInputStream(new File("input.txt")));
  31.  
  32.         int ntestcase = sc.nextInt();
  33.         while (ntestcase-- > 0) {
  34.             int n = sc.nextInt();
  35.             int m = sc.nextInt();
  36.             int a = sc.nextInt() - 1;
  37.             int b = sc.nextInt() - 1;
  38.  
  39.             ArrayList<ArrayList<Edge>> adjList = new ArrayList<ArrayList<Edge>>();
  40.             for (int i = 0; i < n; i++) {
  41.                 ArrayList<Edge> neighbor = new ArrayList<Edge>();
  42.                 adjList.add(neighbor);
  43.             }
  44.  
  45.             for (int i = 0; i < m; i++) {
  46.                 int u = sc.nextInt() - 1;
  47.                 int v = sc.nextInt() - 1;
  48.                 int w = sc.nextInt();
  49.                 adjList.get(u).add(new Edge(v, w));
  50.                 adjList.get(v).add(new Edge(u, w));
  51.             }
  52.  
  53.             int left = 0;
  54.             int right = 1000;
  55.             while (left + 1 < right) {
  56.                 int mid = (left + right) / 2;
  57.                 if (dfs(a, b, adjList, mid))
  58.                     left = mid;
  59.                 else
  60.                     right = mid;
  61.             }
  62.  
  63.             System.out.println(left);
  64.  
  65.         }
  66.     }
  67.  
  68.     public boolean dfs(int s, int t, ArrayList<ArrayList<Edge>> adjList, int cap) {
  69.         boolean[] check = new boolean[adjList.size()];
  70.         Queue<Integer> queue = new LinkedList<Integer>();
  71.         queue.add(s);
  72.         while (!queue.isEmpty()) {
  73.             int v = queue.poll();
  74.             if (check[v])
  75.                 continue;
  76.             check[v] = true;
  77.             if (v == t)
  78.                 return true;
  79.             for (int i = 0; i < adjList.get(v).size(); i++) {
  80.                 int u = adjList.get(v).get(i).u;
  81.                 int w = adjList.get(v).get(i).weight;
  82.                 if (cap <= w && !check[u])
  83.                     queue.add(u);
  84.             }
  85.         }
  86.         return false;
  87.     }
  88.  
  89.     static class Edge {
  90.         int u, weight;
  91.  
  92.         public Edge(int u, int weight) {
  93.             this.u = u;
  94.             this.weight = weight;
  95.         }
  96.     }
  97.  
  98.     static class InputReader {
  99.         public BufferedReader reader;
  100.  
  101.         public StringTokenizer tokenizer;
  102.  
  103.         public InputReader(InputStream stream) {
  104.             reader = new BufferedReader(new InputStreamReader(stream));
  105.             tokenizer = null;
  106.         }
  107.  
  108.         public String next() {
  109.             while (tokenizer == null || !tokenizer.hasMoreTokens()) {
  110.                 try {
  111.                     tokenizer = new StringTokenizer(reader.readLine());
  112.                 }
  113.                 catch (IOException e) {
  114.                     throw new RuntimeException(e);
  115.                 }
  116.             }
  117.             return tokenizer.nextToken();
  118.         }
  119.  
  120.         public int nextInt() {
  121.             return Integer.parseInt(next());
  122.         }
  123.  
  124.         public double nextDouble() {
  125.             return Double.parseDouble(next());
  126.         }
  127.  
  128.         public float nextFloat() {
  129.             return Float.parseFloat(next());
  130.         }
  131.  
  132.         public long nextLong() {
  133.             return Long.parseLong(next());
  134.         }
  135.  
  136.         public BigInteger nextBigInteger() {
  137.             return new BigInteger(next());
  138.         }
  139.  
  140.     }
  141. }
Advertisement
Add Comment
Please, Sign In to add comment