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

#BF0000005. Vé máy bay rẻ nhất với K điểm dừng (Cheapest Flights Within K Stops)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Vé máy bay rẻ nhất với K điểm dừng (Cheapest Flights Within K Stops)

Cheapest Flights Within K Stops

Source: LeetCode

Version: Phuoc Hung OJ Extended

Problem Statement

Find the cheapest directed flight route from src to dst allowing at most k intermediate stops.

There are nn cities numbered 00 through n−1n-1 and directed flights. Each flight u→vu\to v costs a positive integer pp. Find a cheapest itinerary from city src to city dst.

The itinerary is allowed to have at most kk intermediate stops. A direct flight has zero stops, while a two-flight journey through a transfer city has one stop. Therefore, at most k+1k+1 flights can be taken. Print the cheapest eligible total fare, or -1 if no eligible itinerary exists.

Input

First line: n,m,src,dst,kn,m,src,dst,k (PHOJ input adaptation). Next mm lines u,v,pu,v,p.

Output

Print the minimum fare or -1 if none exists.

Subtasks

  • Subtask 1 (20 points): 2≤n≤202\le n\le20; 0≤m≤500\le m\le50; 0≤src,dst,k<n0\le src,dst,k<n; src≠dstsrc\ne dst; 1≤p≤100001\le p\le10000.
  • Subtask 2 (30 points): 2≤n≤1002\le n\le100; 0≤m≤5000\le m\le500; 0≤src,dst,k<n0\le src,dst,k<n; src≠dstsrc\ne dst; 1≤p≤100001\le p\le10000.
  • Subtask 3 (50 points): 2≤n≤1002\le n\le100; 0≤m≤49500\le m\le4950; 0≤src,dst,k<n0\le src,dst,k<n; src≠dstsrc\ne dst; 1≤p≤100001\le p\le10000.

Examples

Input

4 5 0 3 1
0 1 100
1 2 100
2 0 100
1 3 600
2 3 200

Output

700

Explanation

With k=1k=1, route 0→1→30\to1\to3 costs 100+600=700100+600=700; the cheaper three-edge route has too many stops.