Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.math.BigInteger;
- import java.util.ArrayList;
- import java.util.LinkedList;
- import java.util.Queue;
- import java.util.StringTokenizer;
- /**
- * <b> Algorithm on Codeforces. Problem E div 2 </b> </br>
- *
- * @author Huynh Quang Thao
- *
- */
- public class Main {
- public static void main(String[] args) throws Exception {
- Main main = new Main();
- main.run();
- }
- public void run() throws Exception {
- InputReader sc = null;
- sc = new InputReader(System.in);
- sc = new InputReader(new FileInputStream(new File("input.txt")));
- int ntestcase = sc.nextInt();
- while (ntestcase-- > 0) {
- int n = sc.nextInt();
- int m = sc.nextInt();
- int a = sc.nextInt() - 1;
- int b = sc.nextInt() - 1;
- ArrayList<ArrayList<Edge>> adjList = new ArrayList<ArrayList<Edge>>();
- for (int i = 0; i < n; i++) {
- ArrayList<Edge> neighbor = new ArrayList<Edge>();
- adjList.add(neighbor);
- }
- for (int i = 0; i < m; i++) {
- int u = sc.nextInt() - 1;
- int v = sc.nextInt() - 1;
- int w = sc.nextInt();
- adjList.get(u).add(new Edge(v, w));
- adjList.get(v).add(new Edge(u, w));
- }
- int left = 0;
- int right = 1000;
- while (left + 1 < right) {
- int mid = (left + right) / 2;
- if (dfs(a, b, adjList, mid))
- left = mid;
- else
- right = mid;
- }
- System.out.println(left);
- }
- }
- public boolean dfs(int s, int t, ArrayList<ArrayList<Edge>> adjList, int cap) {
- boolean[] check = new boolean[adjList.size()];
- Queue<Integer> queue = new LinkedList<Integer>();
- queue.add(s);
- while (!queue.isEmpty()) {
- int v = queue.poll();
- if (check[v])
- continue;
- check[v] = true;
- if (v == t)
- return true;
- for (int i = 0; i < adjList.get(v).size(); i++) {
- int u = adjList.get(v).get(i).u;
- int w = adjList.get(v).get(i).weight;
- if (cap <= w && !check[u])
- queue.add(u);
- }
- }
- return false;
- }
- static class Edge {
- int u, weight;
- public Edge(int u, int weight) {
- this.u = u;
- this.weight = weight;
- }
- }
- static class InputReader {
- public BufferedReader reader;
- public StringTokenizer tokenizer;
- public InputReader(InputStream stream) {
- reader = new BufferedReader(new InputStreamReader(stream));
- tokenizer = null;
- }
- public String next() {
- while (tokenizer == null || !tokenizer.hasMoreTokens()) {
- try {
- tokenizer = new StringTokenizer(reader.readLine());
- }
- catch (IOException e) {
- throw new RuntimeException(e);
- }
- }
- return tokenizer.nextToken();
- }
- public int nextInt() {
- return Integer.parseInt(next());
- }
- public double nextDouble() {
- return Double.parseDouble(next());
- }
- public float nextFloat() {
- return Float.parseFloat(next());
- }
- public long nextLong() {
- return Long.parseLong(next());
- }
- public BigInteger nextBigInteger() {
- return new BigInteger(next());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment