大臣的旅费2

package 搜索;


import java.io.BufferedInputStream;
import java.io.IOException;
import java.util.Arrays;


public class 大臣的旅费2 {
static BufferedInputStream bs = new BufferedInputStream(System.in);

static final int INFINITY = 999999;


static int readInt() {
int num = 0;
int i = 0;
try {
while (((i = bs.read()) & 48) != 48 || i > 57)
;
while ((i & 56) == 48 || (i & 62) == 56) {
num = num * 10 + (i & 15);
i = bs.read();
}
} catch (IOException e) {
}
return num;
}


static class Edge {
public int to;
public int next;
public int distance;
}


static Edge[] edge;
static int[] head;
static int top;
static boolean[] flag;
static int maxDi;
static int maxIndex;


public static void main(String[] args) {
int n = readInt();
init(n);
for (int i = 0; i < n - 1; i++) {
int a = readInt();
int b = readInt();
int c = readInt();
addEdge(a, b, c);
addEdge(b, a, c);
}
dfs(1, 0);
dfs(maxIndex, 0);
System.out.println(maxDi * (21 + maxDi) / 2);
}


private static void dfs(int start, int distance) {
if (distance > maxDi) {
maxDi = distance;
maxIndex = start;
}
flag[start] = true;
for (int i = head[start]; i != -1; i = edge[i].next) {
int to = edge[i].to;
int dis = edge[i].distance;
if (flag[to])
continue;
dfs(to, distance + dis);
}
flag[start] = false;


}


private static void init(int n) {
flag = new boolean[n + 1];
edge = new Edge[(n - 1) * 2];
head = new int[(n - 1) * 2];
top = 0;
Arrays.fill(head, -1);
}


private static void addEdge(int a, int b, int c) {
edge[top] = new Edge();
edge[top].to = b;
edge[top].next = head[a];
edge[top].distance = c;
head[a] = top++;


}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值