POJ3631

来源:互联网 发布:mac地址修改器安卓 编辑:程序博客网 时间:2024/05/15 00:08
#include <iostream>#include <cstdio>#include <algorithm>#include <vector>#include <cstring>using namespace std;const int maxn = 10010;const int bound = 10000;struct edge {    int to;    int w;};int d[maxn];bool vis[maxn];vector<edge> G[maxn];void dfs(int start) {    vis[start] = false;    edge v;    for(int i = 0; i < (int)G[start].size(); i++) {        v = G[start][i];        if(vis[v.to]) {            d[v.to] = d[start] + v.w;            dfs(v.to);        }    }}void init() {    memset(d, 0, sizeof(d));    memset(vis, true, sizeof(vis));}int main(){   //freopen("in", "r", stdin);    int x, y, w;    edge read;    while(scanf("%d%d%d", &x, &y, &w) != EOF) {        read.to = x;        read.w = w;        G[y].push_back(read);        read.to = y;        G[x].push_back(read);    }    init();    dfs(1);    int index, maxv = -1;    for(int i = 1; i <= bound; i++) {        if(d[i] > maxv) {            maxv = d[i];            index = i;        }    }    init();    dfs(index);    maxv = -1;    for(int i = 1; i <= bound; i++) {        maxv = max(d[i], maxv);    }    printf("%d\n", maxv);    return 0;}

0 0