lightoj1094 - Farthest Nodes in a Tree

1094 - Farthest Nodes in a Tree
Time Limit: 2 second(s)Memory Limit: 32 MB

Given a tree (a connected graph with no cycles), you have to find the farthest nodes in the tree. The edges of the tree are weighted and undirected. That means you have to find two nodes in the tree whose distance is maximum amongst all nodes.

Input

Input starts with an integer T (≤ 10), denoting the number of test cases.

Each case starts with an integer n (2 ≤ n ≤ 30000) denoting the total number of nodes in the tree. The nodes are numbered from 0 to n-1. Each of the next n-1 lines will contain three integers u v w (0 ≤ u, v < n, u ≠ v, 1 ≤ w ≤ 10000) denoting that node u and v are connected by an edge whose weight is w. You can assume that the input will form a valid tree.

Output

For each case, print the case number and the maximum distance.

Sample Input

Output for Sample Input

2

4

0 1 20

1 2 30

2 3 50

5

0 2 20

2 1 10

0 3 29

0 4 50

Case 1: 100

Case 2: 80

Notes

Dataset is huge, use faster i/o methods.


PROBLEM SETTER: JANE ALAM JAN
 

题意:给定若干两点间的距离,求两点的距离的最大距离。即?树的直径,据说找到离任意一点最远的点index,然后找离index最远的点就是最大距离?

假定0为根节点找离0最远的点就是,最深的点index,然后找离index最远的点就是树上最远的距离

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 
 5 using namespace std;
 6 
 7 #define N 30008
 8 
 9 struct node
10 {
11     int v, w, next;
12 }e[N*4];
13 
14 int n, cnt, maxx;
15 int Index;   // 写成index过不了lightoj=·=||
16 int head[N], dist[N];
17 
18 void addedge(int u, int v, int w)
19 {
20     e[cnt].v = v;
21     e[cnt].w = w;
22     e[cnt].next = head[u];
23     head[u] = cnt++;
24 }
25 
26 void dfs(int u, int w)
27 {
28     dist[u] = w;
29     if(w > maxx)
30     {
31         maxx = dist[u];
32         Index = u;
33     }
34     for(int i = head[u]; i != -1; i = e[i].next)
35     {
36         if(dist[e[i].v] == -1)
37         {
38             dfs(e[i].v, dist[u]+e[i].w);
39         }
40     }
41 }
42 int main()
43 {
44     int t, u, v, w, k = 1;
45 
46     scanf("%d", &t);
47 
48     while(t--)
49     {
50         cnt = maxx = 0;
51         memset(head, -1, sizeof(head));
52 
53         scanf("%d", &n);
54         n--;
55         while(n--)
56         {
57             scanf("%d%d%d", &u, &v, &w);
58             addedge(u, v, w);
59             addedge(v, u, w);
60         }
61         memset(dist, -1, sizeof(dist));
62         dfs(0, 0);
63         memset(dist, -1, sizeof(dist));
64         dfs(Index, 0);
65         printf("Case %d: %d\n", k++, maxx);
66     }
67     return 0;
68 }

bfs

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 #include <queue>
 5 
 6 using namespace std;
 7 
 8 #define N 30008
 9 
10 struct node
11 {
12     int v, w, next;
13 } e[N*2];
14 
15 int n, cnt, maxx;
16 int Index;
17 int head[N], dist[N], vis[N];
18 
19 void addedge(int u, int v, int w)
20 {
21     e[cnt].v = v;
22     e[cnt].w = w;
23     e[cnt].next = head[u];
24     head[u] = cnt++;
25 }
26 
27 void bfs(int u)
28 {
29     memset(vis, 0, sizeof(vis));
30     queue<int> Q;
31     Q.push(u);
32     vis[u] = 1;
33     dist[u] = 0;
34 
35     while(Q.size())
36     {
37         u = Q.front();
38         Q.pop();
39 
40         for(int i = head[u]; i != -1; i = e[i].next)
41         {
42             int v = e[i].v;
43             if(!vis[v])
44             {
45                 vis[v] = 1;
46                 dist[v] = dist[u]+e[i].w;
47                 if(dist[v] > maxx)
48                 {
49                     maxx = dist[v];
50                     Index = v;
51 
52                 }
53                 Q.push(v);
54             }
55         }
56     }
57 }
58 
59 int main()
60 {
61     int t, u, v, w, k = 1;
62     scanf("%d", &t);
63 
64     while(t--)
65     {
66         maxx = cnt = 0;
67         memset(head, -1,sizeof(head));
68 
69         scanf("%d", &n);
70         n--;
71 
72         while(n--)
73         {
74             scanf("%d%d%d", &u, &v, &w);
75             addedge(u, v, w);
76             addedge(v, u, w);
77         }
78         memset(dist, 0, sizeof(dist));
79         bfs(0);
80         bfs(Index);
81         printf("Case %d: %d\n", k++, maxx);
82     }
83     return 0;
84 }

 

转载于:https://www.cnblogs.com/Tinamei/p/4739674.html

主要内容:本文详细介绍了一种QRBiLSTM(分位数回归双向长短期记忆网络)的时间序列区间预测方法。首先介绍了项目背景以及模型的优势,比如能够有效利用双向的信息,并对未来的趋势上限和下限做出估计。接着从数据生成出发讲述了具体的代码操作过程:数据预处理,搭建模型,进行训练,并最终可视化预测结果与计算分位数回归的边界线。提供的示例代码可以完全运行并且包含了数据生成环节,便于新手快速上手,深入学习。此外还指出了模型未来发展的方向,例如加入额外的输入特性和改善超参数配置等途径提高模型的表现。文中强调了时间序列的标准化和平稳检验,在样本划分阶段需要按时间序列顺序进行划分,并在训练阶段采取合适的手段预防过度拟合发生。 适合人群:对于希望学习和应用双向长短时记忆网络解决时序数据预测的初学者和具有一定基础的研究人员。尤其适用于有金融数据分析需求、需要做多一步或多步预测任务的从业者。 使用场景及目标:应用于金融市场波动预报、天气状况变化预测或是物流管理等多个领域内的决策支持。主要目的在于不仅能够提供精确的数值预计还能描绘出相应的区间概率图以增强结论置信程度。 补充说明:本教程通过一个由正弦信号加白噪构造而成的简单实例来指导大家理解和执行QRBiLSTM流程的所有关键步骤,这既方便于初学者跟踪学习,又有利于专业人士作为现有系统的补充参考工具。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值