一. 题意:

有n个节点,n-1条边,并且任意两个节点都连通。模拟一下,实际上是一棵树的便利,求从特定根节点出发最长路径的值。这里用了广搜。

二. 每个节点只有两条邻接边,每个节点用一个vector来存储这些边。还有isVisited数组保证一条路径中一个节点只能经过一次。

三.

 //
// main.cpp
// sicily-1024
//
// Created by ashley on 14-10-13.
// Copyright (c) 2014年 ashley. All rights reserved.
// #include <iostream>
#include <vector>
using namespace std;
typedef struct
{
int left;
int right;
int weight;
}edge;
vector<edge> route;
vector<edge> adj[];
bool isVisited[];
void breadthSearch(int source, int &length, int pathLength)
{
isVisited[source] = true;
for (int i = ; i < (int)adj[source].size(); i++) {
if (isVisited[adj[source][i].right] == false || isVisited[adj[source][i].left] == false) {
if (pathLength + adj[source][i].weight > length) {
length = pathLength + adj[source][i].weight;
}
if (isVisited[adj[source][i].right] == false) {
breadthSearch(adj[source][i].right, length, pathLength + adj[source][i].weight);
}
if (isVisited[adj[source][i].left] == false) {
breadthSearch(adj[source][i].left, length, pathLength + adj[source][i].weight);
}
}
}
}
int main(int argc, const char * argv[])
{
int nodeNum, capital;
while (cin >> nodeNum >> capital) {
for (int i = ; i < ; i++) {
adj[i].clear();
isVisited[i] = false;
}
//memset(adj, 0, sizeof(adj));
//memset(isVisited, 0, sizeof(isVisited));
int l, r, w;
for (int i = ; i < nodeNum - ; i++) {
cin >> l >> r >> w;
adj[l].push_back(edge{l, r, w});
adj[r].push_back(edge{l, r, w});
}
int longest = ;
breadthSearch(capital, longest, );
cout << longest << endl;
}
return ;
}

源代码

Sicily-1024的更多相关文章

  1. sicily 1024 Magic Island

    题意:求无向图路径中的最大带权值. 解法:深搜 // Problem#: 9859 // Submission#: 2661875 // The source code is licensed und ...

  2. python3爬取1024图片

    这两年python特别火,火到博客园现在也是隔三差五的出现一些python的文章.各种开源软件.各种爬虫算法纷纷开路,作为互联网行业的IT狗自然看的我也是心痒痒,于是趁着这个雾霾横行的周末瞅了两眼,作 ...

  3. mysql Packet for query is too large (1185 > 1024)异常

    注:最近mysql一直提示如下错误 Packet for query is too large (1185 > 1024). You can change this value on the s ...

  4. sicily 中缀表达式转后缀表达式

    题目描述 将中缀表达式(infix expression)转换为后缀表达式(postfix expression).假设中缀表达式中的操作数均以单个英文字母表示,且其中只包含左括号'(',右括号‘)’ ...

  5. sicily 1934. 移动小球

    Description 你有一些小球,从左到右依次编号为1,2,3,...,n. 你可以执行两种指令(1或者2).其中, 1 X Y表示把小球X移动到小球Y的左边, 2 X Y表示把小球X移动到小球Y ...

  6. 2016年1月25日 《1024伐木累》-小白篇之开发网站,三天!(中篇-2奇怪的IE)-总章节十一

    往期回顾:  老王的“先见之明”,解决了困扰耗仔三人的大难题.顺利安装完开发工具,大家投入紧张的工作.航空部领导的突然闯入,IE不兼容,页面错乱,摆在三人面前的形势依然严峻.第一次见这阵仗的耗仔,又会 ...

  7. BZOJ 1024: [SCOI2009]生日快乐

    Description 将一个 \(x\times y\) 的矩形分成 \(n\) 块,让最长边:最短边 最小. Sol 搜索. \(n\) 只有 \(10\) 写一个类似于记搜的东西就好了. Cod ...

  8. HDU 1024 max sum plus

    A - Max Sum Plus Plus Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I6 ...

  9. RCA:未注意Curl-library Post 1024以上字节时的HTTP/1.1特性导致 HessianPHP 传输数据失败

    先列出 HessianPHP 的错误提示: CURL transport error: transfer closed with outstanding read data remaining 基础知 ...

  10. 使用TarOutputStream出现 request to write '1024' bytes exceeds size in header错误的解决方法

    因为测试流程中,所测客户端会根据服务器A返回的response决定发送给服务器B的请求里各参数的值,所以现在需要模拟服务器的响应.而这个项目服务器A的响应式返回一个流,一个GZIP压缩格式流,压缩的是 ...

随机推荐

  1. ostringstream的使用方法

    ostringstream的使用方法 [本文来自]http://www.builder.com.cn/2003/0304/83250.shtml http://www.cppblog.com/alan ...

  2. Jquery简单动画的实现记录

    <div style="background:#98bf21;height:100px;width:100px;"> //从元素当前所在位置,往下消失 $(docume ...

  3. objective-C学习笔记(五)函数成员:初始化器和析构器

    初始化器:init 对象初始化器: -(id)init 可以重载多个. 类型初始化器: +(id)initialize只能有一个. 对象初始化器: 初始化对象实例时,init通常和alloc(手动内存 ...

  4. POJ 3376 Finding Palindromes(扩展kmp+trie)

    题目链接:http://poj.org/problem?id=3376 题意:给你n个字符串m1.m2.m3...mn 求S = mimj(1=<i,j<=n)是回文串的数量 思路:我们考 ...

  5. HDU 3729 二分匹配 反向匹配

    题意: 给定 n个学生 说的 自己 考试排名的 可能范围 确定最多几个人说真话 如果有多种答案,输出字典序最大的那种( 要求字典序最大,所以solve中从最大字典序开始匹配) 思路: 题目给定  点 ...

  6. Creating a Broker (创建代理)

    1,CMD中运行 2,apollo的目录结构. bin  执行相关的脚步. etc  保存实例的配置文件 data  存储消息的文件 log 日志 tmp 临时的文件 3,Broker Configu ...

  7. 创建txt格式文本日志

    公共方法(可以将其放到类库里边): #region 记录日志 #region 写日志 /// <summary> /// 写日志 /// </summary> /// < ...

  8. C#指定目录存放DLL

    C#开发中,常常会用到不少扩展库,把这些扩展库的大量DLL放在软件目录下面,非常不美观. 通过设置自定义的DLL存放目录,可以把DLL存在指定的目录下面. 代码如下: <?xml version ...

  9. struts2上传下载

    struts上传下载必须引入两个jar文件: commons-fileupload-x.x.x.jar和comons-io-x.x.x.jar上传文件 import java.io.BufferedI ...

  10. python数据库连接

    现在装python基本都内置了sqlite连接,写成如下形式即可 from sqlite3 import dbapi2 as sqlite 如果需要insert或update东西,之后的cur必须co ...