## CCC '17 S4 - Minimum Cost Flow

View as PDF

Points: 17 (partial)
Time limit: 1.4s
Memory limit: 256M

Author:
Problem type
##### Canadian Computing Competition: 2017 Stage 1, Senior #4

The city of Watermoo has buildings numbered . The city has pipes that connect pairs of buildings. Due to urban planning oversights, building is the only sewage treatment plant in the city. Each pipe can be either active or inactive. The set of active pipes forms a valid plan if building is directly or indirectly connected to each other building using active pipes. (Pipes directly connect pairs of buildings. Buildings and are indirectly connected if is directly or indirectly connected to , and is directly or indirectly connected to .)

The municipal government of Watermoo is currently operating a valid plan of pipes today, but they think it is too expensive! Each pipe has a monthly maintenance fee that the city must pay when it is active, and the total cost of a valid plan is the sum of the maintenance fees of its active pipes. (Inactive pipes cost nothing.)

Additionally, researchers at the University of Watermoo have developed an experimental pipe enhancer which you can use on one pipe of your choice. It will reduce that pipe's cost from down to , where is the enhancer's strength.

The city wants you to minimize the cost of the plan, and they want you to do it quickly. Every day, the city will allow you to activate one pipe, and deactivate another pipe. How many days do you need to make the set of active pipes form a valid plan, whose cost is minimum among all valid plans and all choices of enhanced pipe?

Note that it is possible that the plan becomes invalid while you are working, but by the end it should be a valid plan.

#### Input Specification

The first line will contain the integers , , and . Each of the next lines contain three integers , , and , which means that there is a pipe from building to building that costs per month when activated . The first of these lines represent the valid plan the city is currently using.

It is guaranteed that there is at most one pipe connecting any two buildings and no pipe connects a building to itself.

For 3 of the 17 available marks, , and .
For an additional 5 of the 17 available marks, and and .
For an additional 3 of the 17 available marks, .
For an additional 2 of the 17 available marks, and .

Note: The final 2 of the 17 available marks consists of test cases made by d and r3mark and were not present on the CCC. These test cases were made in response to the initial incorrect official solution presented.

#### Output Specification

Output one integer on a single line, the minimum number of days to complete this task. If the initial valid plan is already an optimal plan, then output .

#### Sample Input 1

4 4 0
1 2 1
2 3 2
3 4 1
4 1 1

#### Sample Output 1

1

#### Explanation for Sample Output 1

Note that it does not matter which pipe you use the pipe enhancer on because , so it will not affect the maintenance fee of any pipe.
On the first day, you should deactivate the pipe from building to and activate the pipe from building to .

#### Sample Input 2

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

#### Sample Output 2

2

#### Explanation for Sample Output 2

One solution using the minimum number of days is to first use the pipe enhancer on the pipe from building to to decrease its cost to . Then on the first day, replace the pipe from building to with the pipe from building to , and on the second day replace the pipe from to with the pipe from building to . Note that the cost of the optimal plan is .
Additionally, there are no solutions where you use the pipe enhancer on the pipe from building to or the pipe from building to . Doing so would make that pipe have a maintenance fee of , and then any optimal plan would have cost (and we have already seen that we can achieve a cost of ).

#### Sample Input 3

4 4 0
1 2 715827882
2 3 715827882
3 4 715827882
4 1 715827884

#### Sample Output 3

0

#### Explanation for Sample Output 3

The initial valid plan is already an optimal plan. Be careful of integer overflow when implementing your solution.

• commented on May 14, 2022, 3:16 a.m.

Can Prim's be used to solve this?

• commented on Feb. 13, 2021, 12:55 p.m.

Is DFS fast enough to pass?(for python or c++)

• commented on Feb. 13, 2021, 2:13 p.m. edited

why would you need DFS, this is a minimum spanning tree problem..

• commented on Dec. 5, 2018, 1:17 a.m.

CopyPastingFromGithubIsWrong just because this is your username I don't think you can use the editorial ... https://dmoj.ca/src/1117801 and I don't think "just seeing if it works" is a valid reason to use it. I'm pretty sure it would work since it's the editorial???

• commented on March 15, 2022, 5:39 a.m.

I agree, copying code is an egregious crime - I can't believe anyone would so such a thing...

• commented on April 22, 2018, 5:51 p.m.

If you only use the pipe enhancer, then does it still count as a day?

• commented on July 3, 2017, 12:35 a.m. edit 3

This comment is hidden due to too much negative feedback. Show it anyway.

• commented on March 7, 2017, 7:24 p.m.

Help on last sample case. How is it a valid plan for the last sample case? Doesnt the pipe from 4 to 1 need to be removed?

• commented on March 7, 2017, 8:22 p.m.

The first of these lines represent the valid plan the city is currently using.

As you can see, the first three form a perfectly valid plan.

• commented on March 3, 2017, 3:10 a.m. edit 2

New cases by d and r3mark have been added as a 2 point subtask. All submissions have been rejudged.

• commented on June 17, 2017, 9:30 p.m. edited

This comment is hidden due to too much negative feedback. Show it anyway.

• commented on June 17, 2017, 9:42 p.m.

A new case by d has been added as a 1 point subtask. All submissions have been rejudged.

New cases by d and r3mark have been added as a 2 point subtask. All submissions have been rejudged.

The second edit is because the number of cases increased from 1

• commented on Dec. 2, 2018, 4:15 p.m.

This comment is hidden due to too much negative feedback. Show it anyway.

• commented on Dec. 5, 2018, 2:03 a.m.

Guys, this post is from over a year ago... don't beat a dead horse.