I was solving a programming problem on a site. On my machine (Visual Studio 2010) a test case gives wrong result, while gives correct result on the site. I am not sure what is the compiler of the site’s judge, but I think it is something like gcc or cygwin.
THE CODE
A graph problem. The graph here is represnted as a tree. The graph is directed, and doesn’t contain loops. The solution is (2 * sum of all edges - max path length from root)
// to-vertex & edge-length
vector<pair<int, int> > pr[100];
int dfs(int i) // to find max path length from root
{
int mx = 0;
for (int j = 0; j < pr[i].size(); ++j)
mx = max(mx, dfs(pr[i][j].first) + pr[i][j].second);
return mx;
}
int PowerOutage::estimateTimeOut(vector <int> from_vertex,
vector <int> to_vertex, vector <int> edge_length)
{
int tot = 0;
for (int i = 0; i < from_vertex.size(); ++i)
{
pr[from_vertex[i]].push_back(make_pair(to_vertex[i], edge_length[i]));
tot += (2 * edge_length[i]);
}
return tot - dfs(0);
}
THE TEST CASE
from_vertex {0, 0, 0, 0, 0}
to_vertex {1, 2, 3, 4, 5}
edge_length {100, 200, 300, 400, 500}
Visual Studio returns: 2493, While the site’s compiler returns the correct answer: 2500.
Why the two results are different? I think there is some hidden bug (in my code) that appears in VS giving wrong answer but disapper in the other compiler. Should I determine the site’s compiler and use it instead?
Despite my first(wrong) assumption, OP has found out that it was 2500 already but the test-function had the flaw after i asked him “where is 2500 printed?”.