Longest Paths uva
来源:互联网 发布:thinkphp门户网站源码 编辑:程序博客网 时间:2024/04/29 12:58
Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu
Description
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 ( )
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 ( ). 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 ``"
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;}
- UVa 10000 - Longest Paths
- UVA 10000 - Longest Paths
- Longest Paths uva
- Uva Oj 10000 - Longest Paths
- uva 10000 Longest Paths (SPFA)
- UVA 10000 Longest Paths(SPFA)
- uva 10000 - Longest Paths(bellman_ford算法过)
- UVA 10000 -Longest Paths-Bellman - Ford,flody,spfa
- UVa:10000 Longest Paths (DAG上的最长路)
- uva 11823 - Two Longest Paths (最小费用流)
- UVA-10000 Longest Paths (SPFA最短路算法)
- UVA 10000 Longest Paths 最简单的最长路
- 10000-Longest Paths
- uva10000 Longest Paths
- UVa 10000 Longest Paths (单源最长路 - floyd or 拓扑排序)
- uva 125 - Numbering Paths
- UVA - 125 Numbering Paths
- UVA 125 Numbering Paths
- javaScript:后台管理功能
- UML之类图
- Xcode 真机调试报错:This application's application-identifier entitleme
- iOS NSTimer释放不掉的问题
- 查看oracle数据库的数据文件及控制文件存放位置
- Longest Paths uva
- 这是一个测试信息
- 基于stm32F1的过采样技术研究与实现
- dup 与 dup2
- 安卓智慧上海学习笔记系列——measure()、getMeasuredHeight()的使用
- mysql性能
- Unreal中代码添加一个触发器
- Android Studio 百度地图开发——环境搭建
- 测试vs2013对c99的支持情况和解决“将此类型用作表达式非法”的问题