luogu#P6190. [NOI Online #1 入门组] 魔法

    ID: 4221 远端评测题 1000ms 250MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>动态规划 DP图论2020倍增矩阵加速最短路NOI Online

[NOI Online #1 入门组] 魔法

Problem Description

Country C consists of nn cities and mm directed roads. Both cities and roads are numbered starting from 11. Traversing road ii costs tit_i.

Now you want to travel from city 11 to city nn. You may cast magic at most kk times, so that when you traverse the next road, the cost becomes the opposite of the original, i.e. the cost changes from tit_i to ti-t_i. Please compute the minimum total cost needed to finish this trip.

Note: Casting magic only changes the cost for one traversal, and does not change the road’s own tit_i. The final total cost may be negative, and a city may be visited multiple times (including city nn).

Input Format

The first line contains three integers, representing the number of cities nn, the number of roads mm, and the limit on the number of times you can cast magic kk.

Lines 22 to (m+1)(m + 1) each contain three integers. On line (i+1)(i + 1), the integers ui,vi,tiu_i, v_i, t_i indicate that there is a one-way road from uiu_i to viv_i, and traversing it costs tit_i.

Output Format

Output one integer in one line, representing the minimum total cost.

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

-8
2 2 2
1 2 10
2 1 1

-19

Hint

Explanation for Sample Input/Output 1

Traverse road 11, road 22, and road 33 in order, and cast magic before traversing roads 11 and 22.

Explanation for Sample Input/Output 2

Traverse road 11, road 22, and road 11 in order, and cast magic before both times you traverse road 11.

Constraints

This problem has 2020 test points. The information for each test point is shown in the table below.

Test Point ID nn \leq mm \leq kk \leq Acyclic
121 \sim 2 55 2020 00 Not guaranteed
343 \sim 4 1010 5050
565 \sim 6 00
787 \sim 8 2020 200200 5050 Yes
9109 \sim 10 00 Not guaranteed
111211 \sim 12 100100 5050 Yes
131413 \sim 14 Not guaranteed
151815 \sim 18 25002500 10001000
192019 \sim 20 10610^6

For test points where the “Acyclic” column is “Yes”, it is guaranteed that the given graph is a directed acyclic graph; otherwise, there is no guarantee about the graph structure.

For all test points, it is guaranteed that:

  • 1n1001 \leq n \leq 100, 1m25001 \leq m \leq 2500, 0k1060 \leq k \leq 10^6.
  • 1ui,vin1 \leq u_i, v_i \leq n, 1ti1091 \leq t_i \leq 10^9.
  • The given graph has no multiple edges or self-loops, and there exists at least one path from 11 to nn.

**Unofficial testdata is generated within 5 minutes using CYaRon. If you find any issues with the testdata, please post in the discussion board or send a private message to

/user/22030

Translated by ChatGPT 5