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.
题意:
给的我们一些带权值的边,判定树的最大直径。
感悟:
之前定义值,都是采用宏定义#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可以在任何地方定义常量,编译器对它的处理过程与变量相似,只是分配内存的地方不同
*********************************************************************************************************************************************************************