Sunday, November 22, 2015

Interview Coding Problems : Solved

Hereon afterwards, I will be posting Java Code, for various problem sets, which are generally given during placement exams and interviews. These problems are well known and I certainly do not claim that these are most optimum, but I in the spirit of open source want to make my code available to those who might benefit from it.

If you feel this code helped you at all, just drop me a mail or comment, so that it keeps me motivated to keep doing it for more and more problem sets.

Thank you.

Today's problem : Based on Maxflow. 

Problem Statement
After succeeding to the British crown, you inherit a 16th-century Scottish castle with an elaborate plumbing system that has accumulated pipes and junctions over four centuries. You'd like to know if it is safe to install a modern American overHead Tank , or if this will eventually overflow the historic bathtub of James VI/James I.
Input Format
The first line of input consist of two integers N (no of junctions) and M (no of pipes) , separated by a single space.
Then, M lines follow. Each of these lines consist of three integers X, Y, Cxy separated by a single space. It means that there is a pipe between the junction X and the junction Y with a capacity of pipe equal to Cxy.
Assume junction 1 will be Source and junction N represents Sink.
Constraints:
3N500
3M500
1capacity of pipe 100
The junctions are indexed from 1 to N. its implicit that 1 is source and N is sink
Output Format
A yes or no, that represent to install new American overhead tank or not. A Integer representing maximum flow allowed my network into sink.
Sample Input
6 10
1 2 16
1 3 13
2 3 10
3 2 4
2 4 12
3 5 14
4 3 9
5 4 7
4 6 20
5 6 4
Sample Output
Yes
23
Explanation
The max flow of the network is 23 which is lesser than maximum input capacity of sink(bathtub) i.e 24.
Solution


No comments:

Post a Comment