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

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 <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
const int maxn=;
int cnt,dis[maxn],q[maxn*],e[maxn][maxn]; int ed,n;
int vis[maxn];
void BFS(int node,int d)
{
int front=,back=;
q[back++]=node;
vis[node]=true;
while(front<back)
{
node=q[front++];vis[node]=false;
for(int i=;i<=n;i++)
if(e[node][i])
if(dis[i]<dis[node]+){
dis[i]=dis[node]+;
if(!vis[i])q[back++]=i;
vis[i]=true;
}
}
for(int i=;i<=n;i++)
if(ed==-||dis[i]>dis[ed])
ed=i;
return;
}
int main()
{
int a,b,Case=,st;
while(scanf("%d",&n)==&&n)
{
scanf("%d",&st);
for(int i=;i<=n;i++)dis[i]=;cnt=;
memset(e,,sizeof(e));
while(~scanf("%d%d",&a,&b)&&a&&b)e[a][b]=;
ed=-;
BFS(st,); printf("Case %d: The longest path from %d has length %d, finishing at %d.\n\n",++Case,st,dis[ed],ed);
} return ;
}
												

杂题 UVAoj 10000 Longest Paths的更多相关文章

  1. UVA 10000 Longest Paths (SPFA算法,模板题)

    题意:给出源点和边,边权为1,让你求从源点出发的最长路径,求出路径长度和最后地点,若有多组,输出具有最小编号的最后地点. #include <iostream> #include < ...

  2. 杂题 UVAoj 107 The Cat in the Hat

     The Cat in the Hat  Background (An homage to Theodore Seuss Geisel) The Cat in the Hat is a nasty c ...

  3. 2016 10 26考试 NOIP模拟赛 杂题

    Time 7:50 AM -> 11:15 AM 感觉今天考完后,我的内心是崩溃的 试题 考试包 T1: 首先看起来是个贪心,然而,然而,看到那个100%数据为n <= 2000整个人就虚 ...

  4. 贪心/构造/DP 杂题选做Ⅱ

    由于换了台电脑,而我的贪心 & 构造能力依然很拉跨,所以决定再开一个坑( 前传: 贪心/构造/DP 杂题选做 u1s1 我预感还有Ⅲ(欸,这不是我在多项式Ⅱ中说过的原话吗) 24. P5912 ...

  5. 正睿OI DAY3 杂题选讲

    正睿OI DAY3 杂题选讲 CodeChef MSTONES n个点,可以构造7条直线使得每个点都在直线上,找到一条直线使得上面的点最多 随机化算法,check到答案的概率为\(1/49\) \(n ...

  6. dp杂题(根据个人进度选更)

    ----19.7.30 今天又开了一个新专题,dp杂题,我依旧按照之前一样,这一个专题更在一起,根据个人进度选更题目; dp就是动态规划,本人认为,动态规划的核心就是dp状态的设立以及dp转移方程的推 ...

  7. wangkoala杂题总集(根据个人进度选更)

    CQOI2014 数三角形 首先一看题,先容斥一波,求出网格内选三个点所有的情况,也就是C(n*m,3);然后抛出行里三点共线的方案数:C(n,3)*m; 同理就有列中三点共线的方案数:n*C(m,3 ...

  8. 2019暑期金华集训 Day6 杂题选讲

    自闭集训 Day6 杂题选讲 CF round 469 E 发现一个数不可能取两次,因为1,1不如1,2. 发现不可能选一个数的正负,因为1,-1不如1,-2. hihoCoder挑战赛29 D 设\ ...

  9. Atcoder&CodeForces杂题11.7

    Preface 又自己开了场CF/Atcoder杂题,比昨天的稍难,题目也更有趣了 昨晚炉石检验血统果然是非洲人... 希望这是给NOIP2018续点rp吧 A.CF1068C-Colored Roo ...

随机推荐

  1. ASP.NET 动态属性筛选和分页绑定

    分页控件为:AspNetPager.dll 我们先建立一个产品属性名称表 CREATE TABLE ProductAttr ( ,) NOT NULL primary key, [ParentID] ...

  2. java验证码Captcha

    import java.awt.Color; import java.io.IOException; import java.util.Random; import javax.servlet.htt ...

  3. JAAS - Document

    JAAS 参考文档: JAAS Reference Guide JAAS Authentication Tutorial JAAS Authorization Tutorial LoginModule ...

  4. maven jetty运行命令

    1.先运行build.xml <?xml version="1.0" encoding="UTF-8"?> <project name=&qu ...

  5. 监听polygon变化

    Polygons are made of Paths which are just MVCArrays (in this case, they're a list of LatLng objects) ...

  6. jdbc mysql - Column count doesn't match value count at row 1.

    该句的意思是,insert操作的SQL语句里列的数目和后面值的数目不一致.比如说, String sql = "insert into t_aqi(city_name, cur_date, ...

  7. What's the use of @ before the path defination

    When you define your path const : const string Child_Medical_Pdf = @"~/Media/ChildPdf"; Th ...

  8. Linux命令:scp命令(文件上传和下载)

    #本地下载远端文件 并且重命名(从本地机器下载远端) scp webmaster@10.10.65.103:/ROOT/logs/tomcate.log /home/dajie/mywork/newn ...

  9. 基于CDH5.x 下面使用eclipse 操作hive 。使用java通过jdbc连接HIVESERVICE 创建表

    基于CDH5.x 下面使用eclipse 操作hive .使用java通过jdbc连接HIVESERVICE 创建表 import java.sql.Connection; import java.s ...

  10. JDBC标准事物编程模式

    事物简介: 事物是一种数据库中保证交易可靠的机制,JDBC支持数据库中事物的概念,默认情况下事物是默认提交的. 事物的特性: 1.事物必须是原子工作单元,对于其数据的修改,要么都执行,要么都不执行2. ...