Longest Paths uva

来源:互联网 发布:thinkphp门户网站源码 编辑:程序博客网 时间:2024/04/29 12:58
 Longest Paths

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

Submit Status Practice UVA
10000

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 

211 20 0531 23 53 12 44 50 0555 15 25 35 44 14 20 00

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
原创粉丝点击