博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
lightoj1094 - Farthest Nodes in a Tree
阅读量:7013 次
发布时间:2019-06-28

本文共 4149 字,大约阅读时间需要 13 分钟。

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 
2 #include
3 #include
4 5 using namespace std; 6 7 #define N 30008 8 9 struct node10 {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 
2 #include
3 #include
4 #include
5 6 using namespace std; 7 8 #define N 30008 9 10 struct node11 {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
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

你可能感兴趣的文章
《中国人工智能学会通讯》——11.30 深度迁移学习
查看>>
Dell EMC扩充数据保护产品线 Data Domain增强云分层功能
查看>>
美柚社区精选:贴心宝妈的八大育儿经验
查看>>
走进医疗明星企业之北京天坛普华医院
查看>>
一点资讯电影贴片广告以假乱真
查看>>
曙光出炉“数据中国加速计划”
查看>>
中国制造2025新机遇 机器视觉行业爆发
查看>>
中国工商银行阿根廷分行用数据运营展现本地特色
查看>>
使用闪存存储的优势与注意事项
查看>>
网络钓鱼防不胜防:大型科技公司竟被骗逾1亿美元
查看>>
网络间谍活动月光迷宫已演变成Turla
查看>>
欧洲运营商展开5GTango项目 应对特定行业市场
查看>>
Windows 10创作者更新将改进蓝牙功能
查看>>
睿联嘉业边缘融合大屏幕多媒体会议系统方案
查看>>
凯立德货车专用导航 应“运”而生
查看>>
聊天机器人真正的潜力,潜藏在个人金融领域
查看>>
英特尔或推可超频Kaby Lake酷睿i3处理器: 重拾赛扬300A荣光?
查看>>
要想在未来立足 微软等软件公司就必须折本研发硬件
查看>>
QTP使用中的陷阱
查看>>
Cirrus Delaware公司数据中心计划因建设电厂再次受阻
查看>>