Skip to content

Instantly share code, notes, and snippets.

@danicat
Created January 5, 2014 22:41
Show Gist options
  • Save danicat/8275028 to your computer and use it in GitHub Desktop.
Save danicat/8275028 to your computer and use it in GitHub Desktop.
TopCoder SRM 144 Division 2 1100 pts problem. It took me a while to figure this out, but once you notice that the technician must traverse each junction twice except for the largest path the solution comes quite straightforward.
#include <vector>
using std::vector;
class PowerOutage {
public:
int estimateTimeOut(vector<int> fromJunction, vector<int> toJunction, vector<int> ductLength) {
int max_minutes = 0;
for(auto p : ductLength) {
max_minutes += p * 2;
}
return max_minutes - longestPath(0, fromJunction, toJunction, ductLength);
}
int longestPath(int origin, vector<int>& fromJunction, vector<int>& toJunction, vector<int>& ductLength) {
int max_path = 0;
for(int i = 0; i < fromJunction.size(); ++i) {
if( fromJunction[i] == origin ) {
int path_size = ductLength[i] + longestPath( toJunction[i], fromJunction, toJunction, ductLength );
max_path = (path_size > max_path) ? path_size : max_path;
}
}
return max_path;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment