POJ 2631 Roads in the North(村庄最大距离,树的直径,经典题目)
2016-08-02 16:52
477 查看
Roads in the North(护眼背景)
Description
Building and maintaining roads among communities in the far North is an expensive business. With this in mind, the roads are build such that there is only one route from a village to a village that does not pass through some other village twice.
Given is an area in the far North comprising a number of villages and roads among them such that any village can be reached by road from any other village. Your job is to find the road distance between the two most remote villages in the area.
The area has up to 10,000 villages connected by road segments. The villages are numbered from 1.
Input
Input to the problem is a sequence of lines, each containing three positive integers: the number of a village, the number of a different village, and the length of the road segment connecting the villages in kilometers. All road segments are two-way.
Output
You are to output a single integer: the road distance between the two most remote villages in the area.
Sample Input
Sample Output
Source
The UofA Local 1999.10.16
题意:
给定一些村庄的距离,问哪两个最远。
键入数据:
村庄编号 村庄编号 距离
村庄编号 村庄编号 距离
村庄编号 村庄编号 距离
终止条件!=EOF
思路:
就是简单的树的直径,愣是对着宇神的代码,打了一个小时
代码:1736K 32MS
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
const int MYDD=110300;
int edgenum;//边的数目
int head[MYDD];//节点头指针
void init_edge() {
edgenum=0;
memset(head,-1,sizeof(head));
}
struct EDGE { //边的信息
int u,v,w,next;
} edge[MYDD*2];
void addedge(int u,int v,int w) {//增加边信息
EDGE Edge= {u,v,w,head[u]};//可以学习这种方法赋值
edge[edgenum]=Edge;
head[u]=edgenum++;
}
bool vis[MYDD];//记录节点的访问状态
int dis[MYDD];//记录节点所在直径的最大值
int Tnode,ans;
void BFS(int sx) {
queue<int> Q;
memset(vis,false,sizeof(vis));
memset(dis,0,sizeof(dis));
vis[sx]=true;
Q.push(sx);
Tnode=sx;
ans=0;
while(!Q.empty()) {
int first=Q.front();//队列头元素
Q.pop();
for(int j=head[first]; j!=-1; j=edge[j].next) {
int v=edge[j].v;//当前边的终结点
if(!vis[v]) {
if(dis[v]<edge[j].w+dis[first]) {
dis[v]=edge[j].w+dis[first];
}
vis[v]=true;
Q.push(v);
if(ans<dis[v]) {//找到最大直径
ans=dis[v];
Tnode=v;
}
}
}
}
}
int main() {
int u,v,w;
init_edge(); //漏掉后超时
while(scanf("%d%d%d",&u,&v,&w)!=EOF) {
addedge(u,v,w);//坑人的结束方式
addedge(v,u,w);
}
BFS(1);
BFS(Tnode);
printf("%d\n",ans);
}
后:
继续敲
***************************
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 2554 | Accepted: 1257 |
Building and maintaining roads among communities in the far North is an expensive business. With this in mind, the roads are build such that there is only one route from a village to a village that does not pass through some other village twice.
Given is an area in the far North comprising a number of villages and roads among them such that any village can be reached by road from any other village. Your job is to find the road distance between the two most remote villages in the area.
The area has up to 10,000 villages connected by road segments. The villages are numbered from 1.
Input
Input to the problem is a sequence of lines, each containing three positive integers: the number of a village, the number of a different village, and the length of the road segment connecting the villages in kilometers. All road segments are two-way.
Output
You are to output a single integer: the road distance between the two most remote villages in the area.
Sample Input
5 1 6 1 4 5 6 3 9 2 6 8 6 1 7
Sample Output
22
Source
The UofA Local 1999.10.16
题意:
给定一些村庄的距离,问哪两个最远。
键入数据:
村庄编号 村庄编号 距离
村庄编号 村庄编号 距离
村庄编号 村庄编号 距离
终止条件!=EOF
思路:
就是简单的树的直径,愣是对着宇神的代码,打了一个小时
代码:1736K 32MS
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
const int MYDD=110300;
int edgenum;//边的数目
int head[MYDD];//节点头指针
void init_edge() {
edgenum=0;
memset(head,-1,sizeof(head));
}
struct EDGE { //边的信息
int u,v,w,next;
} edge[MYDD*2];
void addedge(int u,int v,int w) {//增加边信息
EDGE Edge= {u,v,w,head[u]};//可以学习这种方法赋值
edge[edgenum]=Edge;
head[u]=edgenum++;
}
bool vis[MYDD];//记录节点的访问状态
int dis[MYDD];//记录节点所在直径的最大值
int Tnode,ans;
void BFS(int sx) {
queue<int> Q;
memset(vis,false,sizeof(vis));
memset(dis,0,sizeof(dis));
vis[sx]=true;
Q.push(sx);
Tnode=sx;
ans=0;
while(!Q.empty()) {
int first=Q.front();//队列头元素
Q.pop();
for(int j=head[first]; j!=-1; j=edge[j].next) {
int v=edge[j].v;//当前边的终结点
if(!vis[v]) {
if(dis[v]<edge[j].w+dis[first]) {
dis[v]=edge[j].w+dis[first];
}
vis[v]=true;
Q.push(v);
if(ans<dis[v]) {//找到最大直径
ans=dis[v];
Tnode=v;
}
}
}
}
}
int main() {
int u,v,w;
init_edge(); //漏掉后超时
while(scanf("%d%d%d",&u,&v,&w)!=EOF) {
addedge(u,v,w);//坑人的结束方式
addedge(v,u,w);
}
BFS(1);
BFS(Tnode);
printf("%d\n",ans);
}
后:
继续敲
***************************
相关文章推荐
- POJ:2631 Roads in the North(树的最大直径)
- poj--2631--Roads in the North(树的直径 裸模板)
- POJ 2631 Roads in the North(树的直径)
- 【树的直径】 POJ 2631 Roads in the North
- POJ Problem 2631 Roads in the North【树的直径】
- POJ 2631 Roads in the North(树的直径)
- poj--2631--Roads in the North(树的直径 裸模板)
- 【树的直径】 POJ 2631 Roads in the North
- 【POJ】-2631-Roads in the North(树的直径)
- POJ-2631 Roads in the North(最大路径)
- poj 2631 Roads in the North 【树的直径裸题】
- Roads in the North POJ - 2631(树的直径)
- POJ 2631 Roads in the North(树的直径)
- poj 2631 Roads in the North【树的直径裸题】
- 【树的直径】POJ 2631 Roads in the North
- poj2631 Roads in the North(求树的直径裸题)
- POJ-2631-Roads in the North-树的直径(最长路)
- poj 2631 Roads in the North (树的直径模板题)
- POJ 2631 Roads in the North 树的直径
- POJ 2631 Roads in the North (树的直径)