POJ 1985 Cow Marathon

Source: Internet
Author: User

Topic Connection

http://poj.org/problem?id=1985

Cow marathondescription

After hearing about the epidemic of obesity in the USA, Farmer John wants his cows to get more exercise, so he had Committ Ed to create a bovine marathon for the cows to run. The marathon route would include a pair of farms and a path comprised of a sequence of roads between them. Since FJ wants the cows to get as much exercise as possible he wants to find the both farms on his map that is the farthes T apart from all other (distance being measured in terms of total length of road on the path between the same farms). Help him determine the distances between this farthest pair of farms.

Input

* Lines 1 ...: same input format as "Navigation nightmare".

Output

* Line 1:an integer giving the distance between the farthest pair of farms.

Sample Input

7 6
1 6 E
6 3 9 E
3 5 7 S
4 1 3 N
2 4 W
4 7 2 S

Sample Output

52

The diameter of the tree.

#include <algorithm> #include <iostream> #include <cstdlib> #include <cstring> #include < cstdio> #include <vector> #include <queue> #include <map>using std::map;using std::min;using std:: Find;using std::p air;using std::queue;using std::vector;using std::multimap; #define PB (E) push_back (e) #define SZ (c) ( int) (c). Size () #define MP (A, b) Make_pair (A, B) #define ALL (c) (c). Begin (), (c). End () #define ITER (c) __typeof ((c). Begin () ) #define CLS (arr, Val) memset (arr, Val, sizeof (arr)) #define Cpresent (C, E) (Find (All (c), (e))! = (c). End ()) #define REP (I, n) for (int i = 0; i < (int.) n; i++) #define TR (c, I) for (ITER (c) i = (c). Begin (); I! = (c). end (); ++i) const int n = 50010 ; const int INF = 0x3f3f3f3f;struct work {struct Edge {int to, w, next;}    G[n << 1];    int tot, head[n], dist[n];    inline void init () {tot = 0, CLS (Head,-1);        } inline void Add_edge (int u, int v, int w) {G[tot] = (edge) {V, W, Head[u]}; head[u] = tot++; G[toT] = (edge) {u, W, Head[v]};    HEAD[V] = tot++;        } inline int BFs (int s) {int id = s, max_dist = 0;        CLS (Dist,-1);        Queue<int> Q;        Q.push (s);        Dist[s] = 0;            while (!q.empty ()) {int u = q.front (); Q.pop ();            if (Dist[u] > max_dist) {max_dist = Dist[id = u];                } for (int i = head[u]; ~i; i = g[i].next) {Edge &e = G[i];                    if ( -1 = = Dist[e.to]) {dist[e.to] = Dist[u] + e.w;                Q.push (e.to);    }}} return ID;        } inline void solve (int m) {char ch;        int U, V, W;        CLS (Head,-1);            while (m--) {scanf ("%d%d%d%c", &u, &v, &w, &ch);        Add_edge (U, V, W);    } printf ("%d\n", Dist[bfs (BFS (U))]);    }}go;int Main () {#ifdef LOCAL freopen ("In.txt", "R", stdin); Freopen ("OUT.txt", "w+", stdout); #endif int N,M    while (~SCANF ("%d%d", &n, &m)) {go.solve (M); } return 0;}

POJ 1985 Cow Marathon

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.