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. linshi_temp_erweima_html_backup

    <!doctype html><html><head><meta charset="utf-8"><meta content= ...

  2. bzoj2052: Pku1777 Vivian

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2052 2052: Pku1777 Vivian Time Limit: 10 Sec  M ...

  3. 老oj1965:polygon半平面交

    题目链接:http://192.168.2.240:8080/JudgeOnline/showproblem?problem_id=1965 polygon半平面交 Time Limit:1000MS ...

  4. IIS7部署MVC站点后,打开无法正常跳转到首页

    产品拿到安装包后想在本地安装测试一下,但是管理工具里没有IIS. 后来在windows功能里添加iis服务. 添加后成功安装. 但是第一次打开时,页面提示要“启用目录浏览”. 启用后,打开的却是站点目 ...

  5. OpenGL4.x不支持gluPerspective函数。故备份之

    template <typename type> inline mat4<type> mat4<type>::perspectiveProjection(type ...

  6. UIButton的属性设置

    1.背景颜色 btn.backgroundColor = [UIColor redColor]; 2.给按钮添加文字并添加显示状态  [btn setTitle@"播放" forS ...

  7. rabbitmq——镜像队列

    转自:http://my.oschina.net/hncscwc/blog/186350?p=1 1. 镜像队列的设置 镜像队列的配置通过添加policy完成,policy添加的命令为: rabbit ...

  8. ajax的项目实操(只用于记录部分文件未引入)

    <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...

  9. ADO.NET 数据访问类查询、属性扩展

    今天,我首先在之前所做的人员管理系统的基础上,利用数据访问类进行了所有人员信息的查询. 主程序代码: List<Users> Ulist = new UsersData().Select( ...

  10. [css3动画]渐隐渐现

    测试 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8& ...