input

u1 v1 w1

u2 v2 w2

...

un vn wn  1<=vi,ui<=n+1

/n

output

距离最远的两个点的距离

做法:一颗全连通且只有一条路从一个顶点到达另一个顶点,直接深搜,返回时返回最远的支路,且最远的支路加上第二远的支路和总路途最远比较,更新总路途最大,因为以一个点为中心走很多条路,最远的肯定是最大两条路的和,做法类似dp

  输入有点坑,输完最后一组数据直接EOF,处理输入搞了好久,gets返回的是指针,遇到EOF返回NULL,遇空白行字符串第一个字符为NULL结束符,返回指针不为NULL

 #include <iostream>
#include <stdio.h>
#include <vector>
#include <algorithm>
#include<cstring>
#define MAX 10010 using namespace std; struct node
{
vector<int>next,dis;
}; node tree[MAX];
int vis[MAX],maxd; int dfs(int x)
{
int max1=,max2=;
vector <int>dis;
vector<int>::iterator i,j,k;
for(i=tree[x].next.begin(),j=tree[x].dis.begin();i!=tree[x].next.end();i++,j++)
if(!vis[*i])
{
vis[*i]=;
dis.push_back(dfs(*i)+*j);
}
if(dis.empty()) return ;
for(k=i=dis.begin();i!=dis.end();i++)
if(max1<*i)
{
max1=*i;
k=i;
}
for(j=dis.begin();j!=dis.end();j++)
if(max2<*j&&j!=k) max2=*j;
maxd=max(maxd,max1+max2);//最远的和第二远的相加更新最远
//printf("maxd=%d max1=%d max2=%d\n",maxd,max1,max2);
return max1;//返回最远的
} void init()
{
memset(vis,,sizeof(vis));
for(int i=;i<MAX;i++)
{
tree[i].next.clear();
tree[i].dis.clear();
}
maxd=-;
}
int main()
{
//freopen("/home/user/桌面/in","r",stdin);
int a,b,c;
char s[];
init();
while()
{
char*p=gets(s);
//printf("p=%p\n",p);
if(s[]&&p)
{
//printf("s[0]=%d\n",s[0]);
//printf("1s[0]=%d\n",s[0]);
sscanf(s,"%d%d%d",&a,&b,&c);
tree[a].next.push_back(b);
tree[a].dis.push_back(c);
tree[b].next.push_back(a);
tree[b].dis.push_back(c);
}
else
{
/*printf("cal:s[0]=%d\nmaxd=%d\nvis[1]=%d\n",s[0],maxd,vis[1]);
for(int i=1;i<=6;i++)
for(int j=0;tree[i].next.begin()+j!=tree[i].next.end();j++)
printf("%d:%d %d\n",i,tree[i].next[j],tree[i].dis[j]);*/
//printf("s[0]=%d\n",s[0]);
vis[]=;
maxd=max(maxd,dfs());
printf("%d\n",maxd);
init();
if(p==NULL) break;
}
}
return ;
}

UVA 10308 Roads in the North的更多相关文章

  1. poj 2631 Roads in the North

    题目连接 http://poj.org/problem?id=2631 Roads in the North Description Building and maintaining roads am ...

  2. poj 2631 Roads in the North【树的直径裸题】

    Roads in the North Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2359   Accepted: 115 ...

  3. POJ 2631 Roads in the North(树的直径)

    POJ 2631 Roads in the North(树的直径) http://poj.org/problem? id=2631 题意: 有一个树结构, 给你树的全部边(u,v,cost), 表示u ...

  4. Roads in the North POJ - 2631

    Roads in the North POJ - 2631 Building and maintaining roads among communities in the far North is a ...

  5. poj 2631 Roads in the North (自由树的直径)

    Roads in the North Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4513   Accepted: 215 ...

  6. Roads in the North(POJ 2631 DFS)

    Description Building and maintaining roads among communities in the far North is an expensive busine ...

  7. POJ 2631 Roads in the North(求树的直径,两次遍历 or 树DP)

    题目链接:http://poj.org/problem?id=2631 Description Building and maintaining roads among communities in ...

  8. 题解报告:poj 2631 Roads in the North(最长链)

    Description Building and maintaining roads among communities in the far North is an expensive busine ...

  9. POJ 2631 Roads in the North (求树的直径)

    Description Building and maintaining roads among communities in the far North is an expensive busine ...

随机推荐

  1. jQuery技巧大放送【转】

    1.关于页面元素的引用 通过jquery的$()引用元素包括通过id.class.元素名以及元素的层级关系及dom或者xpath条件等方法,且返回的对象为jquery对象(集合对象),不能直接调用do ...

  2. art中的部分内容,留着慢慢研究

    root@hbg:/tmp# cat /proc/mtddev:    size   erasesize  namemtd0: 00040000 00010000 "u-boot" ...

  3. #if defined和#if !defined(c语言的宏定义)

    我们要检查a是否定义 #if defined a #undef a #define a 200 #endif 上述语句检验a是否被定义,如果被定义,则用#undef语句解除定义,并重新定义a为200 ...

  4. 使用某些Widows API时,明明包含了该头文件,却报错“error C2065: undeclared identifier”

    在使用一些新版本的API,或者控件的新特性(比如新版的ComCtl32.dll)的时候,你可能会得到“error C2065: undeclared identifier.“这个错误.原因是这些功能是 ...

  5. dell 去鼠标版功能widnows

    桌面计算机(点击右键)----管理----设备管理器-----鼠标------选择触摸板(ps/2 兼容鼠标)---右击------跟新驱动-------浏览计算机查找------从计算机列表中选择- ...

  6. The type java.util.Map$Entry cannot be resolved. It is indirectly referenced from required .class files

    JDK版本的问题. 解决方法: 原来jdk1.8不向下兼容,用回1.6的就可以了. 下图有三个jdk,前两个自己装的,第三个MyEclipse自带的.

  7. 2.4 chrome 开发者工具

    开发者工具顶部有Elements.Console.Network等八个栏目.常用的有三个:Elements,用来查看需爬取字段的HTML标签信息:Console,可以检测你的JS代码:Network, ...

  8. 使用cocoapods的两个大坑的修改方法

    1.报错内容: [!] The dependency `ReactiveCocoa (= 2.1.8)` is not used in any concrete target. The depende ...

  9. unknow type name 'off64_t'

    或者  错误"error: 'off64_t' does not name a type" MinGW的bug,使用-std=c++11, 有可能出现, 修改{MinGW dir} ...

  10. hdu_5748_Bellovin(LIS)

    题目链接:hdu_5748_Bellovin 题意: 给你一个数列ai,设f(a1,a2,a3,..an)=(f1,f2,f3,...,fn),其中fi表示以ai结尾的最长递增子序列长度,注意:必须要 ...