QOJ.ac

QOJ

Límite de tiempo: 1 s Límite de memoria: 128 MB Puntuación total: 100

#15266. Logistics Transportation

Estadísticas

A logistics company needs to transport a batch of goods from port A to port B. Due to the large volume of goods, it will take $n$ days to complete the transport. During the transport process, the goods usually stop at several intermediate ports. The logistics company typically designs a fixed transport route to implement strict management and tracking of the entire process. Due to various factors, sometimes a certain port may be unable to load or unload goods. In such cases, the transport route must be modified to ensure the goods reach their destination on time. However, modifying the route is a very troublesome task and incurs additional costs. Therefore, the logistics company hopes to establish an $n$-day transport plan that minimizes the total cost.

Input

The first line contains four integers $n$ ($1 \le n \le 100$), $m$ ($1 \le m \le 20$), $K$, and $e$. $n$ represents the number of days required for transport, $m$ represents the total number of ports, $K$ represents the cost of each route modification, and $e$ represents the number of shipping routes. The next $e$ lines each contain three integers, representing the two port IDs connected by the route and the route length ($>0$). Port A is numbered 1, and port B is numbered $m$. The transport cost per unit length is 1. Routes are bidirectional.

The next line contains an integer $d$. The following $d$ lines each contain three integers $P$ ($1 < P < m$), $a$, and $b$ ($1 \le a \le b \le n$), indicating that port $P$ is unavailable for loading or unloading from day $a$ to day $b$ (inclusive). A single port may be unavailable during multiple time periods. However, at any given time, there is at least one transport route from port A to port B.

Output

Output a single integer representing the minimum total cost. The total cost is defined as the sum of the transport route lengths over $n$ days plus $K \times (\text{number of route changes})$.

Examples

Input 1

5 5 10 8
1 2 1
1 3 3
1 4 2
2 3 2
2 4 4
3 4 1
3 5 2
4 5 2
4
2 2 3
3 1 1
3 3 3
4 4 5

Output 1

32

Note

For the first three days, the route is 1-4-5, and for the last two days, the route is 1-3-5. The total cost is $(2+2) \times 3 + (3+2) \times 2 + 10 = 32$.

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.