• poj 1985 Cow Marathon【树的直径裸题】


    Cow Marathon
    Time Limit: 2000MS   Memory Limit: 30000K
    Total Submissions: 4185   Accepted: 2118
    Case Time Limit: 1000MS

    Description

    After hearing about the epidemic of obesity in the USA, Farmer John wants his cows to get more exercise, so he has committed to create a bovine marathon for his cows to run. The marathon route will include a pair of farms and a path comprised of a sequence of roads between them. Since FJ wants the cows to get as much exercise as possible he wants to find the two farms on his map that are the farthest apart from each other (distance being measured in terms of total length of road on the path between the two farms). Help him determine the distances between this farthest pair of farms. 

    Input

    * Lines 1.....: Same input format as "Navigation Nightmare".

    Output

    * Line 1: An integer giving the distance between the farthest pair of farms. 

    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
    昨天比赛遇到一个树的直径的题 不知道是什么 唉 坑啊,今天赶紧看看
    题意:N个点M条边,每条边都有权值,让你找到两点,使得这两点间所有边的权值之和最长,
    (这条路径上任意节点不能有岔路(此处不能有岔路指的是岔路上的边的权值不能算上))
    题解:运用两次bfs求最长距离 第一次先任选一点,找到这一点可以到达的最长距离的节点u处
    然后再以u点为起点,找到最长路
    #include<stdio.h>
    #include<string.h>
    #include<queue>
    #define MAX 100000
    using namespace std;
    int head[MAX];
    int vis[MAX],dis[MAX];
    int n,m,ans;
    int sum,aga;
    char s[30];
    struct node
    {
    	int u,v,w;
    	int next;
    }edge[MAX];
    
    void add(int u,int v,int w)
    {
    	edge[ans].u=u;
    	edge[ans].v=v;
    	edge[ans].w=w;
    	edge[ans].next=head[u];
    	head[u]=ans++;
    }
    void getmap()
    {
    	int i,j;
    	int a,b,c;
    	ans=0;
    	memset(head,-1,sizeof(head));
        while(m--)
        {
        	scanf("%d%d%d%s",&a,&b,&c,s);
        	add(a,b,c);
        	add(b,a,c);
    	}
    }
    
    
    void bfs(int beg)
    {
    	queue<int>q;
    	memset(dis,0,sizeof(dis));
    	memset(vis,0,sizeof(vis));
    	int i,j;
    	while(!q.empty())
    	    q.pop();
    	aga=beg;
    	sum=0;
    	vis[beg]=1;
    	q.push(beg);
    	int top;
    	while(!q.empty())
    	{
    		top=q.front();
    		q.pop();
    		for(i=head[top];i!=-1;i=edge[i].next)
    		{
    			if(!vis[edge[i].v])
    			{
    				dis[edge[i].v]=dis[top]+edge[i].w;
    				vis[edge[i].v]=1;
    				q.push(edge[i].v);
    			}
    		}
    	}
    	
    	for(i=1;i<=n;i++)
    	{
    		if(sum<dis[i])
    		{
    			sum=dis[i];
    			aga=i;
    		}
    	}
    }
    int main()
    {
    	while(scanf("%d%d",&n,&m)!=EOF)
    	{
    		getmap();
    		bfs(1);
    		bfs(aga);
    		printf("%d
    ",sum);
    	}
    	return 0;
    }
    

      



  • 相关阅读:
    面试6 在c#中如何声明一个类不能被继承
    面试5 如何理解静态变量,局部变量,全局变量
    面试4 你在什么情况下会用到虚方法?它与接口有什么不同
    面试3 不用系统自带的方法将字符串类型123456转换为值类型
    面试2 递归的算法求1,1,2,3,5,8.......的第30位数是多少,然后求这些数的和.
    面试1 SQL SERVER 查询第20行到30之间的数据
    ubuntu安装nginx
    ubuntu上使用ufw配置管理防火墙
    ubuntu上安装docker
    ubuntu上使用vim编辑文本内容
  • 原文地址:https://www.cnblogs.com/tonghao/p/4738445.html
Copyright © 2020-2023  润新知