sicily 1024 Magic Island dfs
这题就是个杯具~~~简单的dfs, WA了无数次,后来发现是因为每次cases忘了重新把vector数组clear掉,晕死~~~
#include<iostream> #include <vector> #include <stdio.h> #include <memory.h> using namespace std; struct info { int id; int len; info(int id, int len) { this->id = id; this->len = len; } info(){} }; vector<info> head[10005]; bool isvisit[10005]; int N, K; int result; int sum = 0; void dfs(int, int); int main() { int x, y, distance; while (cin >> N) { cin >> K; sum = 0; result = 0; memset(isvisit, false, sizeof(isvisit)); for (int i = 1; i < N; i++) { scanf("%d%d%d", &x, &y, &distance); head[x-1].push_back(info(y-1, distance)); head[y-1].push_back(info(x-1, distance)); } isvisit[K-1] = true; dfs(K-1, 0); cout << result << endl; for (int i = 0; i < N; i++) head[i].clear(); } return 0; } void dfs(int begin, int sum) { for (int i = 0; i < head[begin].size(); i++) { if (!isvisit[head[begin][i].id]) { isvisit[head[begin][i].id] = true; sum += head[begin][i].len; result = max(result, sum); dfs(head[begin][i].id, sum); sum -= head[begin][i].len; isvisit[head[begin][i].id] = false; } } }