http://poj.org/problem?id=2631

2333水题,

有一个小技巧是说随便找一个点作为起点,

找到这个点的最远点,

以这个最远点为起点,

再次找到的最远点就是这个图的最远点

证明可以用三角形定理

#include<iostream>
#include<cstdio>
#include<vector>
#include<cstring>
#define maxn 10005
using namespace std;
struct donser
{
int tow;
int dis;
};
vector<donser> vec[maxn];
int maxdistances=,distances=,maxtown=;
bool used[maxn];
void maxway(int x)
{
int way=vec[x].size(),i=;
struct donser a;
//cout<<"?x:"<<x<<" vec[x].size():"<<way<<endl;
while(i<way)
{
a=vec[x].at(i);
i++;
if(used[a.tow]) continue;
distances+=a.dis;
//cout<<"+a.dis:"<<a.dis<<endl;
used[a.tow]=;
maxway(a.tow);
if(distances>=maxdistances)
{
maxdistances=distances;
maxtown=a.tow;
}
used[a.tow]=;
//cout<<"-a.dis:"<<a.dis<<endl;
distances-=a.dis;
}
return;
}
int main()
{
int a,b,dist;
struct donser num;
//freopen("in.txt","r",stdin);
while(~scanf("%d%d%d",&a,&b,&dist))
{
num.tow=b;
num.dis=dist;
vec[a].push_back(num);
num.tow=a;
vec[b].push_back(num);
}
used[]=;
maxway();
distances=;
//cout<<"!maxtown"<<maxtown<<"!maxdistances"<<maxdistances<<endl;
memset(used,,sizeof(used));
maxway(maxtown);
//cout<<"!maxtown"<<maxtown<<"!maxdistances"<<maxdistances<<endl;
cout<<maxdistances<<endl;
return ;
}

POJ 2631 DFS+带权无向图最长路径的更多相关文章

  1. poj 1723 SOLDIERS 带权中位数

    题目 http://poj.org/problem?id=1723 题解 带权中位数类型的题目~ 可以先考虑降维,最后集合的y坐标,明显是y坐标的中位数的位置,容易求出y方向的贡献res_y.比较麻烦 ...

  2. K - Find them, Catch them POJ - 1703 (带权并查集)

    题目链接: K - Find them, Catch them POJ - 1703 题目大意:警方决定捣毁两大犯罪团伙:龙帮和蛇帮,显然一个帮派至少有一人.该城有N个罪犯,编号从1至N(N<= ...

  3. A Bug's Life POJ - 2492 (带权并查集)

    A Bug's Life POJ - 2492 Background Professor Hopper is researching the sexual behavior of a rare spe ...

  4. Roads in the North(POJ 2631 DFS)

    Description Building and maintaining roads among communities in the far North is an expensive busine ...

  5. POJ - 2912 Rochambeau (带权并查集+枚举)

    题意:有N个人被分为了三组,其中有一个人是开了挂的.同组的人的关系是‘=’,不同组的人关系是‘<’或'>',但是开了挂的人可以给出自己和他人任意的关系.现在要根据M条关系找出这个开了挂的人 ...

  6. Going from u to v or from v to u? POJ - 2762(强连通 有向最长路径)

    In order to make their sons brave, Jiajia and Wind take them to a big cave. The cave has n rooms, an ...

  7. POJ 3860 Fruit Weights(数学+最长路径 or 最短路径)

    Description Have you ever thought about comparing the weight of fruits? That’s what you should do in ...

  8. poj 1703(带权并查集)

    Find them, Catch them Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 31840   Accepted: ...

  9. POJ 1182 食物链 (带权并查集 && 向量偏移)

    题意 : 中文题就不说题意了…… 分析 : 通过普通并查集的整理归类, 能够单纯地知道某些元素是否在同一个集合内.但是题目不仅只有种类之分, 还有种类之间的关系, 即同类以及吃与被吃, 而且重点是题目 ...

随机推荐

  1. Window 中安装python多版本环境

    由于python 3 已是目前稳定,且是将来版本版本.新学者可以直接从python3入手学习.但是目前还有很多企业级应用使用的是python 2,可能还得要维护.所以,我们在本地windows中可以安 ...

  2. Linux下的压缩解压缩命令详解

    linux zip命令zip -r myfile.zip ./*将当前目录下的所有文件和文件夹全部压缩成myfile.zip文件,-r表示递归压缩子目录下所有文件. 2.unzipunzip -o - ...

  3. autofac + owin + webform + mvc + webapi集成demo

    http://git.oschina.net/shiningrise/AutofacOwinDemo using Microsoft.Owin; using Owin; using System.We ...

  4. JNDI全面总结

    JNDI全面总结原理: 在DataSource中事先建立多个数据库连接,保存在数据库连接池中.当程序访问数据库时,只用从连接池中取空闲状态的数据库连接即可,访问结束,销毁资源,数据库连接重新回到连接池 ...

  5. centos 7.0 编译安装mysql 5.6.22 再次总结 成功编译安装~ 越来越熟练了~

    查找php.ini文件所在位置 [root@localhost /]# find -name php.ini ./usr/etc/php/etc/php.ini mysql官网的安装说明http:// ...

  6. [MongoDB]对数组操作

    摘要 在实际开发中遇到更新某个document中的数组的值,这里做一下记录. 这里使用的驱动为 using MongoDB.Bson;using MongoDB.Driver; 相关文章 [Mongo ...

  7. jq 构造函数,然后再表单提交过程中对数据进行修改

    先贴代码 <script type="text/javascript"> function appendText(){ var content = $("#t ...

  8. LAMP环境CentOS6.4 PHP5.4随笔未整理

    首先安装一些辅助的软件或者说是依赖的关系包. 1.安装libxml2: libxml是一个用来解析XML文档的函数库.它用C语言写成, 并且能为多种语言所调用,例如C语言,C++,XSH.C#, Py ...

  9. C语言中的EOF和回车不一样

    经常我们碰到这样一个C语言问题,例如: 输入一个组整数,按照从小到大排序后输出结果 输入:  1 7 9 2 4 输出:  1 2 4 7 9 这里要用C语言读入一段数的话,如果用 int array ...

  10. MYSQL 连接数据库命令收藏

    一.MySQL 连接本地数据库,用户名为“root”,密码“123”(注意:“-p”和“123” 之间不能有空格) C:\>mysql -h localhost -u root -p123 二. ...