luogu#P4806. [CCC 2017] 最小费用流

    ID: 3834 远端评测题 3000ms 256MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>2017CCC(加拿大)生成树最近公共祖先 LCA

[CCC 2017] 最小费用流

Problem Description

Translated from CCC 2017 Senior T4 “Minimum Cost Flow”.

In Watermoo, there are buildings numbered 1,2,,N1,2,\dots,N. There are also MM pipes, each connecting a pair of buildings. Due to an oversight in city planning, Building 11 is the only sewage treatment plant in the city. Each pipe can be active or inactive. If Building 11 is directly or indirectly connected to every other building using only active pipes, then the set of active pipes is called a valid plan. (Each pipe directly connects two buildings. If building XX is directly or indirectly connected to building YY, and building YY is directly or indirectly connected to building ZZ, then we say XX and ZZ are indirectly connected.)

The city government of Watermoo is currently using an obviously valid plan consisting of exactly N1N-1 pipes, but this has already pushed the budget too far. Each pipe has its own monthly maintenance cost, which must be paid if the pipe is active. The total cost of a valid plan is the sum of the maintenance costs of all active pipes. (Inactive pipes cost nothing.)

There is also good news: researchers at Watermoo University have developed an imperfect pipe booster, which you may use on exactly one pipe. It reduces that pipe’s maintenance cost from CC to max(0,CD)\mathrm{max}(0,C-D), where DD is the strength of the booster.

The government wants to minimize the cost, and also wants you to finish as quickly as possible. Each day, the city allows you to activate one pipe and deactivate one other pipe. How many days do you need so that the set of active pipes forms a valid plan and has the minimum possible cost among all valid plans and all choices of where to use the booster?

Note that during your process, the plan may be invalid, but in the end it must be a valid plan.

Input Format

The first line contains three integers N,MN, M and $D(1 \le N \le 100\ 000, N - 1 \le M \le 200\ 000, 0 \le D \le 10^9)$.

The next MM lines each contain three integers Ai,BiA_i, B_i and CiC_i, meaning there is a pipe with monthly maintenance cost CiC_i connecting building AiA_i and building BiB_i (1Ai,BiN,1Ci109)(1 \le A_i, B_i \le N, 1 \le C_i \le 10^9).

The first N1N-1 of these MM lines describe the valid plan Watermoo is currently using.

It is guaranteed that there are no multiple edges and no self-loops in the testdata.

Output Format

Output one line containing an integer, the minimum number of days needed to finish the task. If the initial plan is already optimal, output 0.

4 4 0
1 2 1
2 3 2
3 4 1
4 1 1
1
5 6 2
1 2 5
2 3 5
1 4 5
4 5 5
1 3 1
1 5 1
2
4 4 0
1 2 715827882
2 3 715827882
3 4 715827882
4 1 715827884
0

Hint

Sample Explanation 1

Since D=0D=0, the pipe booster is useless.

On the first day, you should deactivate the pipe between buildings 22 and 33 and activate the pipe between buildings 44 and 11.

Sample Explanation 2

One feasible solution is: first install the booster on the pipe connecting buildings 11 and 22, reducing its cost to 33. On the first day, replace the pipe connecting 22 and 33 with the pipe connecting 11 and 33. On the second day, replace the pipe connecting 11 and 44 with the pipe connecting 11 and 55.

In addition, installing the booster on the pipe connecting 11 and 33 or the pipe connecting 11 and 55 is meaningless. Doing so would make that pipe’s maintenance cost 00, and the optimal total cost would be 1111 (as you can see, we have already found a plan with total cost 1010).

Sample Explanation 3

The initial plan is already optimal. Please note integer overflow.

For 315\frac3{15} of the testdata, N8,M28,D=0N \le 8, M \le 28, D=0.

For another 515\frac5{15} of the testdata, N1 000,M5 000,D=0N \le 1\ 000, M \le 5\ 000, D=0.

For another 315\frac3{15} of the testdata, D=0D=0.

For another 215\frac2{15} of the testdata, N1 000,M5 000N \le 1\ 000, M \le 5\ 000.

Translated by ChatGPT 5