poj 2631 Roads in…
来源:互联网 发布:高清电子相册软件 编辑:程序博客网 时间:2024/05/09 13:47
就是给你一棵树,求其上距离最远的两点的距离 int next,w,to;
e[++lon].to=to; e[lon].w=w; e[lon].next=head[from]; head[from]=lon;
memset(text,0,sizeof(text)); memset(dist,0,sizeof(dist)); int front=1,end=1; que[1]=s; text[s]=1; while(front<=end) { int t=que[front++]; for(int k=head[t];k!=-1;k=e[k].next) { intu=e[k].to; if(text[u]==0) { text[u]=1; dist[u]=dist[t]+e[k].w; que[++end]=u; } } }
memset(head,-1,sizeof(head)); intfrom,to,w,n=10000; int timm=0; while(scanf("%d %d%d",&from,&to,&w)!=EOF) { timm++; edgemake(from,to,w); edgemake(to,from,w); } if(!timm) { printf("0\n"); return(0); } bfs(1); int t,tmp=0;
for(inti=1;i<=n;i++) if(dist[i]>tmp) { t=i; tmp=dist[i]; } bfs(t); tmp=0; for(inti=1;i<=n;i++) if(dist[i]>tmp) { t=i; tmp=dist[i]; } printf("%d\n",tmp); return 0;
有一个性质,就是先任意找一个点h求距离它最远的点s(距离为d[s]),然后从s出发找一个距离它最远的点t(距离d[t]),那么d[t]就是其直径。
原因,这么考虑,如果这一次求出来的不是直径
那么证明存在一条比它更长的路径,并且它的两个点到h的距离都小于d[s]。
接下来的情况不好说明,但是可以想象出来,用s节点去跟假设出来的直径中的两个点去构造,绝对可以构造出一个比假设的直径更长的路径(想象那两个点的各种位置)。所以假设不成立。
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
const int maxe=33333,maxn=11111;
struct
{
}e[maxe];
int head[maxn],lon;
int edgemake(int from,int to,int w)
{
}
int text[maxn],que[maxn*2],dist[maxn];
int ans;
int bfs(int s)
{
}
int main()
{
// freopen("in.txt","r",stdin);
}
- poj 2631 Roads in…
- poj 2421 Constructing Roads mst(…
- HDU 1025 Constructing Roads In …
- POJ 3625 Building Roads
- hdu 1157 Who's in the Middle(poj…
- MSCSLD : Error in…
- Check a loop in a list with line…
- Simulate and use real GPS in WM …
- Join语句的on 与 select xxx in…
- ZOJ 1060 (POJ 1094) Sorting It …
- POJ 1005 I Think I Need a Houseb…
- HDOJ 1301 Jungle Roads
- [solve]Bind: Address Already in …
- unrecognized selector sent to in…
- unrecognized selector sent to in…
- Improved logging in Objective-C …
- Command-line Processing in Korn …
- run Windows Mobile connecting in…
- ACM算法训练
- 管理者,你要懂这十大黄金管理法则
- poj 2240 Arbitrage spfa求正环
- acm题目分类
- poj 1324 Holedox Moving 宽搜
- poj 2631 Roads in…
- poj Light Up dfs
- poj 2288 Islands
- poj 2996 Help Me with the Game
- poj 1577 Falling Leaves
- poj 2418 Hardwood Species
- poj 2255 Tree Recovery
- poj 3349 Snowflake&nbs…
- poj 1840 Eqs 哈希