Maniacal Midsummer Marathon 2014 by AL, TL, JJ
You have just been hired as the new manager of a city's main power plant. Going to work on your first day, you learned that the company has been losing massive amounts of money. After some investigating, you've discovered that this is because the company has been delivering unnecessarily high amounts of energy to the substations in the city's electricity grid!
The electricity grid consists of
Some of these substations are just too wasteful, so it's time for a
reform! You would like to pick out some subset of all of the substations
in the entire city to keep for the long term. There is a catch: for any
of the substations you wish to keep, you must keep all the substations
that depend on it for electricity, directly or indirectly. More
specifically, in order to keep a substation
Just when you thought your new job couldn't get any harder, the tyrants
from the EPA came over and complained that the maximum energy usage
across your substations is too high, and will harm the environment.
Every month, they will fine you
To save yourself from some headaches, you might as well write a program to determine the highest possible total monthly profit that you can obtain by keeping a subset of the power substations.
Input Specification
Line
Next
Next
Output Specification
A single integer representing the highest possible total monthly profit
(in dollars) obtainable by selecting a subset of all of the
Sample Input 1
4 3 1
5 3
-7 2
15 4
-3 1
1 2
2 3
3 4
Sample Output 1
8
Explanation for Sample 1
There are
If we choose to keep substations
The EPA fine will be
Sample Input 2
4 4 1
-1 1
2 1
-3 1
4 1
1 2
2 3
3 4
4 1
Sample Output 2
1
Explanation for Sample 2
It's possible that cyclic dependencies exist, as shown by these four
substations. You must take either all or none, and your best choice is
to take them all for a profit of
Sample Input 3
3 3 8
15 2
-10 1
10 1
1 2
3 2
1 3
Sample Output 3
0
Explanation for Sample 3
It is not profitable to keep any substation because the fine by the EPA
would always be more than the profit obtainable from keeping any valid
subset of substations.
(With no functional substations anywhere, you should probably be heading
to the bank to file bankruptcy for your company.)
Comments