题解报告:poj 1985 Cow Marathon(求树的直径)
Description
After hearing about the epidemic of obesity in the USA, Farmer John wants his cows to get more exercise, so he has committed to create a bovine marathon for his cows to run. The marathon route will 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 two farms on his map that are the farthest apart from each other (distance being measured in terms of total length of road on the path between the two 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 13 E 6 3 9 E 3 5 7 S 4 1 3 N 2 4 20 W 4 7 2 S
Sample Output
52
解题思路:求树的直径,多了边权,改一下模板即可。
AC代码(102ms):两次bfs。
1 #include<iostream> 2 #include<queue> 3 #include<string.h> 4 #include<cstdio> 5 using namespace std; 6 const int maxn=1e6+5; 7 struct EDGE{int to,cap,next;}edge[maxn<<1]; 8 struct node{ 9 int u,tot; 10 node(int x,int y):u(x),tot(y){} 11 }; 12 int n,x,y,w,q,cnt,res,maxcap,maxvex,head[maxn];char ch;bool vis[maxn]; 13 queue<node> que; 14 void add_edge(int u,int v,int w){ 15 edge[cnt].to=v; 16 edge[cnt].cap=w; 17 edge[cnt].next=head[u]; 18 head[u]=cnt++; 19 } 20 void bfs(int u,int cap,int &maxcap,int &maxvex){ 21 while(!que.empty())que.pop(); 22 memset(vis,false,sizeof(vis)); 23 que.push(node(u,cap));vis[u]=true; 24 while(!que.empty()){ 25 node nod=que.front();que.pop(); 26 for(int i=head[nod.u];~i;i=edge[i].next){ 27 int v=edge[i].to; 28 if(!vis[v]){ 29 vis[v]=true; 30 que.push(node(v,nod.tot+edge[i].cap));//先把其邻接点都入队,并标记为已访问 31 } 32 } 33 if(nod.tot>maxcap)maxcap=nod.tot,maxvex=nod.u;//再更新当前节点的深度 34 } 35 } 36 int main(){ 37 while(~scanf("%d%d",&n,&q)){ 38 memset(head,-1,sizeof(head));cnt=0; 39 while(q--){ 40 scanf("%d %d %d %c",&x,&y,&w,&ch); 41 add_edge(x,y,w); 42 add_edge(y,x,w); 43 } 44 maxcap=0,maxvex=1; 45 bfs(1,0,maxcap,maxvex);maxcap=0; 46 bfs(maxvex,0,maxcap,maxvex); 47 printf("%d\n",maxcap); 48 } 49 return 0; 50 }
AC代码二(157ms):一次dfs。
1 #include<iostream> 2 #include<string.h> 3 #include<cstdio> 4 using namespace std; 5 const int maxn=1e6+5; 6 struct EDGE{int to,cap,next;}edge[maxn<<1]; 7 int n,x,y,w,q,cnt,res,maxdist,head[maxn];char ch; 8 void add_edge(int u,int v,int w){ 9 edge[cnt].to=v; 10 edge[cnt].cap=w; 11 edge[cnt].next=head[u]; 12 head[u]=cnt++; 13 } 14 int dfs(int u,int fa,int &maxdist){ 15 int Dmax=0,Dsec=0; 16 for(int i=head[u];~i;i=edge[i].next){ 17 int v=edge[i].to; 18 if(v^fa){ 19 int nowd=dfs(v,u,maxdist)+edge[i].cap; 20 if(nowd>Dmax)Dsec=Dmax,Dmax=nowd; 21 else if(nowd>Dsec)Dsec=nowd; 22 } 23 } 24 maxdist=max(maxdist,Dmax+Dsec); 25 return Dmax; 26 } 27 int main(){ 28 while(~scanf("%d%d",&n,&q)){ 29 memset(head,-1,sizeof(head));cnt=0; 30 while(q--){ 31 scanf("%d %d %d %c",&x,&y,&w,&ch); 32 add_edge(x,y,w); 33 add_edge(y,x,w); 34 } 35 maxdist=0; 36 dfs(1,-1,maxdist); 37 printf("%d\n",maxdist); 38 } 39 return 0; 40 }