Red segments and blue segments
Time limit2sMemory limit512 MB
Color N points red or blue, then draw non-crossing same-color segments so that no red and blue segment touch; maximize total segment scores.
- Level
Hard8 of 10
- Topics
- Dynamic programming, Geometry, Backtracking, Combinatorics
- Solved
- No attempts yet
Problem
There are distinct points on a plane, numbered from 0 to .
You collect a score by drawing segments between points. The game runs in two stages. In the first stage you paint every point red or blue. In the second stage you draw zero or more segments.
A segment joins two points of the same color, and the segment takes the color of those points.
Two segments of the same color may touch or cross. Two segments of different colors may not touch and may not cross. A red segment and a blue segment must share no point at all, and an endpoint of one may not lie on the other.
The restriction applies only to the segments you actually draw. A red segment may pass through a blue point as long as you draw no blue segment that touches that point.
The red segment joining point and point scores red[i][j], and the blue segment joining the same two points scores blue[i][j].
Write a program that finds the largest score you can collect by drawing segments.
Input
The first line contains the number of points . ()
Each of the next lines contains the coordinates and of one point, starting with point 0. () No two points share the same coordinates.
The next lines contain the score matrix of the red segments. The -th number on the -th of those lines is red[i][j]. The following lines contain the score matrix blue[i][j] of the blue segments in the same layout. Every red[i][j] and every blue[i][j] is an integer between 0 and 100,000.
For every , red[i][i] and blue[i][i] are 0, and for every and , red[i][j] equals red[j][i] and blue[i][j] equals blue[j][i].
Output
Print the maximum score you can collect on the first line.
Hint
In the first example you paint every point blue and draw all six blue segments, which gives .