0

我正在解决一个网站上的编程问题。在我的机器(Visual Studio 2010)上,一个测试用例给出了错误的结果,而在网站上给出了正确的结果。我不确定该站点的判断的编译器是什么,但我认为它类似于 gcc 或 cygwin。

编码

一个图形问题。这里的图表示为一棵树。该图是有向的,并且不包含循环。解决方案是(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);
}

测试用例

from_vertex   {0,     0,   0,   0,   0}
to_vertex     {1,     2,   3,   4,   5}
edge_length   {100, 200, 300, 400, 500}

Visual Studio 返回: 2493而站点的编译器返回正确答案: 2500

为什么两个结果不一样?我认为有一些隐藏的错误(在我的代码中)出现在 VS 中给出错误的答案,但在其他编译器中消失了。我应该确定站点的编译器并改用它吗?

4

1 回答 1

0

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?".

于 2012-09-02T07:33:37.687 回答