Time Limit: 2000MS   Memory Limit: 30000K
Total Submissions: 7536   Accepted: 3559
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 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. 
 
题意就是求树的直径。水这篇博客为了存板子,自己创的板子,不知道写其他题怎么样,自我感觉很好用,因为自己用着顺手。
 
代码1(BFS版):
 //BFS
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<bitset>
#include<cassert>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<deque>
#include<iomanip>
#include<list>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii; const double PI=acos(-1.0);
const double eps=1e-;
const ll mod=1e9+;
const int inf=0x3f3f3f3f;
const int maxn=1e5+;
const int maxm=+;
#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); int n,m,cnt;//cnt为边数
int dist[maxn],head[maxn];//dist表示最长路,head为存图用的
bool vis[maxn]; struct node{//定义边的结构体
int from,to,val,next;
}edge[maxn<<];//注意是无向图,边数是二倍的 void init()//初始化,不可少
{
cnt=;
memset(head,-,sizeof(head));
} void addedge(int u,int v,int w)
{
edge[cnt].from=u;//起点
edge[cnt].to=v;//终点
edge[cnt].val=w;//权值
edge[cnt].next=head[u];//指向下一条边
head[u]=cnt++;
} int length;//最终的最长路径(树的直径)
int node;//记录端点值 void bfs(int s)
{
queue<int>q;//定义队列
memset(vis,false,sizeof(vis));//初始化,清零
memset(dist,,sizeof(dist));
q.push(s);//入列
vis[s]=true;//记录为遍历过的点
length=;
node=s;
while(!q.empty()){
int u=q.front();
q.pop();
for(int i=head[u];i!=-;i=edge[i].next){//遍历每一条边
int v=edge[i].to;
if(!vis[v]&&dist[v]<dist[u]+edge[i].val){
vis[v]=true;
dist[v]=dist[u]+edge[i].val;//到v的最长路径
if(length<dist[v]){
length=dist[v];//不断更新最长路径
node=v;//更新节点
}
q.push(v);//重新入列,寻找下一个点
}
}
}
} int main()
{
init();
scanf("%d%d",&n,&m);
for(int i=;i<=m;i++){
int u,v,val;char c[];
scanf("%d%d%d%s",&u,&v,&val,c);
addedge(u,v,val);
addedge(v,u,val);
}
bfs();//第一遍找到距离最远的端点
bfs(node);//第二遍找最长距离
printf("%d\n",length);
return ;
}

代码2(DFS vector存图版):

 //DFS-vector存图
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<bitset>
#include<cassert>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<deque>
#include<iomanip>
#include<list>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii; const double PI=acos(-1.0);
const double eps=1e-;
const ll mod=1e9+;
const int inf=0x3f3f3f3f;
const int maxn=1e5+;
const int maxm=+;
#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); struct node{
int to,val;
}; int n,m,length,start;//length为路径长度,start为树的直径开始的节点
vector <node> G[maxn<<];//存图
bool vis[maxn<<];
int dist[maxn<<]; void init()//初始化
{
length=-;
memset(vis,false,sizeof(vis));
for(int i=;i<n;i++)
dist[i]=inf;
} void dfs(int u)
{
vis[u]=true;
for(int i=;i<G[u].size();i++){//对与顶点u相连的点数进行扫描
node v=G[u][i];
if (!vis[v.to]){//如果没有访问过
dist[v.to]=dist[u]+G[u][i].val;//更新节点
if(dist[v.to]>length&&dist[v.to]!=inf){//更新路径长度
length=dist[v.to];
start=v.to;//更新节点
}
dfs(v.to);
}
}
} int main()
{
scanf("%d%d",&n,&m);
for(int i=;i<=m;i++){
int u,v,w;char c[];
scanf("%d%d%d%s",&u,&v,&w,c);
G[u-].push_back({v-,w});
G[v-].push_back({u-,w});
}
init();dist[]=;dfs();
init();dist[start]=;dfs(start);
printf("%d\n",length);
}

代码3(DFS 前向星存图版):

 //DFS-前向星存图
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<bitset>
#include<cassert>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<deque>
#include<iomanip>
#include<list>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii; const double PI=acos(-1.0);
const double eps=1e-;
const ll mod=1e9+;
const int inf=0x3f3f3f3f;
const int maxn=1e5+;
const int maxm=+;
#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); struct node{
int to,nex,val;
}edge[maxn<<]; int n,m,cnt=,length,start;
int dist[maxn<<],head[maxn<<];
bool vis[maxn]; void addedge(int x,int y,int w)
{
edge[++cnt].to=y;
edge[cnt].val=w;
edge[cnt].nex=head[x];
head[x]=cnt;
} void init()//初始化
{
length=-;
memset(vis,false,sizeof(vis));
for(int i=;i<n;i++)
dist[i]=inf;
} void dfs(int u)
{
vis[u]=true;
for(int i=head[u];i;i=edge[i].nex){//对与顶点u相连的点数进行扫描
int v=edge[i].to;
if (!vis[v]){//如果没有访问过
dist[v]=dist[u]+edge[i].val;//更新节点
if(dist[v]>length&&dist[v]!=inf){//更新路径长度
length=dist[v];
start=v;//更新节点
}
dfs(v);
}
}
} int main()
{
scanf("%d%d",&n,&m);
for(int i=;i<=m;i++){
int u,v,w;char c[];
scanf("%d%d%d%s",&u,&v,&w,c);
addedge(u-,v-,w);
addedge(v-,u-,w);
}
init();dist[]=;dfs();
init();dist[start]=;dfs(start);
printf("%d\n",length);
}

