POJ 1985.Cow Marathon-树的直径-树的直径模板(BFS、DFS(vector存图)、DFS(前向星存图))
Time Limit: 2000MS | Memory Limit: 30000K | |
Total Submissions: 7536 | Accepted: 3559 | |
Case Time Limit: 1000MS |
Description
Input
Output
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
//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(前向星存图))的更多相关文章
- poj 1985 Cow Marathon
题目连接 http://poj.org/problem?id=1985 Cow Marathon Description After hearing about the epidemic of obe ...
- poj 1985 Cow Marathon【树的直径裸题】
Cow Marathon Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 4185 Accepted: 2118 Case ...
- POJ 1985 Cow Marathon && POJ 1849 Two(树的直径)
树的直径:树上的最长简单路径. 求解的方法是bfs或者dfs.先找任意一点,bfs或者dfs找出离他最远的那个点,那么这个点一定是该树直径的一个端点,记录下该端点,继续bfs或者dfs出来离他最远的一 ...
- poj:1985:Cow Marathon(求树的直径)
Cow Marathon Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 5496 Accepted: 2685 Case ...
- POJ 1985 Cow Marathon (模板题)(树的直径)
<题目链接> 题目大意: 给定一颗树,求出树的直径. 解题分析:树的直径模板题,以下程序分别用树形DP和两次BFS来求解. 树形DP: #include <cstdio> #i ...
- POJ 1985 Cow Marathon (树形DP,树的直径)
题意:给定一棵树,然后让你找出它的直径,也就是两点中的最远距离. 析:很明显这是一个树上DP,应该有三种方式,分别是两次DFS,两次BFS,和一次DFS,我只写了后两种. 代码如下: 两次BFS: # ...
- poj 1985 Cow Marathon 树的直径
题目链接:http://poj.org/problem?id=1985 After hearing about the epidemic of obesity in the USA, Farmer J ...
- POJ 1985 Cow Marathon(树的直径模板)
http://poj.org/problem?id=1985 题意:给出树,求最远距离. 题意: 树的直径. 树的直径是指树的最长简单路. 求法: 两遍BFS :先任选一个起点BFS找到最长路的终点, ...
- 题解报告:poj 1985 Cow Marathon(求树的直径)
Description After hearing about the epidemic of obesity in the USA, Farmer John wants his cows to ge ...
- BZOJ 3363 POJ 1985 Cow Marathon 树的直径
题目大意:给出一棵树.求两点间的最长距离. 思路:裸地树的直径.两次BFS,第一次随便找一个点宽搜.然后用上次宽搜时最远的点在宽搜.得到的最长距离就是树的直径. CODE: #include < ...
随机推荐
- 编译skia静态库时,图片解码库无法注册的问题
转载:http://www.cnblogs.com/imlucky/archive/2012/08/01/2617851.html 今天编译skia库,增加图片解码库时总是无效.按照此博客的方法修改后 ...
- uboot各文件及文件夹分析
1.配置编译 uboot的配置编译需要在linux原生文件夹下,因为在编译过程中会生成符号链接.在windows中不支持.配置方法是:首先cd进入uboot源码的根目录,然后在根目录下执行:make ...
- (转)史上最好的Python线程指南
来自AstalWind的好文,彻底认识python线程 http://www.cnblogs.com/huxi/archive/2010/06/26/1765808.html . . . . .
- Nginx简介及使用Nginx实现负载均衡的原理【通俗易懂,言简意赅】【转】
Nginx 这个轻量级.高性能的 web server 主要可以干两件事情: 直接作为http server(代替apache,对PHP需要FastCGI处理器支持): 另外一个功能就是作为反向代理服 ...
- Android 命名规范 (转)
刚接触android的时候,命名都是按照拼音来,所以有的时候想看懂命名的那个控件什么是什么用的,就要读一遍甚至好几遍才知道,这样的话,在代码的审查和修改过程中就会浪费不少不必要的时间.如果就是我一个人 ...
- web api 支持cors
1. configservice //******************* cors start *********************** var urls = Configuration[S ...
- Git版本回退的最佳方式
使用git开发的过程中,存在误提交的时候怎么办呢?不用慌张,强大的git提供了两种版本回退的方式,可以让你恢复提交之前的内容: 方式一:reset(不推荐) 通过reset的方式,把head指针指向之 ...
- SpringBoot 在CentOS7部署,注册为服务,开机启动
1.首先在maven工程的pom文件中引入以下标签并保存 <build> <plugins> <plugin> <groupId>org.springf ...
- [CodeChef - GERALD07 ] Chef and Graph Queries
Read problems statements in Mandarin Chineseand Russian. Problem Statement Chef has a undirected gra ...
- .NET FrameWork 中的 CTS
CTS:Common Type System 通用类型系统. 1.不仅可以把C#编译成.Net IL,还支持Basic.Python.Ruby等语言,甚至还支持Java.不同语言中的数据类型定义是不一 ...