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. Linux下interface文件修改

    我们来通过一些例子,来记录interfaces文件的书写.详情可参照man interfaces. 设置常用ethernet参数 auto lo iface lo inet loopback # Th ...

  2. linux 开机启动过程详解

    Linux开机执行内核后会启动init进程,该进程根据runlevel(如x)执行/etc/rcx.d/下的程序,其下的程序是符号链接,真正的程序放在/etc/init.d/下.开机启动的程序(服务等 ...

  3. svn 强制用户添加注释 和 允许用户修改注释

    当我们用TortoiseSVN提交代码时,有很多人不喜欢写注释,导致以后代码版本多,也不清楚哪个版本到底改了什么东西.所以在提交的时候,我会强制要求添加注释.这是如何实现的?这个话题就涉及到了svn的 ...

  4. CentOS 7 AMD64安装nginx和mysql

    NGINX: rpm -ivh http://nginx.org/packages/centos/7/x86_64/RPMS/nginx-1.8.0-1.el7.ngx.x86_64.rpm 查看: ...

  5. div文字超出

    效果一:div文字超出处理 <div style="width:200px; white-space:nowrap;overflow:hidden;text-overflow:elli ...

  6. strace命令(收集整理,常看常新)

    starce的用途和参数:http://man.linuxde.net/strace(linux命令大全) strace命令是一个集诊断.调试.统计与一体 的工具,我们可以使用strace对应用的系统 ...

  7. mysql Table 'performance_schema.session_variables' doesn't exist

    CMD  进入MYSQL的安装目录..Bin 下 执行 mysql_upgrade -u root -p --force 输入密码..然后等待一会儿..会跑一些东西..然后重启服务

  8. 由Collections.unmodifiableList引发的重构

    原文  http://www.cnblogs.com/persist-confident/p/4516741.html 今天阅读源码的时候,无意中看到了Collections.unmodifiable ...

  9. Linux 运行 apt-get install 就出现jdk installer 错误的解决方法

    解决办法如下: sudo rm /var/lib/dpkg/info/oracle-java7-installer* sudo apt-get purge oracle-java7-installer ...

  10. CF456B Fedya and Maths 找规律

    http://codeforces.com/contest/456/problem/B CF#260 div2 B Fedya and Maths Codeforces Round #260 B. F ...