Editorial for WC '15 Contest 1 S3 - Contest Sites
Submitting an official solution before solving the problem yourself is a bannable offence.
This is a graph theory problem that all comes down to casework. It is good practice for many contest problems where we can make life easier by pruning out all of the trivial cases first before making bigger decisions.
The network of towns and roads can be represented as a directed, weighted graph. First we will need to determine the shortest distance from each town to each of the two contest sites. Finding shortest paths is a well-known problem in computer science, and many great online tutorials can be easily found. Two algorithms for finding the pairwise distance between every node in the graph are the Floyd-Warshall algorithm and Dijkstra's algorithm. The former is very simple to code and runs in time while the latter will run for per starting node, or if we run it starting from all nodes. However, since is as big as , both of these methods will not run in time for larger cases. To obtain full marks, we can simply reverse the directions of all edges in the graph, and then run Dijkstra's algorithm backwards from the two contest sites. This will give us the distances from every node to the contest sites in merely time.
Now that we have the distances from every town to the contest sites, we can individually consider each town with more than competitors. Let's say for a town, the distance to the main site is and the distance to the secondary site is . There are 4 possible cases:
- . This means we cannot reach either of the contest sites from the town we're currently examining. Immediately, we know that there is no way to distribute all the contestants.
- Only . Since we cannot reach town , we know that we must send the competitors from this town to . This is yet another trivial case we can get out of the way.
- . Since the main site is as close or closer to the secondary site, and sending competitors has no downside, we might as well send everyone from this town to the main site.
- . This is the toughest case because we have to make a decision. It requires less distance for competitors in this town to attend the secondary site, but not necessarily all of them can do it considering the limit of . We know for every competitor in this town that attends the main site over the secondary site, they will have to travel an excess distance of compared to those who attend the secondary site. The solution is to sort all of the towns by decreasing value of (the sacrifice to attend the main site over the secondary), and send as many competitors from the beginning of this sorted list as possible to the secondary site, until we run out of capacity there. Then we send the remainder to the primary site.
Comments