zoukankan      html  css  js  c++  java
  • Cow Marathon

    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
    

    Hint

    The longest marathon runs from farm 2 via roads 4, 1, 6 and 3 to farm 5 and is of length 20+3+13+9+7=52. 

    没什么难度  直接套模板

    #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,m;
    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=1;i<=n;i++)
    	{
    			if(ans<dist[i])
    		     {
    			    ans=dist[i];
    			    jilu=i;
    	          }
    	}
    		
    }
    int main()
    {
    
    	scanf("%d%d",&n,&m);
         	chushihua();
    		int a,b,c,d;
    		for(int i=1;i<=m;i++)
    		{
    			scanf("%d%d%d%c",&a,&b,&c,&d);
    			getchar();
    			Edge(a,b,c);
    			Edge(b,a,c);
    		}
    		bfs(1);bfs(jilu);
    		printf("%d
    ",ans);
    	return 0;
    }


    编程五分钟,调试两小时...
  • 相关阅读:
    复合表达式
    用DOM4J解析XML文件案例
    XPath可以快速定位到Xml中的节点或者属性。XPath语法很简单,但是强大够用,它也是使用xslt的基础知识。
    java base64编码和解码
    String空格删除和java删除字符串最后一个字符的几种方法
    java解析xml汇总
    XML解析——Java中XML的四种解析方式
    Java 读写Properties配置文件
    Spring + Mybatis 使用 PageHelper 插件分页
    Mybatis分页插件-PageHelper的使用
  • 原文地址:https://www.cnblogs.com/kingjordan/p/12027101.html
Copyright © 2011-2022 走看看