Longest Paths uva

 Longest Paths

Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu

Description

Download as PDF


  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.
 
   
 
   
 
   
 
   

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <iostream>
using namespace std;
const int maxn=110;
/*
spfa 算法 int n;  //表示n个点,从1到n标号


int s,t;  //s为源点,t为终点


int d[N];  //d[i]表示源点s到点i的最短路


int p[N];  //记录路径(或者说记录前驱)


queue <int> q;  //一个队列,用STL实现,当然可有手打队列,无所谓


bool vis[N];   //vis[i]=1表示点i在队列中 vis[i]=0表示不在队列中
void spfa
{
队列初始化为空;
所有的d【】初始化为无穷大,
p的前驱初始化为 s;
d【s】=0;
入队时vis【x】=1;
出队时vis【x】=0;


入队+松弛
读取队头顶点u,并将队头顶点u出队(记得消除标记);将与点u相连的所有点v进行松弛操作,如果能更新估计值(即令d[v]变小),
那么就更新,另外,如果点v没有在队列中,那么要将点v入队(记得标记),如果已经在队列中了,那么就不用入队


每次的松弛操作会使结果不断靠近终点,没有环的话不会陷入死循环


更详细参考 网页 http://www.cnblogs.com/scau20110726/archive/2012/11/18/2776124.html
}
*/
int n,x,y,st,en,length,l,r,a[maxn][maxn],dis[maxn],h[100*maxn];
//a[][]表示x,y之间是否有路径 h是手打的队列,啊哈!尴尬
bool v[maxn];
int main()
{
    int j=1;
   while(~scanf("%d",&n)&&n)
   {
       en=1000;
       memset(a,0,sizeof(a));
       memset(v,0,sizeof(v));
       memset(dis,0,sizeof(dis));
       memset(h,0,sizeof(h));
       length=-1;
       scanf("%d",&st);
       while(~scanf("%d%d",&x,&y)&&x&&y)a[x][y]=1;
    l=r=1;
     h[1]=st;
      v[st]=1;
     while(1)
     {
         int x=h[l];
        for( y=1;y<=n;y++)
         {


             if(a[x][y])if(dis[x]+1>dis[y])
             {
                 dis[y]=dis[x]+1;
                 if(!v[y])
                 {
                     h[++r]=y;
                     v[y]=1;
                 }
             }
         }
         printf("%d ",x);
         if(l==r)break;
         l++;
         v[x]=0;
     }




       for(int i=1;i<=n;i++)
        if(dis[i]>length)length=dis[i],en=i;
       printf("Case %d: The longest path from %d has length %d, finishing at %d.\n",j++,st,length,en);
   }
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值