zoukankan      html  css  js  c++  java
  • Farthest Nodes in a Tree

    Description

    Given a tree (a connected graph with no cycles), you have to find the farthest nodes in the tree. The edges of the tree are weighted and undirected. That means you have to find two nodes in the tree whose distance is maximum amongst all nodes.

    Input

    Input starts with an integer T (≤ 10), denoting the number of test cases.

    Each case starts with an integer n (2 ≤ n ≤ 30000) denoting the total number of nodes in the tree. The nodes are numbered from 0 to n-1. Each of the next n-1 lines will contain three integers u v w (0 ≤ u, v < n, u ≠ v, 1 ≤ w ≤ 10000) denoting that node u and v are connected by an edge whose weight is w. You can assume that the input will form a valid tree.

    Output

    For each case, print the case number and the maximum distance.

    Sample Input

    2

    4

    0 1 20

    1 2 30

    2 3 50

    5

    0 2 20

    2 1 10

    0 3 29

    0 4 50

    Sample Output

    Case 1: 100

    Case 2: 80

    直接套模板   

    #include<cstdio>
    #include<queue>
    #include<cstring>
    using namespace std;
    struct node 
    {
    	int from,to,val,next;
    };
    node dian[1000001];
    int cut;
    int head[100001];
    void chushihua()
    {
    	cut=0;
    	memset(head,-1,sizeof(head));
    }
    
    void  Edge(int u,int v,int w)
    {
    	dian[cut].from =u;
    	dian[cut].to =v;
    	dian[cut].val =w;
    	dian[cut].next =head[u];
    	head[u]=cut++;
    }
    int jilu;
    int vis[100001];
    int dist[100001];
    int ans;
    int n;
    void bfs(int s)
    {
    memset(vis,0,sizeof(vis));
    memset(dist,0,sizeof(dist));
    	ans=0;
    	queue<int>q;
    	q.push(s);vis[s]=1;dist[s]=0;
    	while(!q.empty() )
    	{
    		int u=q.front() ;
    		q.pop() ;
    		for(int i=head[u];i!=-1;i=dian[i].next )
    		{
    			int v=dian[i].to ;
    			if(!vis[v])
    			{
    				if(dist[v]<dist[u]+dian[i].val )
    				dist[v]=dist[u]+dian[i].val ;
    					vis[v]=1;
    		            q.push(v); 	
    			}
    		}
         }
    	for(int i=0;i<n;i++)
    	{
    			if(ans<dist[i])
    		     {
    			    ans=dist[i];
    			    jilu=i;
    	          }
    	}
    		
    }
    int main()
    {
    	int t,k=0;
    	scanf("%d",&t);
    	while(t--)
    	{
    		k++;
    		chushihua();
    		int a,b,c;
    		scanf("%d",&n);
    		for(int i=1;i<n;i++)
    		{
    			scanf("%d%d%d",&a,&b,&c);
    			Edge(a,b,c);
    			Edge(b,a,c);
    		}
    		bfs(0);bfs(jilu);
    		printf("Case %d: %d
    ",k,ans);
    	}
    	return 0;
    }


  • 相关阅读:
    IE6 跟随滚动解决方法
    CentOS 7 用户怎样安装 LNMP(Nginx+PHP+MySQL)
    [ConcurrencyCheck]并发检查
    centos7下修改docker工作目录
    kubernetes 1.14安装部署helm插件
    kubernetes 1.14安装部署EFK日志收集系统
    kubernetes 1.14安装部署dashboard
    kubernetes 1.14安装部署metrics-server插件
    calico客户端工具calicoctl
    centos7使用kubeadm安装部署kubernetes 1.14
  • 原文地址:https://www.cnblogs.com/kingjordan/p/12027097.html
Copyright © 2011-2022 走看看