一道信息学奥赛题,请帮忙解一下。
#include <stdio.h> #define V 100
int n, m, ans, e[V][V], visited[V];
void dfs(int x, int len)
{
int i;
visited[x] = 1;
if (len > ans)
ans = len;
for (i = 1; i <= n; i++)
if ((visited[i] == 0) && (e[x][i] != -1))
dfs(i, len + e[x][i]);
visited[x] = 0;
}
int main()
{
int i, j, a, b, c;
scanf("%d%d", &n, &m);
for (i = 1; i <= n; i++)
for (j = 1; j <= n; j++)
e[i][j] = -1;
for (i = 1; i <= m; i++) {
scanf("%d%d%d", &a, &b, &c);
e[a][b] = c;
e[b][a] = c;
}
for (i = 1; i <= n; i++)
visited[i] = 0;
ans = 0;
for (i = 1; i <= n; i++)
dfs(i, 0);
printf("%d\n", ans);
return 0;
}
输入:
4 6
1 2 10
2 3 20
3 4 30
4 1 40
1 3 50
2 4 60
输出:____150_____
程序没有问题,可是我在主函数第四次调用dfs时就出不来这个答案,请高手指点。