PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#FW0000113. Số cách đóng chi nhánh hợp lệ (Number of Possible Sets of Closing Branches)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Số cách đóng chi nhánh hợp lệ (Number of Possible Sets of Closing Branches)

Number of Possible Sets of Closing Branches

Source: LeetCode

Version: Phuoc Hung OJ Extended

Problem Statement

An undirected weighted graph has nn branches indexed from 00 to n−1n-1, with possible parallel roads. Choose any subset of branches to close. All closed branches, including roads incident to them, become unusable. A closing set is valid if every two remaining open branches have shortest-path distance at most DD in the induced graph of open branches. Zero or one open branch is always valid. Count all valid closing sets.

Input

First line: n,D,mn,D,m. Then mm undirected weighted edges u,v,wu,v,w. Vertices are 00-indexed.

Output

Print the number of valid closing sets.

Subtasks

  • Subtask 1 (40 points): 1≤n≤51\le n\le 5, 0≤m≤120\le m\le 12, 1≤D≤251\le D\le 25, 1≤w≤251\le w\le 25. All input conditions remain in force.

  • Subtask 2 (60 points): 1≤n≤101\le n\le 10, 0≤m≤10000\le m\le 1000, 1≤D≤1051\le D\le 10^5, 1≤w≤10001\le w\le 1000. All input conditions remain in force.

Examples

Input

3 2 3
0 1 2
1 2 2
0 2 10

Output

6

Explanation

There are 4 valid open sets of size at most 1, plus the valid open pairs {0,1} and {1,2}; total 6.