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 ...
随机推荐
- 四大类NoSQL数据库
http://blog.sina.com.cn/s/blog_636415010101945l.html 原文:http://blog.monitis.com/index.php/2011/05/22 ...
- emoji图像转码解码 存入数据库
public String emojiConvert1(String str) throws UnsupportedEncodingException { String patternString = ...
- 抓取网页中数据 -----51book中城市码
================== 获取网页中span标签里面的t_id的值 public function getpreg(){ $www = 'http://monkey.test.tripb. ...
- 转delphi中nil的用法
转自:http://blog.csdn.net/haiou327/article/details/6666124 delphi中nil的用法 和C++中的NULL一样的意思,指空值,它和0值不一样-- ...
- Python库 - import matplotlib.pyplot as plt 报错问题
为了避免各种问题,请使用最新的2.7.13安装文件 1.先设置好环境变量 在path变量中设置好以下路径: C:\Python27\Scripts C:\Python27 2.大部分报错问题 ...
- 杭电1002 Etaoin Shrdlu
Problem Description The relative frequency of characters in natural language texts is very important ...
- 实验六 多线程编程 1.随便选择两个城市作为预选旅游目标。实现两个独立的线程分别显示10次城市名,每次显示后休眠一段随机时间(1000ms以内),哪个先显示完毕,就决定去哪个城市。分别用Runnable接口和Thread类实现。
//继承Thread类 package zuoye; //继承Thread类 public class City extends Thread{ private String name; public ...
- java 判断大小写、数字出现的次数
//定义一个字符串 String s = "Hello123World"; //定义三个统计变量 int bigCount = 0; int smallCount = 0; int ...
- Entity Framework Tools install to VS 2015
因为在VS2013,2015里不再支持Sql compact 数据库的显示, 但是我们可以通过安装EF tools扩展来支持,参考地址:http://thedatafarm.com/data-acce ...
- UVA 12083 POJ 2771 Guardian of Decency
/* http://acm.hust.edu.cn/vjudge/contest/view.action?cid=71805#problem/C */ 性质: [1]二分图最大点独立数=顶点数-二分图 ...