题目:
Constraints
Time Limit: 1 secs, Memory Limit: 32 MB
Description
There are N cities and N-1 roads in Magic-Island. You can go from one city to any other. One road only connects two cities. One day, The king of magic-island want to visit the island from the capital. No road is visited twice. Do you know the longest distance the king can go.
Input
There are several test cases in the input
A test case starts with two numbers N and K. (1<=N<=10000, 1<=K<=N). The cities is denoted from 1 to N. K is the capital.
The next N-1 lines each contain three numbers X, Y, D, meaning that there is a road between city-X and city-Y and the distance of the road is D. D is a positive integer which is not bigger than 1000.
Input will be ended by the end of file.
Output
One number per line for each test case, the longest distance the king can go.
Sample Input
3 1 1 2 10 1 3 20
Sample Output
20
Problem Source
ZSUACM Team Member
思路:因为要求出国王能走的最远路径,所以我们考虑用深度优先遍历。将出发点标记为已访问,其他点标记为未访问,然后进行深度优先遍历,此处要注意,嵌套DFS后,要进行回溯,即将嵌套DFS后的状态恢复为嵌套前,否则for循环中下一个点进行操作时,会带有嵌套DFS中改变了的状态。具体代码如下
代码:
#include <iostream>
#include <stdio.h>
#include <map>
#include <vector>
#include <cstring>
using namespace std;
class Node {
public:
int from;
int edge;
int to;
};
int longest;
map<int, vector<Node> > nodes;
bool visited[10001];
void DFS(int k, int distance) {
for (int i = 0; i < nodes[k].size(); i++) {
if (visited[nodes[k].at(i).to] == false) {
visited[nodes[k].at(i).to] = true;
distance += nodes[k].at(i).edge;
if(distance > longest)
longest = distance;
DFS(nodes[k].at(i).to, distance);
distance -= nodes[k].at(i).edge;
visited[nodes[k].at(i).to] = false;
}
}
}
int main() {
int n, k;
while(cin >> n >> k){
memset(visited, false, sizeof(visited));
for (int i = 0; i < n - 1; i ++){
int from, to, edge;
cin >> from >> to >> edge;
Node temp;
temp.from = from;
temp.to = to;
temp.edge = edge;
nodes[from].push_back(temp);
temp.from = to;
temp.to = from;
nodes[to].push_back(temp);
}
longest = 0;
int distance = 0;
visited[k] = true;
DFS(k, distance);
cout << longest << endl;
nodes.clear();
}
return 0;
}
当然也可以设置一个标志位deeper,表示是否已经遍历到了最后一个点(否则就会形成环)具体代码如下
代码:
#include <iostream>
#include <stdio.h>
#include <map>
#include <vector>
#include <cstring>
using namespace std;
class Node {
public:
int from;
int edge;
int to;
};
int longest;
map<int, vector<Node> > nodes;
bool visited[10001];
void DFS(int k, int distance) {
bool deeper = false;
for (int i = 0; i < nodes[k].size(); i++) {
if (visited[nodes[k].at(i).to] == false) {
visited[nodes[k].at(i).to] = true;
deeper = true;
DFS(nodes[k].at(i).to, distance + nodes[k].at(i).edge);
}
}
if (!deeper && distance > longest) {
longest = distance;
}
}
int main() {
int n, k;
while(cin >> n >> k){
memset(visited, false, sizeof(visited));
for (int i = 0; i < n - 1; i ++){
int from, to, edge;
cin >> from >> to >> edge;
Node temp;
temp.from = from;
temp.to = to;
temp.edge = edge;
nodes[from].push_back(temp);
temp.from = to;
temp.to = from;
nodes[to].push_back(temp);
}
longest = 0;
int distance = 0;
visited[k] = true;
DFS(k, distance);
cout << longest << endl;
nodes.clear();
}
return 0;
}