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. 基于Python实现对PDF文件的OCR识别

    http://www.jb51.net/article/89955.htm https://pythontips.com/2016/02/25/ocr-on-pdf-files-using-pytho ...

  2. Spring MVC学习笔记——Welcome

    参考: http://blog.csdn.net/hehexiaoyou/article/details/23747617 http://www.codingyun.com/article/47.ht ...

  3. Linux学习笔记(一)2015.4.13

    研究生由单片机转Linux学习 首先安装VMware虚拟机,用的是VMware 10.0 在VMware 10.0上安装视频上推荐的Red Hat Linux 5 安装后正式进入Linux学习 笔记1 ...

  4. 《深入理解bootstrap》读书笔记:第4章 CSS组件(下)

    十. 标签(.label类,label-xxx) 高亮一些标题部分. 1 2 3 4 5 6 <h1>HELLO<span class="label label-defau ...

  5. GC算法

    http://www.brpreiss.com/books/opus5/html/page424.html http://www.brpreiss.com/books/opus5/html/page4 ...

  6. Construct Bounding Sphere

    点集的包围球 http://en.wikipedia.org/wiki/Bounding_sphere http://blogs.agi.com/insight3d/index.php/2008/02 ...

  7. sky

    UniSky入门资料 http://game.ceeger.com/forum/read.php?tid=4069

  8. Python 线程、进程和协程

    python提供了两个模块来实现多线程thread 和threading ,thread 有一些缺点,在threading 得到了弥补,为了不浪费时间,所以我们直接学习threading 就可以了. ...

  9. Jquery中的$().each() 方法

    先举例子, 输出每个 li 元素的文本: <html> <head> <script type="text/javascript" src=" ...

  10. 使用Java练习算法常用的基本操作

    一.使用Java练习算法常常需要使用控制台的数据输入和输出,下面记录一下基本的使用方法: 基本用法 import java.util.*; public class Main { public sta ...