10000-Longest Paths
来源:互联网 发布:西游记知大圣归来 编辑:程序博客网 时间:2024/06/04 08:49
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 numbern () 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 <iostream>#include <cstdlib>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int maxn=100+10;const int INF=-1;int map[maxn][maxn];int n,res;int src,target;inline int max(int a,int b){ return a>b?a:b;}inline int min(int a,int b){ return a<b?a:b;}inline void init(){ for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) { if(i!=j) map[i][j]=INF; else map[i][j]=0; } }}inline void Floyd(){ for(int k=1;k<=n;k++) { for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) { if(map[i][k]==INF||map[k][j]==INF) continue; if(map[i][j]==INF||map[i][k]+map[k][j]>map[i][j]) map[i][j]=map[i][k]+map[k][j]; } } }}inline void solve(){ res=0; for(int i=1;i<=n;i++) { if(map[src][i]>res) { res=map[src][i];//修改最长距离和目的地; target=i; } }}int main(){ int test=1; while(~scanf("%d",&n)&&n) { scanf("%d",&src); init(); int u,v; while(~scanf("%d%d",&u,&v)) { if(u==0&&v==0) break; map[u][v]=1; } Floyd(); solve(); printf("Case %d: The longest path from %d has length %d, finishing at %d.\n\n",test++,src,res,target); } return 0;}
- 10000-Longest Paths
- UVa 10000 - Longest Paths
- UVA 10000 - Longest Paths
- Uva Oj 10000 - Longest Paths
- uva 10000 Longest Paths (SPFA)
- UVA 10000 Longest Paths(SPFA)
- uva10000 Longest Paths
- Longest Paths uva
- uva 10000 - Longest Paths(bellman_ford算法过)
- UVA 10000 -Longest Paths-Bellman - Ford,flody,spfa
- UVa:10000 Longest Paths (DAG上的最长路)
- UVA-10000 Longest Paths (SPFA最短路算法)
- UVA 10000 Longest Paths 最简单的最长路
- UVa 10000 Longest Paths (单源最长路 - floyd or 拓扑排序)
- UVa10000 - Longest Paths(为什么是WA)
- uva 11823 - Two Longest Paths (最小费用流)
- Paths
- Paths
- 基于WINDOWS OS ANT + NSIS + BAT + SVN 环境,ANT 一键安装制作 JAVA WEB + TOMCAT7.0 EXE 安装包
- 一字节16进制
- 网路IO详解
- hdu 3713 Double Maze(四维的BFS)
- 字节数组转16进制
- 10000-Longest Paths
- 10进制与16进制互相转换
- 在iis中打开网站提示:http 500 内部服务器错误
- 第八周项目3——加班费
- 摄氏度到华氏度得转换
- 模糊逻辑与逻辑推理
- 海盗分赃问题
- C++读写XML文件(libxml2库)
- poj 3070 Fibonacci