zoukankan      html  css  js  c++  java
  • 杂题 UVAoj 10000 Longest Paths

      Longest Paths 

    It is a well known fact that some people do not have their social abilities completely enabled. One example is the lack of talent for calculating distances and intervals of time. This causes some people to always choose the longest way to go from one place to another, with the consequence that they are late to whatever appointments they have, including weddings and programming contests. This can be highly annoying for their friends.

    César has this kind of problem. When he has to go from one point to another he realizes that he has to visit many people, and thus always chooses the longest path. One of César's friends, Felipe, has understood the nature of the problem. Felipe thinks that with the help of a computer he might be able to calculate the time that César is going to need to arrive to his destination. That way he could spend his time in something more enjoyable than waiting for César.


    Your goal is to help Felipe developing a program that computes the length of the longest path that can be constructed in a given graph from a given starting point (César's residence). You can assume that the graph has no cycles (there is no path from any node to itself), so César will reach his destination in a finite time. In the same line of reasoning, nodes are not considered directly connected to themselves.

    Input 

    The input consists of a number of cases. The first line on each case contains a positive number n ( $1 < n le 100$) that specifies the number of points that César might visit (i.e., the number of nodes in the graph).

    A value of n = 0 indicates the end of the input.


    After this, a second number s is provided, indicating the starting point in César's journey ( $1 le s le n$). Then, you are given a list of pairs of places p and q, one pair per line, with the places on each line separated by white-space. The pair ``$p  q$" indicates that César can visit q after p.

    A pair of zeros (``0 0") indicates the end of the case.


    As mentioned before, you can assume that the graphs provided will not be cyclic.

    Output 

    For each test case you have to find the length of the longest path that begins at the starting place. You also have to print the number of the final place of such longest path. If there are several paths of maximum length, print the final place with smallest number.


    Print a new line after each test case.

    Sample Input 

    2
    1
    1 2
    0 0
    5
    3
    1 2
    3 5
    3 1
    2 4
    4 5
    0 0
    5
    5
    5 1
    5 2
    5 3
    5 4
    4 1
    4 2
    0 0
    0
    

    Sample Output 

    Case 1: The longest path from 1 has length 1, finishing at 2.
    
    Case 2: The longest path from 3 has length 4, finishing at 5.
    
    Case 3: The longest path from 5 has length 2, finishing at 1.


      水题啊,需要注意每个答案后带个回车!
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cmath>
     5 #include <algorithm>
     6 using namespace std;
     7 const int maxn=510;
     8 int cnt,dis[maxn],q[maxn*10],e[maxn][maxn];
     9 
    10 int ed,n;
    11 int vis[maxn];
    12 void BFS(int node,int d)
    13 {
    14     int front=1,back=1;
    15     q[back++]=node;
    16     vis[node]=true;
    17     while(front<back)
    18     {
    19         node=q[front++];vis[node]=false;
    20         for(int i=1;i<=n;i++)
    21             if(e[node][i])
    22                 if(dis[i]<dis[node]+1){
    23                     dis[i]=dis[node]+1;
    24                     if(!vis[i])q[back++]=i;
    25                     vis[i]=true;
    26                 }
    27     }
    28     for(int i=1;i<=n;i++)
    29         if(ed==-1||dis[i]>dis[ed])
    30             ed=i;
    31     return;    
    32 }
    33 int main()
    34 {
    35     int a,b,Case=0,st;
    36     while(scanf("%d",&n)==1&&n)
    37     {
    38         scanf("%d",&st);
    39         for(int i=1;i<=n;i++)dis[i]=0;cnt=0;
    40         memset(e,0,sizeof(e));
    41         while(~scanf("%d%d",&a,&b)&&a&&b)e[a][b]=1;
    42         ed=-1;
    43         BFS(st,0);
    44         
    45         printf("Case %d: The longest path from %d has length %d, finishing at %d.
    
    ",++Case,st,dis[ed],ed);
    46     }
    47     
    48     return 0;
    49 }

    尽最大的努力,做最好的自己!
  • 相关阅读:
    eclipse- DDMS截图功能使用
    宏-新项目物理按键不能用
    宏-宏的添加跟代码中的使用
    SQlite-数据库的访问实例(转)
    git 工具的使用总结(6)-提交合并处理
    git 工具的使用总结(5)-查看历史记录
    git -处理分支合并
    Linux查询网址
    SQLite常用网址
    Java查询网址
  • 原文地址:https://www.cnblogs.com/TenderRun/p/5250021.html
Copyright © 2011-2022 走看看