UVA 10308 Roads in the North
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的更多相关文章
- poj 2631 Roads in the North
题目连接 http://poj.org/problem?id=2631 Roads in the North Description Building and maintaining roads am ...
- poj 2631 Roads in the North【树的直径裸题】
Roads in the North Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 2359 Accepted: 115 ...
- POJ 2631 Roads in the North(树的直径)
POJ 2631 Roads in the North(树的直径) http://poj.org/problem? id=2631 题意: 有一个树结构, 给你树的全部边(u,v,cost), 表示u ...
- Roads in the North POJ - 2631
Roads in the North POJ - 2631 Building and maintaining roads among communities in the far North is a ...
- poj 2631 Roads in the North (自由树的直径)
Roads in the North Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4513 Accepted: 215 ...
- Roads in the North(POJ 2631 DFS)
Description Building and maintaining roads among communities in the far North is an expensive busine ...
- POJ 2631 Roads in the North(求树的直径,两次遍历 or 树DP)
题目链接:http://poj.org/problem?id=2631 Description Building and maintaining roads among communities in ...
- 题解报告:poj 2631 Roads in the North(最长链)
Description Building and maintaining roads among communities in the far North is an expensive busine ...
- POJ 2631 Roads in the North (求树的直径)
Description Building and maintaining roads among communities in the far North is an expensive busine ...
随机推荐
- 【LeetCode】459. Repeated Substring Pattern
Given a non-empty string check if it can be constructed by taking a substring of it and appending mu ...
- input美化上传按钮美化
今天工作需求碰到 样式改变上传按钮 效果: <a href="javascript:;" class="a-upload"> <input t ...
- hdu_5858_Hard problem(数学题)
题目链接:hdu_5858_Hard problem 题意: 让你求阴影部分面积 题解: 推推公式就行 #include<stdio.h> #include<math.h> # ...
- linux 查看日志
第一步 :提交自己目录文件(先到自己目录下载最新文件-->合并-->提交到临时目录temp-->在提交到master总目录-->其他关联master远 程分支的目录.就可以pu ...
- 请问如何查询一个APP的Android和iOS下载量?
作者:Jasmine Jiang链接:http://www.zhihu.com/question/28533067/answer/87871598来源:知乎著作权归作者所有,转载请联系作者获得授权. ...
- 从MySQL全库备份中恢复某个库和某张表【转】
从MySQL全库备份中恢复某个库和某张表 一.全库备份-A [root@mha2 backup]#mysqldump -uroot -p123456 --default-character-set=u ...
- android 编译的原理介绍
http://blog.csdn.net/mr_raptor/article/details/7540066
- html5 画个圈
<html><body><canvas id="myCanvas" width="200" height="100&qu ...
- postgreSqL的序列sequence
PostgreSQL uses sequences to generate values for serial columns and serial columns are generally wha ...
- iOS导航栏主题
主要是取得导航栏的appearance对象,操作它就设置导航栏的主题 UINavigationBar *navBar = [UINavigationBar appearance]; 常用主题设置 导航 ...