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 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.
Source |
原题链接: http://poj.org/problem?id=1985
题目大意:有n个农场,这n个农场有一些边连着,然后要你找出两个点,使得这一对点的路径长度最大,输出这个最大的长度.输入的最后一个参数没用....
直接裸树的直径就可以了.之前都用DFS解决,现在是一下BFS.并且用了pair,建图方便多了,不用结构体,不用写构造函数,配合vector,简直完美...
AC代码1.DFS
/**
* 行有余力,则来刷题!
* 博客链接:http://blog.csdn.net/hurmishine
*
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
using namespace std;
const int maxn=100000;
int n,m;
struct Edge
{
int v,w;
Edge(){}
Edge(int v,int w):v(v),w(w){}
};
vector<Edge>G[maxn];
int dis[2][maxn];
bool vis[maxn];
int DIS;
int p;
void DFS(int root,int flag)
{
vis[root]=true;
if(dis[flag][root]>DIS)
{
DIS=dis[flag][root];
p=root;
}
for(int i=0;i<G[root].size();i++)
{
int v=G[root][i].v;
int w=G[root][i].w;
if(!vis[v])
{
vis[v]=true;
dis[flag][v]=dis[flag][root]+w;
DFS(v,flag);
}
}
}
int main()
{
//freopen("C:\\Documents and Settings\\Administrator\\桌面\\data.txt","r",stdin);
while(cin>>n>>m)
{
int u,v,w;
char ch[2];
for(int i=0;i<=n;i++)
{
G[i].clear();
}
for(int i=0;i<m;i++)
{
scanf("%d%d%d%s",&u,&v,&w,ch);
G[u].push_back(Edge(v,w));
G[v].push_back(Edge(u,w));
}
memset(dis,0,sizeof(dis));
memset(vis,false,sizeof(vis));
DIS=0;
DFS(1,0);
int d1=p;
memset(vis,false,sizeof(vis));
DIS=0;
DFS(d1,1);
cout<<dis[1][p]<<endl;
}
return 0;
}
AC代码2:BFS.
/**
* 行有余力,则来刷题!
* 博客链接:http://blog.csdn.net/hurmishine
*
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
using namespace std;
const int maxn=100000;
int n,m;
struct Edge
{
int v,w;
Edge(){}
Edge(int v,int w):v(v),w(w){}
};
vector<Edge>G[maxn];
int dis[2][maxn];
bool vis[maxn];
int DIS;
int p;
int BFS(int root,int flag)
{
vis[root]=true;
DIS=0;
int r=0;
queue<int>q;
q.push(root);
while(!q.empty())
{
int p=q.front();
q.pop();
for(int i=0;i<G[p].size();i++)
{
int v=G[p][i].v;
int w=G[p][i].w;
if(!vis[v])
{
vis[v]=true;
dis[flag][v]=dis[flag][p]+w;
if(dis[flag][v]>DIS)
{
DIS=dis[flag][v];
r=v;
}
q.push(v);
}
}
}
return r;
}
int main()
{
//freopen("C:\\Documents and Settings\\Administrator\\桌面\\data.txt","r",stdin);
while(cin>>n>>m)
{
int u,v,w;
char ch[2];
for(int i=0;i<=n;i++)
{
G[i].clear();
}
for(int i=0;i<m;i++)
{
scanf("%d%d%d%s",&u,&v,&w,ch);
G[u].push_back(Edge(v,w));
G[v].push_back(Edge(u,w));
}
memset(dis,0,sizeof(dis));
memset(vis,false,sizeof(vis));
int r1=BFS(1,0);
memset(vis,false,sizeof(vis));
int r2=BFS(r1,1);
cout<<dis[1][r2]<<endl;
}
return 0;
}