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. 用SQLMAP工具进行SQL注入

    1.检查注入点 [注入点需要自己寻找,可以利用一些工具,例如:Acunetix Web Vulnerability scanner (WVS),AppScan等]u表示URL. sqlmap   -  ...

  2. [z]Java开发必会的Linux命令

    1.查找文件 find / -name filename.txt 根据名称查找/目录下的filename.txt文件. find . -name "*.xml" 递归查找所有的xm ...

  3. 使用spring-data-solr做solr客户端

    solr的客户端基本上只有一个,那就是solrj,spring-data-solr是在solrj的基础上做的封装,使统一成spring-data的风格 官方网站: http://projects.sp ...

  4. Centos7.0安装配置PHP7.0

    YUM安装所需开发包 yum install wget make gcc gcc-c++ bison autoconf patch \ pcre-devel zlib-devel openssl-de ...

  5. Android SDK更新以及ADT更新出现问题的解决办法

    http://jingyan.baidu.com/article/148a192196209d4d70c3b168.html

  6. How to build the Robotics Library from source code on Windows

    The Robotics Library is an open source C++ library for robot kinematics, motion planning and control ...

  7. Keepalived 配置实例

    Keepalived 是一款轻量级HA集群应用,它的设计初衷是为了做LVS集群的HA,即探测LVS健康情况,从而进行主备切换,不仅如此,还能够探测LVS代理的后端主机的健康状况,动态修改LVS转发规则 ...

  8. Ceph剖析:线程池实现

    线程池ThreadPool的实现符合生产者-消费者模型,这个模型解除生产者消费者间的耦合关系,生产者可以专注处理制造产品的逻辑而不用关心产品的消费,消费者亦然.当然,生产者消费者之间需要一个连接的纽带 ...

  9. aa3

    var geoCoordMap = { "海门":[121.15,31.89], "鄂尔多斯":[109.781327,39.608266], "招远 ...

  10. C# 读取Excel文件里面的内容到DataSet

    摘要:读取Excel文件里面的内容到DataSet 代码: /// <summary> /// 读取Excel文件里面的内容到DataSet /// </summary> // ...