Light oj 1094 Farthest Nodes in a Tree(树的最大直径)
来源:互联网 发布:java获取泛型t的class 编辑:程序博客网 时间:2024/05/16 05:02
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.
题意:
给的我们一些带权值的边,判定树的最大直径。
感悟:
之前定义值,都是采用宏定义#define,今天在 light oj 总是出现 Runtime Error 可见以后还是改用 const 吧。
代码:
#include<cstdio>#include<cstring>#include<queue>#define MYDD 1103//#define MAXNUM 30000+16 light oj 这样定义是错误的,运行出错 using namespace std;const int MAXNUM=30000+16;int head[MAXNUM];//边的头" 指针 "struct EDGE {//存储边信息int from;//起始节点int to;//终止节点int value;//边的权值int next;//指向的下一条边} edge[MAXNUM*2];int edgenum;//边的总数void init() {//边的初始化memset(head,-1,sizeof(head));edgenum=0;}int n,ans;//边的数目,记录最后结果int Tnode;//记录最大直径的端点int dis[MAXNUM];//该节点结尾的最长路bool vis[MAXNUM];//标记节点是否被访问过void addedge(int u,int v,int w) {//增加边edge[edgenum].from=u;edge[edgenum].to=v;edge[edgenum].value=w;edge[edgenum].next=head[u];head[u]=edgenum++;}void BFS(int x) {memset(dis,0,sizeof(dis));memset(vis,false,sizeof(vis));queue<int> Q;Q.push(x);vis[x]=true;dis[x]=0;ans=0;while(!Q.empty()) {int temp=Q.front();//访问队列首元素Q.pop();for(int j=head[temp]; j!=-1; j=edge[j].next) {int v=edge[j].to;if(!vis[v]) {if(dis[v]<dis[temp]+edge[j].value)dis[v]=dis[temp]+edge[j].value;vis[v]=true;//标记已访问Q.push(v);}}}for(int j=0; j<n; j++) {if(ans<dis[j]) {ans=dis[j];Tnode=j;}}}int main() {int t;scanf("%d",&t);int Kcase=1;while(t--) {int u,v,w;scanf("%d",&n);init();for(int j=1; j<n; j++) {scanf("%d%d%d",&u,&v,&w);addedge(u,v,w);//存入边的信息addedge(v,u,w);//无向图}BFS(0);//第一次查找BFS(Tnode);//第二次查找printf("Case %d: %d\n",Kcase++,ans);}return 0;}
后:
const和define的区别,参考:新浪博客
define宏定义和const常变量区别:
1.define是宏定义,程序在预处理阶段将用define定义的内容进行了替换。因此程序运行时,常量表中并没有用define定义的常量,系统不为它分配内存。
const定义的常量,在程序运行时在常量表中,系统为它分配内存。
2.define定义的常量,预处理时只是直接进行了替换。所以编译时不能进行数据类型检验。
const定义的常量,在编译时进行严格的类型检验,可以避免出错。
3.define定义表达式时要注意“边缘效应”,例如如下定义:
原因在于在预处理阶段,编译器将
const定义表达式没有上述问题。
const定义的常量叫做常变量原因有二:
const定义常量像变量一样检查类型
const可以在任何地方定义常量,编译器对它的处理过程与变量相似,只是分配内存的地方不同
*********************************************************************************************************************************************************************
- Light oj 1094 Farthest Nodes in a Tree(树的最大直径)
- Light OJ:1094 Farthest Nodes in a Tree(树状DP+统计树的最大直径)
- light oj 1094 Farthest Nodes in a Tree(树的直径模板)
- Light OJ 1094 Farthest Nodes in a Tree (树的直径)
- 【Light-oj】-1094 - Farthest Nodes in a Tree(树的直径)
- light oj 1094 Farthest Nodes in a Tree(树的直径模板)
- Light OJ 1094 Farthest Nodes in a Tree(树的直径模板)
- Light OJ 1094 - Farthest Nodes in a Tree【树的直径 两次bfs】
- Light oj 1094 - Farthest Nodes in a Tree【树的直径】
- Light-oj-1094 Farthest Nodes in a Tree (树的直径模板题)
- 【Light OJ】1049 Farthest Nodes in a Tree(树的直径模板题)
- Light OJ 1049 Farthest Nodes in a Tree【树的直径】
- Light OJ 1049 Farthest Nodes in a Tree(树的直径)(模板题)
- LightOJ 1094 - Farthest Nodes in a Tree【树的直径】
- LightOJ - 1094 Farthest Nodes in a Tree(树的直径)
- LightOJ 1094 Farthest Nodes in a Tree 树的直径
- Farthest Nodes in a Tree(树的直径)
- Farthest Nodes in a Tree(树的直径)
- 窗口与子部件
- maven的java版本随jenv切换改变而改变
- 网页post和get方法中的中文乱码问题详解
- gdb 调试问题Error creating process XXXXXXXX(error 5).
- java synchronized详解
- Light oj 1094 Farthest Nodes in a Tree(树的最大直径)
- vs2010 LINK : fatal error LNK1123: 转换到 COFF 期间失败: 文件无效或损坏
- Tempter of the Bone
- linux下文件操作需要关注的函数
- nginx 配置多域名
- Codeforces Round #287 (Div. 2) C 构造
- java基础----day02:运算符
- cocoa touch——UIImage——create,init
- 腾讯优测优分享干货精选| Android双卡双待适配——隐藏在数据库中的那些秘密