OK,滚了。。。

 

POJ 1985.Cow Marathon-树的直径-树的直径模板(BFS、DFS(vector存图)、DFS(前向星存图))的更多相关文章

  1. poj 1985 Cow Marathon

    题目连接 http://poj.org/problem?id=1985 Cow Marathon Description After hearing about the epidemic of obe ...

  2. poj 1985 Cow Marathon【树的直径裸题】

    Cow Marathon Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 4185   Accepted: 2118 Case ...

  3. POJ 1985 Cow Marathon && POJ 1849 Two(树的直径)

    树的直径:树上的最长简单路径. 求解的方法是bfs或者dfs.先找任意一点,bfs或者dfs找出离他最远的那个点,那么这个点一定是该树直径的一个端点,记录下该端点,继续bfs或者dfs出来离他最远的一 ...

  4. poj:1985:Cow Marathon(求树的直径)

    Cow Marathon Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 5496   Accepted: 2685 Case ...

  5. POJ 1985 Cow Marathon (模板题)(树的直径)

    <题目链接> 题目大意: 给定一颗树,求出树的直径. 解题分析:树的直径模板题,以下程序分别用树形DP和两次BFS来求解. 树形DP: #include <cstdio> #i ...

  6. POJ 1985 Cow Marathon (树形DP,树的直径)

    题意:给定一棵树,然后让你找出它的直径,也就是两点中的最远距离. 析:很明显这是一个树上DP,应该有三种方式,分别是两次DFS,两次BFS,和一次DFS,我只写了后两种. 代码如下: 两次BFS: # ...

  7. poj 1985 Cow Marathon 树的直径

    题目链接:http://poj.org/problem?id=1985 After hearing about the epidemic of obesity in the USA, Farmer J ...

  8. POJ 1985 Cow Marathon(树的直径模板)

    http://poj.org/problem?id=1985 题意:给出树,求最远距离. 题意: 树的直径. 树的直径是指树的最长简单路. 求法: 两遍BFS :先任选一个起点BFS找到最长路的终点, ...

  9. 题解报告:poj 1985 Cow Marathon(求树的直径)

    Description After hearing about the epidemic of obesity in the USA, Farmer John wants his cows to ge ...

  10. BZOJ 3363 POJ 1985 Cow Marathon 树的直径

    题目大意:给出一棵树.求两点间的最长距离. 思路:裸地树的直径.两次BFS,第一次随便找一个点宽搜.然后用上次宽搜时最远的点在宽搜.得到的最长距离就是树的直径. CODE: #include < ...

随机推荐

  1. Nginx的启动、停止、平滑重启

    转载自:http://www.xj123.info/2572.html 启动Nginx /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/ngi ...

  2. Android之实现定位

    基于android的定位无非就两种:network.gps.两者各有优劣. Network:定位快,准确度低,受环境影响小. GPS:定位慢,准确度高,受环境影响大. 本文要解决的问题: 1.     ...

  3. LightOJ 1269 - Consecutive Sum Trie树

    题意:给出一串序列,求区间连续异或值的最大和最小. 思路:如果不是出在专题里,想不到可以用字典树做.先求前缀异或值,转为二进制,加入Trie树中,如果要求最大,就是尽可能走和当前位数字相反的,这样异或 ...

  4. opencv在property panel中新建一行

    是用cv2.QT_NEW_BUTTONBAR和button type通过竖线结合可以在创建一行,如下 cv2.createButton("CV_RADIOBOX2", redraw ...

  5. UIActivityIndicatorView---iOS-Apple苹果官方文档翻译

    本系列所有开发文档翻译链接地址: iOS7开发-Apple苹果iPhone开发Xcode官方文档翻译PDF下载地址 UIActivityIndicatorViewactivityIndicatorVi ...

  6. Vuejs - 工欲善其事必先利其器

    既然是实战,怎离不开项目开发的环境呢?先给大家推荐下我的个人开发环境: 硬件设备:Mac OSX编译器:Visual Studio Code命令行工具:iTerm2调试工具:Chrome Dev to ...

  7. HDU 1002 A + B Problem II (大数加法)

    题目链接 Problem Description I have a very simple problem for you. Given two integers A and B, your job ...

  8. 一个简单插件this传值的跟踪

    <!DOCUTYPE html> <html> <head> <meta charset="UTF-8"> <script s ...

  9. monkey测试===Monkey测试结果分析(系列三)转

    Monkey测试结果分析 一. 初步分析方法: Monkey测试出现错误后,一般的差错步骤为以下几步: 1. 找到是monkey里面的哪个地方出错 2. 查看Monkey里面出错前的一些事件动作,并手 ...

  10. mac os x 把reids nignx mongodb做成随机启动吧

    ~/Library/LaunchAgents 由用户自己定义的任务项 /Library/LaunchAgents 由管理员为用户定义的任务项 /Library/LaunchDaemons 由管理员定义 ...