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. github中cesium-terrain-builder和cesium-terrain-server使用

    cesium-terrain-builder的使用: 这个是用来把含有高程数据的tif图片切片成.terrain的小文件,是给cesium-terrain-server提供服务的. cesium-te ...

  2. ASP.NET MVC3 Model验证总结

    ASP.NET MVC3中的Model是自验证的,这是通过.NET4的System.ComponentModel.DataAnnotations命名空间完成的. 我们要做的只是给Model类的各属性加 ...

  3. div的一些易出错地方

    1.div中放一张图片老是显示不出来? 解决方法如下: 设置一下div的宽度与高度,然而此时直接写width与height是不对的,对于块级元素没有这个属性,只能在style="width: ...

  4. 多个插件依赖不同版本jQuery问题解决案例

     <script src="../../../js/jquery-1.3.2.min.js" type="text/javascript">< ...

  5. 微信小程序0.11.122100版本新功能解析

    微信小程序0.11.122100版本新功能解析   新版本就不再吐槽了,整的自己跟个愤青似的.人老了,喷不动了,把机会留给年轻人吧.下午随着新版本开放,微信居然破天荒的开放了开发者论坛.我很是担心官方 ...

  6. openfire+strophe

    其实是关于strophe的使用的,因为openfire那部分我并没有安装,项目过程中是直接访问的已经部署好的服务器了. 关于使用strophe+ openfire完成 即时通讯,做到及时的信息交互,主 ...

  7. Mysql命令集

    mysql远程授权GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123.com' WITH GRANT OPTION;flush p ...

  8. Java使用Scanner接收中文并输出时出现乱码

    Java中使用Scanner接收输入的中文并输出时会出现乱码现象,怎么解决此问题呢? 1.方法一 在声明Scanner时添加对应的编码格式就可以了,如下所示: Scanner sc = new Sca ...

  9. uglifyjs2压缩混淆js文件

    uglifyjs可以用来压缩混淆js文件,发布release版本应用利器.在StackOverflow浏览了一下,相比Google Closure和YUI compressor,uglifyjs被推荐 ...

  10. yii打印sql

    想打印Sql的话,可以用把你要执行的命令例如queryAll(),queryOne(),execute()换成getRawSql(); 例如 : 要看$result = Yii::$app->d ...