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

#FW0000011. Chuột thoát mê cung (Mice and Maze)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Chuột thoát mê cung (Mice and Maze)

Mice and Maze

Source: UVa

Version: Phuoc Hung OJ Extended

Problem Statement

Each of nn maze cells initially has one mouse. One-way passages have traversal times. Count mice able to reach exit cell ee within time TT, including the mouse already at ee.

Input

nn, exit ee, limit TT, and mm, followed by mm directed passages.

Output

Print the number of escaping mice.

Subtasks

  • Subtask 1 (40 points): n≤10n\le 10; 1≤n≤1001\le n\le 100; 1≤e≤n1\le e\le n; 0≤T≤1000000\le T\le 100000; 0≤m≤100000\le m\le 10000; 1≤w≤1000001\le w\le 100000.
  • Subtask 2 (60 points): 1≤n≤1001\le n\le 100; 1≤e≤n1\le e\le n; 0≤T≤1000000\le T\le 100000; 0≤m≤100000\le m\le 10000; 1≤w≤1000001\le w\le 100000.

Examples

Input

3
3
2
3
1 2 1
2 3 1
1 3 5

Output

3

Explanation

From cells 11, 22, and 33, the shortest times to exit 33 are 22, 11, and 00 respectively. All three escape.