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. 爬虫2 url管理器 url_manager.py

    #coding:utf8 class UrlManager(object): def __init__(self): self.new_urls = set() self.old_urls = set ...

  2. 多日期选择jQuery插件 MultiDatesPicker for jQuery UI

    Multiple-Dates-Picker-for-jQuery-UI是一个多日期选择的jquery控件.   GIT源码: https://github.com/dubrox/Multiple-Da ...

  3. 20145212 实验五《Java网络编程》

    20145212 实验五<Java网络编程> 一.实验内容 1.运行下载的TCP代码,结对进行,一人服务器,一人客户端: 2.利用加解密代码包,编译运行代码,一人加密,一人解密: 3.集成 ...

  4. 20145212 实验三《敏捷开发与XP实践》

    20145212 实验三<敏捷开发与XP实践> 实验内容 使用git上传代码 与20145223同学一组,使用git相互更改代码 同组实验报告链接:http://www.cnblogs.c ...

  5. Robot Framework--06 用户关键字User Keyword

    转自:http://blog.csdn.net/tulituqi/article/details/7906130 在我们做自动化案例的时候,用的最多的主要是用户关键字.说到关键字,大体上可以分为测试库 ...

  6. C#中事件的使用

    C#中事件的使用  http://www.cnblogs.com/wayfarer/archive/2004/04/20/6712.html 用一个例子来说明事件的使用. 创建一个简单的类,名为Fil ...

  7. Python之路【第十六篇续】Django进阶篇

    Django请求生命周期 首先:对于所有的web框架来说本质就是一个socket服务端,浏览器是socket客户端 路由系统 在Django的urls中我们可以根据一个URL对应一个函数名来定义路由规 ...

  8. PHP使用数据库的并发问题(转)

    在并行系统中并发问题永远不可忽视.尽管PHP语言原生没有提供多线程机制,那并不意味着所有的操作都是线程安全的.尤其是在操作诸如订单.支付等业务系统中,更需要注意操作数据库的并发问题. 接下来我通过一个 ...

  9. Fortify

    sourceanalyzer -b my_buildid -scan -f xxx.fpr -b  取一个build的ID号,通常以这个项目名称加扫描时间为buildID-Xmx 指定JVM使用的最大 ...

  10. intelligencia.urlrewriter使用

    见github: https://github.com/sethyates/urlrewriter