This page is still under construction.

Parts of this page are still being built. What you see may change.

Widening of Channels

Time limit1sMemory limit128 MB

Summary
Given a weighted undirected graph, find the minimum number of edges to widen to width k so that every pair of vertices is connected.
Level

Medium6 of 10

Topics
Graph, Union-find, Greedy, Sorting
Solved
No attempts yet

Problem

In the country of Waterland there are nn lakes, numbered from 11 to nn, and mm channels connecting them. Each channel has a known width (in meters), and every channel can be navigated in both directions. It is guaranteed that a boat one meter wide can reach every lake starting from lake 11.

Write a program that computes the minimum number of channels that must be widened so that a boat kk meters wide can travel between every pair of lakes. A boat can pass through a channel only if its width is less than or equal to the width of the channel; that is, a channel of width ww can be used when k≤wk \le w. Widening a channel raises its width to at least kk.

Input

The first line contains two integers nn and mm (1<n≤10001 < n \le 1000, 1<m≤1000001 < m \le 100000).

Each of the next mm lines contains three integers ii, jj, and ww, meaning that there is a channel of width ww between lakes ii and jj (1≤i,j≤n1 \le i, j \le n, 1≤w≤2001 \le w \le 200).

The last line contains the integer kk (1≤k≤2001 \le k \le 200).

Output

Print a single integer: the minimum number of channels that must be widened.

Examples3

  1. Example 1

    Input
    6 9
    1 6 1
    1 2 2
    1 4 3
    2 3 3
    2 5 2
    3 4 4
    3 6 2
    4 5 5
    5 6 4
    4
    
    Expected output
    2
    
  2. Example 2

    Input
    3 3
    1 2 5
    2 3 5
    1 3 5
    5
    
    Expected output
    0
    
  3. Example 3

    Input
    4 3
    1 2 1
    2 3 1
    3 4 1
    2
    
    Expected output
    3