sicily 1024 Magic Island 原

Ciel

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 XYD, 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#: 1024
// Submission#: 1788079
// All Copyright reserved by Informatic Lab of Sun Yat-sen University
#include <iostream>
#include <vector>
#include <cstring>
using namespace std;

#define MAX 10010

struct node{
int e,d;
node( int a, int b ){
e = a; d = b;
}
};

vector<node> buffer[MAX];
bool visit[MAX];
int re;

void dfs( int v, int len ){
if( len>re ) re = len;
visit[v] = true;
for( int i=0 ; i<buffer[v].size() ; i++ ){
node temp = buffer[v][i];
if( !visit[temp.e] ){
visit[temp.e] = true;
len += temp.d;
dfs(temp.e,len);
visit[temp.e] = false;
len -= temp.d;
}
}
}

int main(){
int n,k;
int x,y,z;
while( cin>>n ){
cin >> k;
for( int i=0 ; i<n-1 ; i++ ){
cin >> x >> y >> z;
buffer[x].push_back(node(y,z));
buffer[y].push_back(node(x,z));
}
memset(visit,0,sizeof(visit));
re = 0;
dfs(k,0);
cout << re << endl;
for( int i=1 ; i<=n ; i++ )
buffer[i].clear();
}
return 0;
}``````

Ciel

--============Oracle ADG搭建============== --==========准备阶段========= 1.检查primary为archivelog模式。 select log_mode from v\$database; 如果为noarchivelog模式，切换到archivelo......

UltraSQL
2018/07/23
0
0

AlexTuan
2016/02/21
68
0

nothingfinal
2012/02/22
0
0
FileChannel.transferTo for large file in windows

Using Java NIO use can copy file faster. I found two kind of method mainly over internet to do this job. public static void copyFile(File sourceFile, File destinationFile) throw......

pczhangtl
2014/03/30
204
0
[Leetcode] Max Area of Island 最大岛屿面积

Max Area of Island 最新更新请见：https://yanjia.me/zh/2019/02/... Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing land) connected 4-di......

ethannnli
09/29
0
0

5
0

6
0
JS其他类型值转化为Boolean类型规则

6
0
EurekaClient自动装配及启动流程解析

Java学习录

10
0

p517 在C++中，基类指针可以指向一个派生类的对象。如果基类的析构函数不是虚函数，当需要delete这个指向派生类的基类指针时，就只会调用基类的析构函数，而派生类的析构函数无法被调用。容易...

6
0