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 substations and wires connecting the substations. These substations are interspersed throughout the city and are responsible for distributing power to consumers. Electricity can only travel in one direction along each wire, and the direction on a wire can never be reversed. The substations are each labeled with a unique integer from to . Substation has a monthly energy usage of megajoules. Through field research, your company has determined that substation supplies the company with a monthly profit of dollars.
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 , any substation such that electricity can flow from to using any series of connected wires/substations, must also be kept.
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 dollars for every megajoule of , your maximum energy usage. The value of for a set of substations is the maximum across all in the set. Therefore, your total monthly profit after the reform is the sum of all values for the stations you choose to keep, subtract across those stations. The fine per megajoule will be a nonnegative integer less than .
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 : three integers , , and , representing the number of
substations, the number of wires, and the EPA fine factor.
Next lines: two integers and , representing the
profit and monthly energy usage of station .
Next lines: two integers and , indicating that there
is a one-way wire from substation to .
Output Specification
A single integer representing the highest possible total monthly profit (in dollars) obtainable by selecting a subset of all of the substations.
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 substations with profits and monthly
energy usages of .
If we choose to keep substations and , then the monthly subtotal
profit is .
The EPA fine will be , so the total monthly profit is
. In fact, is the best answer for this example.
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