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. jsp链接sql数据库

    Connection 参数//这个参数用来执行链接数据库的操作 String 参数2="com.microsoft.sqlserver.jdbc.SQLServerDriver"; ...

  2. intellij idea 破解补丁激活

    一.说明 idea激活可以用JetBrains account,Activation Code注册码或者填License server网址,使用注册码的方式可以参考lanyun提供的注册码,但是有效时 ...

  3. Elasticsearch 5.6.5 安装教程

    下载地址   https://www.elastic.co/downloads/past-releases/elasticsearch-5-6-5 安装环境  centos6.5  ,  jdk1.8 ...

  4. WebDriver中如何处理Iframe 及 嵌套Iframe

    最近在用webdriver进行爬虫的时候,遇到了网站存在iframe的情况,处理了好久没有解决,后来发现原来webdriver自带处理方法,汗颜.. 1.iFrame有ID 或者 name的情况 // ...

  5. spring mvc 注解详解

    1.@Controller 在SpringMVC 中,控制器Controller 负责处理由DispatcherServlet 分发的请求,它把用户请求的数据经过业务处理层处理之后封装成一个Model ...

  6. 全面了解Nginx主要应用场景(数漫江湖)

    前言 本文只针对Nginx在不加载第三方模块的情况能处理哪些事情,由于第三方模块太多所以也介绍不完,当然本文本身也可能介绍的不完整,毕竟只是我个人使用过和了解到过得.所以还请见谅,同时欢迎留言交流 N ...

  7. centos6.4 yum安装nginx+mysql+php

    1.配置防火墙,开启80端口.3306端口vi /etc/sysconfig/iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport ...

  8. C# 关于调用微信接口的代码

    调用微信接口前需要准备的内容. 1.微信公众平台的appid 2.微信公众平台的secret 3..获取tokenid 4.获取ticket 5.生成签名的随机串 6.生成签名的时间戳 7.生成签名 ...

  9. list互转datatable 支持Nullable转换

    /// <summary> /// list转datatable /// </summary> /// <param name="list">& ...

  10. servlet线程不安全

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAlgAAAE9CAIAAABY1Yv/AAAgAElEQVR4nOy9eVxN2/8/viuaU5kqZW