Description

There are N cities and N-1 roads in Magic-Island. You can go from one city to any other. One road only connects two cities. One day, The king of magic-island want to visit the island from the capital. No road is visited twice. Do you know the longest distance the king can go.

Input

There are several test cases in the input
A test case starts with two numbers N and K. (1<=N<=10000, 1<=K<=N). The cities is denoted from 1 to N. K is the capital.

The next N-1 lines each contain three numbers XYD, meaning that there is a road between city-X and city-Y and the distance of the road is D. D is a positive integer which is not bigger than 1000.
Input will be ended by the end of file.

Output

One number per line for each test case, the longest distance the king can go.
Sample Input
3 1
1 2 10
1 3 20

Sample Output

20

城市i对应的连接的路存到vector类型的cities[i]中;

每一条路有对应的id;

用visited[id]来记录是否已经走过这条路;

以下是代码:

#include <iostream>
#include <vector>
using namespace std; struct road{
int id; // every road has a unique id
int end; // connect to which city
int length; // the length of this road
road(int i, int e, int l) {
id = i, end = e, length = l;
}
}; #define MAX 10001
bool visited[MAX]; // when the road i is visited, visited[i] = true
vector<road> cities[MAX]; // cities[i] stores all roads connecting the city i
int maxLength; void dfs(int k, int total = ) {
for (int i = ; i < cities[k].size(); i++) {
// when the road has not visited
if (!visited[cities[k][i].id]) {
// visit the road
visited[cities[k][i].id] = true;
total += cities[k][i].length;
if (total > maxLength) maxLength = total;
// visit all roads connecting the city 'cities[k][i].end'
dfs(cities[k][i].end, total);
// unvisit the road
visited[cities[k][i].id] = false;
total -= cities[k][i].length;
}
}
} int main() {
int n, k;
while (cin>>n>>k) {
for (int i = ; i <= n; i++) { // initial
visited[i] = false;
cities[i].clear();
}
for (int i = ; i < n; i++) {
int x, y, l;
cin>>x>>y>>l;
cities[x].push_back(road(i, y, l));
cities[y].push_back(road(i, x, l));
}
maxLength = ;
dfs(k);
cout<<maxLength<<endl;
}
return ;
}

sicily1024 Magic Island(图的遍历)的更多相关文章

  1. C数据结构(文件操作,随机数,排序,栈和队列,图和遍历,最小生成树,最短路径)程序例子

    文件操作 文件打开方式               意义     ”r” 只读打开一个文本文件,只允许读数据     ”w” 只写打开或建立一个文本文件,只允许写数据     ”a” 追加打开一个文本 ...

  2. 图的遍历(搜索)算法(深度优先算法DFS和广度优先算法BFS)

    图的遍历的定义: 从图的某个顶点出发访问遍图中所有顶点,且每个顶点仅被访问一次.(连通图与非连通图) 深度优先遍历(DFS): 1.访问指定的起始顶点: 2.若当前访问的顶点的邻接顶点有未被访问的,则 ...

  3. C++编程练习(9)----“图的存储结构以及图的遍历“(邻接矩阵、深度优先遍历、广度优先遍历)

    图的存储结构 1)邻接矩阵 用两个数组来表示图,一个一维数组存储图中顶点信息,一个二维数组(邻接矩阵)存储图中边或弧的信息. 2)邻接表 3)十字链表 4)邻接多重表 5)边集数组 本文只用代码实现用 ...

  4. Kruskal和prime算法的类实现,图的遍历BFS算法。

    一.图的遍历 #include<iostream> #include<queue> #include<vector> using namespace std; in ...

  5. 图的遍历——DFS(矩形空间)

    首先,这里的图不是指的我们一般所说的图结构,而是大小为M*N的矩形区域(也可以看成是一个矩阵).而关于矩形区域的遍历问题经常出现,如“寻找矩阵中的路径”.“找到矩形区域的某个特殊点”等等之类的题目,在 ...

  6. 图的遍历——DFS和BFS模板(一般的图)

    关于图的遍历,通常有深度优先搜索(DFS)和广度优先搜索(BFS),本文结合一般的图结构(邻接矩阵和邻接表),给出两种遍历算法的模板 1.深度优先搜索(DFS) #include<iostrea ...

  7. 图的遍历算法:DFS、BFS

    在图的基本算法中,最初需要接触的就是图的遍历算法,根据访问节点的顺序,可分为深度优先搜索(DFS)和广度优先搜索(BFS). DFS(深度优先搜索)算法 Depth-First-Search 深度优先 ...

  8. 15 图-图的遍历-基于邻接矩阵实现的BFS与DFS算法

    算法分析和具体步骤解说直接写在代码注释上了 TvT 没时间了等下还要去洗衣服 就先不赘述了 有不明白的欢迎留言交流!(估计是没人看的了) 直接上代码: #include<stdio.h> ...

  9. python 回溯法 子集树模板 系列 —— 8、图的遍历

    问题 一个图: A --> B A --> C B --> C B --> D B --> E C --> A C --> D D --> C E -- ...

随机推荐

  1. JS验证字符长度

    function getStrLength(str) { var cArr = str.match(/[^\x00-\xff]/ig); return str.length + (cArr == nu ...

  2. inotify resources exhausted

    inotify resources exhausted tail -f /var/log/kubelet.log tail: inotify resources exhausted tail: ino ...

  3. postgresql+slony-i安装配置主从

    slon软件下载地址:slony1-1.2.6 http://slony.info/downloads/1.2/source/ postgresql下载地址: http://www.postgresq ...

  4. C# 抓取网站数据

    项目主管说这是项目中的一个亮点(无语...), 类似于爬虫一类的东西,模拟登陆后台系统,获取需要的数据.然后就开始研究这个. 之前有一些数据抓取的经验,抓取流程无非:设置参数->服务端发送请求- ...

  5. HBase安装及简单使用

    通过之前的hadoop0.20.2的安装并调试成功,接下来我们继续安装hbase0.90.5.在安装hbase0.90.5之前,因为hbase0.90.5只支持jdk1.6,所以,我把之前的jdk1. ...

  6. Oracle统计函数之Lead

    一,Lead 语法及例子 Lead函数是十分的好用的一个函数.它的语法如下图: 简单地说,lead是个奇特函数,在允许不使用自连接的情况下,一次返回多行. 参数说明: value_expr 值表达式, ...

  7. orale 函数大全[转]

    oracle函数大全 http://wenku.baidu.com/link?url=bXaGsnn8iN264GB8ec48IUPg5eRGDKAyAiSw0OBKL1I0mBVG549-2u9HT ...

  8. UITextField使用详解

    转iOS中UITextField使用详解 (1) //初始化textfield并设置位置及大小   UITextField *text = [[UITextField alloc]initWithFr ...

  9. GridView基础知识

    首先,gridview是封装好的,直接在设计界面使用,基本不需要写代码: 1.绑定数据源 GridView最好与LinQDatasourse配合使用,相匹配绑定数据: 2.外观控制 整体控制 自动选择 ...

  10. win7下Arduino Mega 2560驱动安装失败解决办法

    因为玩四轴用的apm的飞控板,而其需要安装此驱动,曾经在win8使用其,但是因为win8有相对应的数字证书保护措施(应该是这样的,因为好久了记不清楚了),以至于我每次都需要长按shift重启电脑关闭此 ...