UVA 10308 Roads in the northern Road

Source: Internet
Author: User

Original question:
Building and maintaining roads among communities in a expensive business. With
This-in-mind, the roads was built in such a-on-the-one-there is-one route from a-village to a-village
That's does not pass through some and other village twice.
Given is an area in the comprising a number of villages and roads among them such
Any village can is reached by road from all other village. Your job is to find the road distance between
The most remote villages in the area.
The villages connected by road segments. The villages is numbered from 1.
Input
The input contains several sets of input. Each set of input are a sequence of lines, each containing three
Positive integers:the number of a village, the number of a different village, and the length of the road
Segment connecting the villages in kilometers. All road segments is two-way. Consecutive sets
is separated by a blank line.
Output
For each set of the input, you is to output a containing a single integer:the road distance
Between the most remote villages in the area.
Sample Input
5 1 6
1 4 5
6 3 9
2 6 8
6 1 7
Sample Output
22
Main topic:
To build roads for villages, the route is more special. There is only one pathway between any of the two villages, and will not pass through a village two times. Ask you what is the distance between the two villages farthest apart.
See below code for ideas:

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <vector>
#include <cstring>
#include <string>
#include <cmath>
#include <iomanip>
#include <queue>
#include <cstring>
#include <sstream>
using namespace std;
const int maxn = 10001;
int ans = 0;
vector <pair <int, int>> vp [maxn]; // The village number and distance connected to i
int dfs (int to, int from)
{
    int Aroad = 0, tem;
    for (int i = 0; i <vp [to] .size (); i ++)
    {
        int go = vp [to] [i] .first;
        if (go! = from)
        {
            tem = dfs (go, to) + vp [to] [i] .second;
            ans = max (ans, tem + Aroad);
            Aroad = max (tem, Aroad);
        }
    }
    return Aroad;
}
int main ()
{
    ios :: sync_with_stdio (false);
    int a, b, c;
    string s;
    while (! cin.eof ())
    {
        for (int i = 0; i <maxn; i ++)
        vp [i] .clear ();
        ans = 0;
        getline (cin, s);
        while (s.length ()> 0 &&! cin.eof ())
        {
            stringstream ss;
            ss << s;
            ss >> a >> b >> c;
            vp [a] .push_back (make_pair (b, c));
            vp [b] .push_back (make_pair (a, c));
            getline (cin, s);
        }
        dfs (1,0);
        cout << ans << endl;
    }
    return 0;
} 


idea:
The question just came up when I used floyed, the result must be timed out. It's really not going to work. Know that this is a tree that is similar to the smallest spanning tree. Is that any node can be regarded as the root, and then found in this tree the longest distance of two nodes is called the tree diameter , modeled after someone else's code written. The idea is to find a node, for example, node I search to find a furthest path, this path is now recorded as Aroad, and then backtrack search from I from the other one farthest path, recorded as Broad. The answer is equivalent to the sum of the two paths found with the I node as the intermediary. At first I also wondered why a search, and choose any point as a search starting point can, in fact, or their own understanding of the backtracking method is poor, find a data on the paper on the simulation on it to understand.
For example, the following data:
1 2 1
2 3
2 4 ten


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.