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 < ...
随机推荐
- 制定clone的用户名
git clone http://username:password@127.0.0.1/res/res.git指定用户名clone,有时需要切换clone 的用户名,不切换,会默认config us ...
- Hexo&Github-Pages搭建个人博客
some基础知识 hexo hexo是一款基于Node.js的静态博客框架 github-pages说明 github有两种主页,一种是github-page(个人主页),一种是项目主页,本教程针对个 ...
- [洛谷P1527] [国家集训队]矩阵乘法
洛谷题目链接:[国家集训队]矩阵乘法 题目背景 原 <补丁VS错误>请前往P2761 题目描述 给你一个N*N的矩阵,不用算矩阵乘法,但是每次询问一个子矩形的第K小数. 输入输出格式 输入 ...
- 「模板」「讲解」Treap名次树
Treap实现名次树 前言 学平衡树的过程可以说是相当艰难.浏览Blog的过程中看到大量指针版平衡树,不擅长指针操作的我已经接近崩溃.于是,我想着一定要写一篇非指针实现的Treap的Blog. 具体如 ...
- .net core 安装Swagger
Install-Package Swashbuckle -Pre 1.Startup // This method gets called by the runtime. Use this metho ...
- jenkins Process leaked file descriptors
https://stackoverflow.com/questions/17024441/process-leaked-file-descriptors-error-on-jenkins 1. BUI ...
- 启动hbase输出ignoring option PermSize=128m; support was removed in 8.0告警信息
./start-hbase.sh starting master, logging to /home/hadoop/hbase-1.2.4/bin/../logs/hbase-hadoop-maste ...
- Maven整体认识——详细介绍
前言 本文可以帮助你加深对Maven的整体认识,不是一篇基础文章.如果你现在还没有用 Maven 跑过 HelloWorld,那么本文可能不适合你. 一.Maven简介 Maven 官网:https: ...
- zoj2001 Adding Reversed Numbers
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2001 Adding Reversed Numbers Time ...
- JVM在遇到OOM(OutOfMemoryError)时生成Dump文件
方法一: 命令:jmap -dump:format=b,file=heap.bin file:保存路径及文件名pid:进程编号(windows通过任务管理器查看,linux通过ps aux查看) du ...