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. MySQL安装,启动

    一.安装.配置环境变量 http://www.cnblogs.com/mr-wid/archive/2013/05/09/3068229.html 配置环境变量:把你的安装目录复制下,例如:G:\Do ...

  2. 面试题<初级>

    INTERVIEW .markdown-body ul pre code { background:red; font-size:40px; } @code-char:"```" ...

  3. 23种oop设计模式定义

    创建型模式 单例模式:确保一个类只有一个实例,而且自行实例化并向整个系统提供这个实现.   工厂模式:定义一个用于创建对象的接口,让子类决定将哪一个类实例化.工厂方法使一个类的实例化延迟到子类. 抽象 ...

  4. 6SQL SERVER视图/索引

    一.视图 1.视图概念 ①视图是包含由一张或多张表的列组成的数据集.该表中的记录是由一条查询语句执行后所得到的查询结果所构成的. ②视图是一张虚拟表,它表示一张表的部分数据或多张表的综合数 据,其结构 ...

  5. python 入门学习之环境搭载

    1.常用python 2.7 需要在我的电脑环境变量进行环境搭载 2.用notepad++进行编辑器适配,选择python语言 在输入运行程序名里面输入cmd /k x: & cd " ...

  6. C#时间戳转成php的time()

    DateTime timeStamp = new DateTime(1970,1,1);  //得到1970年的时间戳 long a = (DateTime.UtcNow.Ticks - timeSt ...

  7. angular js 自定义js错误处理(Angularjs js error handler)

    使用AngularJS的时候,对JS错误如何自定义处理?(比如用Google Analytics记录angularjs使用中出现的js错误) AngularJS自带一个错误处理service:$exc ...

  8. vi

    e! 放弃所有修改,从上次保存文件开始再编辑 shift+g 最后一行 gg 第一行 u 恢复上一次操作 如果查找下一个,按"n"即可. set nu 显示行号 编辑模式下111g ...

  9. 关于HTTP session随笔

    1).访问*.html的静态资源因为不会被编译为Servlet,也就不涉及session的问题. 2).当JSP页面没有显式禁止session的时候,在打开浏览器第一次请求该jsp的时候,服务器会自动 ...

  10. flex中下拉框的实现

    flex中下拉框的实现 <mx:ComboBox id = "combobox" dataProvider = "{deviceCodeType }" e ...