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. 《深入理解bootstrap》读书笔记:第一章 入门准备

    一.bootstrap框架简介 Bootstrap是最流行的前端开发框架. 什么是框架:开发过程的半成品. bootstrap具有以下重要特性: (1)完整的CSS样式插件 (2)丰富的预定义样式表 ...

  2. ES6标准

    1. ES6标准感觉越来越向传统语言靠拢了,以后写到ES6的标准都记录下: ,,]; // =>操作符 array.forEach(v => console.log(v)); 是不是简化了 ...

  3. 一个有趣的模拟光照的shader

    一个有趣的模拟光照的shader(类似法线贴图) http://www.cnblogs.com/flytrace/p/3395911.html -----  可否用于需UI中需要加灯的模型.

  4. NopCommerce源码分析ContainerBuilder builder.Update(container)

    /// <summary> /// Register dependencies /// </summary> /// <param name="config&q ...

  5. MVC5的AuthorizeAttribute详解

    现今大多数的网站尤其是购物网站都要求你登录后才能继续操作,当你匿名的将商品放入购物车后,不可能匿名购买这时可以转到登录界面让用户进行登录验证. 适用系统自带的过滤器 MVC5只要将属性[Authori ...

  6. Thread 与 Runnable

    在Java中可有两种方式实现多线程,一种是继承Thread类,一种是实现Runnable接口:Thread类是在java.lang包中定义的.一个类只要继承了Thread类同时覆写了本类中的run() ...

  7. PHP数组处理函数的使用array_push(一)

    使用PHP做数据处理时会涉及到大量的数组操作,这里我就记下各种数组处理函数的使用方法,好记性不如烂笔头!! 一.array_push的使用方法:将一个或多个单元压入数组的末尾(入栈) 说明:int a ...

  8. java中如何将字符串数组转换成字符串(转)

    如果是 “字符串数组” 转 “字符串”,只能通过循环,没有其它方法 String[] str = {"abc", "bcd", "def"} ...

  9. Serializer序列化/反序列化DateTime少了8小时问题解决

    1.举例子 JavascriptSerializer serializer = new JavascriptSerializer(); DateTime now = DateTime.Parse(&q ...

  10. C# Winform 脱离 Framework (二)

    第一个Method: //启动应用程序 VOID RunApplication(LPTSTR lpFilename, LPTSTR args) { //WinExec(lpFilename, SW_S ...