Don't Break the Nile (Large)
Time limit5sMemory limit512 MB
Compute the maximum unit flow from the south edge to the north edge of a grid blocked by up to 1000 rectangles.
- Level
Hard8 of 10
- Topics
- Shortest path, Graph, Geometry
- Solved
- No attempts yet
Problem
Aliens have landed. Their home planet has no flowing water at all, so they find the rivers of Earth interesting, and now they want to put up their buildings in some of those rivers. Your job is to check that the buildings do not block the rivers too much, because a blocked river causes serious trouble. Given the placement of the buildings, determine the maximum flow the river sustains.
The aliens prefer stretches of river that are straight and of uniform width, so you model the river as a rectangular grid. Each cell has integer coordinates with and . Each cell passes 1 unit of flow, and water moves between two cells that share an edge. Every cell on the south side of the river, meaning every cell with coordinate , receives 1 unit of incoming flow. All buildings are rectangles aligned with the grid, and a cell under a building passes no flow at all.
Under these rules, determine the maximum flow that reaches the north side of the river, meaning the cells with coordinate .
Input
The first line contains the number of test cases, . test cases follow.
The first line of each test case contains three integers: the width of the river , the height of the river , and the number of buildings placed in the river . Each of the next lines contains four integers , , , . is the lower left corner of the building and is the upper right corner. Buildings do not overlap, although two buildings can share an edge.
Limits
Output
For each test case, print one line in the form Case #x: m, where is the test case number starting from 1 and is the maximum flow that passes through the river.
Hint
The two pictures below draw the two test cases of the first example.
