Repovive
  • Dashboard
  • Ranking
  • Submissions
  • Discussion
  1. Contests
  2. Premier Round 1
  3. 2G

My submissions

/
/
/
/

Announcements

My submissions

/
/
/
/

Announcements

Solve
Author

2G. Zero Distance

3500 pts·2s·256 MB

Input

Output

ans\text{ans}ans

Samples

Input
2 1
1 2 5
Explanation
Output
0
Input
3 4
1 2 3
2 3 4
1 3 10
1 3 9
Explanation
Output
3

You are given a directed graph with nnn vertices and mmm directed edges.
Each directed edge (u→v)(u \to v)(u→v) has an integer weight www.

If you traverse an edge in its given direction (u→v)(u \to v)(u→v), the cost is www.
If you traverse it in the opposite direction (v→u)(v \to u)(v→u), the cost is −w-w−w.

For any walk, its cost is the sum of costs of its steps.

Define dist(a,b)dist(a,b)dist(a,b) as the minimum possible cost among all walks from aaa to bbb.

We say the graph is good if it has no negative closed walk, meaning there is no walk that starts and ends at the same vertex with total cost <0< 0<0.
Equivalently, the graph is good iff for every vertex vvv, we have dist(v,v)=0dist(v,v)=0dist(v,v)=0.

In one operation, you may choose one directed edge and do one of the following:

  • increase its weight by 111
  • decrease its weight by 111

Find the minimum number of operations needed to make the graph good.

Constraints:

  • 1≤n≤201 \le n \le 201≤n≤20
  • 0≤m≤4000 \le m \le 4000≤m≤400
  • 0≤w≤1000 \le w \le 1000≤w≤100
  • v≠uv \neq uv=u

nmu1v1w1u2v2w2⋮umvmwmn \quad m \\ u_1 \quad v_1 \quad w_1 \\ u_2 \quad v_2 \quad w_2 \\ \vdots \\ u_m \quad v_m \quad w_mnmu1​v1​w1​u2​v2​w2​⋮um​vm​wm​

No change is needed, so the minimum number of operations is 000.

Set the weights to:

  • (1→2)=4(1 \to 2)=4(1→2)=4
  • (2→3)=5(2 \to 3)=5(2→3)=5
  • the two edges (1→3)(1 \to 3)(1→3) both equal 999

This needs 111 operation on (1→2)(1 \to 2)(1→2), 111 operation on (2→3)(2 \to 3)(2→3), and 111 operation to change the edge (1→3)(1 \to 3)(1→3) from 101010 to 999, so the total is 333.