Cow Marathon
Time Limit: 2000MS |
|
Memory Limit: 30000K |
Total Submissions: 4182 |
|
Accepted: 2116 |
Case Time Limit: 1000MS |
Description
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 E6 3 9 E3 5 7 S4 1 3 N2 4 W4 7 2 S
Sample Output
52
Hint
The longest marathon runs from farm 2 via Roads 4, 1, 6 and 3 to farm 5 and is of length 20+3+13+9+7=52.
The diameter problem of the first tree
#include <cstdio> #include <string> #include <cstring> #include <algorithm> #include <queue > #define MAXN 40000+10#define maxm 80000+10#define INF 0x3f3f3f3fusing namespace std;struct node{int u, V, W, next; };node Edge[maxm];int HEAD[MAXN], Cnt;int DIST[MAXN], vis[maxn];int N, M;int sum, ed; void Init () {cnt = 0; Memset (Head,-1, sizeof (head));} void Add (int u, int v, int w) {edge[cnt] = {u, V, W, Head[u]}; Head[u] = cnt++; EDGE[CNT] = {V, u, W, Head[v]}; HEAD[V] = cnt++;} void Getmap () {char str[5]; while (m--) {int A, b, C; scanf ("%d%d%d%s", &a, &b, &c, str); Add (A, B, c); }}void BFS (int sx) {queue<int>q; for (int i = 1; I <= n; ++i) {vis[i] = 0; Dist[i] = 0; } VIS[SX] = 1; sum = 0; ed = sx; Q.push (SX); while (!q.empty ()) {int u = q.front (); Q.pop (); for (int i = head[u]; i =-1; i = edge[i].next) {int v = EDGE[I].V; if (!vis[v]) {Dist[v] = Dist[u] + edge[i].w; if (Sum < Dist[v]) {ed = v; sum = Dist[v]; } Vis[v] = 1; Q.push (v); }}}}int Main () {while (scanf ("%d%d", &n, &m)! = EOF) {init (); Getmap (); BFS (1); BFS (ed); printf ("%d\n", sum); } return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
POJ 1985--cow Marathon "tree diameter && template"