Hints for SRM 632, D1, 500-pointer (CandyCupRunningCompetition)

James S. Plank

Wed Oct 22 02:30:47 EDT 2014
Problem Statement.
First, work through the examples. It should be pretty clear that this is a network flow problem. You are being asked to find the maximum flow through the graph. Easy peasy. Or not. The problem is the edge capacities -- they can be huge (31999)!

The value 3i is a nice one because of this property: if an edge's capacity is 3i, and you subtract all of the capacities that are lower, you remain with a value greater than 3i-1. Why does this matter? Well, you can do a network flow processing without calculating the maximum flow. However, the residual graph that remains is the same as it would be if you were calculating the maximum flow. When you're done, you use the residual to determine the minimum cut, and from the cut, you can get the answer.

That's probably hard to parse, so let's be more explicit. Your residual graph is not going to contain edge capacities. Instead, it is going to two values per edge: a log l and a multiplier m. The capacity of the edge is going to be 3l*m. You'll start with each edge having l be its index in A, and its multiplier is one.

You'll then do network flow (I did Edmonds-Karp). When you process the residual, you find the edge with the smallest value of l, and then you subtract its multiplier, and add its multiplier to the back edge. You don't bother with any other edges in the path, because subtracting or adding 3l won't change the edges' values relative to the other edges. And it's the relative value that counts, because all we care about is the residual graph.

When we're done, we find the minimum cut, using the residual graph. That is a collection of edges, so we can add up their real capacities modulo 1,000,000,007, and we get our answer!

Let's use example 1 to illustrate. Here's the graph, where capacities are represented with l and m rather than with actual capacities.

We find the augmenting path 0-1-3. Its flow is one, but what we're going to do is find the smallest value of l, which is zero. We are only going to remove flow and add reverse flow to the edges with l equal to zero, because modifying the others won't change their relative values. We're left with:

See how we use the multiplier to add and subtract flow? Now, the next augmenting path that we find is 0-2-3. The minimum l is one, so we mess with the multipliers of the forward and reverse edges of the edge 0-2.

Now, the residual has cut off the source from the sink. We use the residual to identify the minimum cut, as described in class. That cut is 0-1 with a capacity of 30 = 1 and 0-2 with a capacity of 32 = 9. The minimum cut is 10, which is the maximum flow.

You have to program this one carefully -- it is a wonderful problem!