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?
尽管我的第一个(错误的)假设,OP已经发现它已经是2500,但是在我问他“ 2500打印在哪里?”之后,测试功能还是存在缺陷。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.