Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 2389 | Accepted: 1173 |
Description
Input
Output
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
#include<stdio.h> #include<string.h> #include<queue> #include<algorithm> using namespace std; #define MAXN 10010 int Node,m,n,cnt,ans; int head[MAXN],dis[MAXN],vis[MAXN],pre[MAXN]; struct node { int u,v,val; int next; }edge[MAXN]; void init() { memset(head,-1,sizeof(head)); cnt=0; for(int i=0;i<10010;i++) pre[i]=i; } void add(int u,int v,int val) { node E={u,v,val,head[u]}; edge[cnt]=E; head[u]=cnt++; } void bfs(int sx) { memset(vis,0,sizeof(vis)); memset(dis,0,sizeof(dis)); ans=0; queue<int>q; Node=sx; q.push(Node); vis[Node]=1; while(!q.empty()) { int u=q.front(); q.pop(); for(int i=head[u];i!=-1;i=edge[i].next) { node E=edge[i]; if(!vis[E.v]) { vis[E.v]=1; dis[E.v]=dis[u]+E.val; q.push(E.v); } } } for(int i=1;i<=cnt;i++) { if(ans<dis[i]) { ans=dis[i]; Node=i; } } } void slove() { bfs(1); bfs(Node); printf("%d\n",ans); } int main() { int a,b,c; init(); while(scanf("%d%d%d",&a,&b,&c)!=EOF) { add(a,b,c); add(b,a,c); } slove(); return 0; }