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. HTML select 操作

    今天遇到一个问题,就是想设置select的默认选择项.但是试了很多方法都不行: <fieldset data-role="contractstatus"> <la ...

  2. webGIS(离线版)研究路线归总

    特注:不做详解,说明网上资源很多,找一篇,照着走一遍即可. 1.数据源要满足开源.Free且地理信息要完善 几经周折,选择了OSM,具体信息可以去其官方查看(它竟然把中国一分为二,大陆.台湾,坑爹!! ...

  3. 表达式:使用API创建表达式树(6)

    一.ConstantExpression:表示具有常量值的表达式.因为表达式应用过程中,参数据多是 Expressions 类型,算是对常量值的一种包装吧. ConstantExpression使用比 ...

  4. TFS 服务器更换后工作区无法绑定

    需要删除工作区,删除命令如下 tf workspace /delete 工作区名;创建的用户 /server:TFS服务器 例 tf workspace /delete WHQ-PC;whq /ser ...

  5. mysql出现的错误

    (一)ERROR 1005 (HY000): Can't create table '.\day19\user_role.frm' (errno: 121) 今天遇到的这个问题是因为创建了五张表,其中 ...

  6. wpf 自定义RadioButton控件样式

    实现的效果为: 我感觉来自定义RadioButton样式和定义button空间的样式差不多,只是类型不同而已. 接下来分析一下样式代码: <!--自定义单选按钮样式-->        & ...

  7. cocos2dx 实现华丽丽的滚动层.

    前言 好久没写博客了. 前几周策划要求实现一个比较多功能的滚动层控件. 这个艰巨的任务就这样自然而然的落在了我这小身板上. 当然了, 只要我出手, 难度再高的需求也变得不堪一击. 哈哈哈哈 示例图 该 ...

  8. asp.net尽量不在js里写<%%>

    asp.net尽量不在js里写<%%> eg: <script type="text/javascript"> var rootsid="&quo ...

  9. c++文件读写相关

    在看C++编程思想中,每个练习基本都是使用ofstream,ifstream,fstream,以前粗略知道其用法和含义,在看了几位大牛的博文后,进行整理和总结: 这里主要是讨论fstream的内容: ...

  10. Javascript中null值,特别注意的两点

    null 是一个javascript字面量,表示空值,就是没有对象被呈现.他是javascript原始值之一.null值常被放在期望一个对象上,但是不引用任何对象的参数位置,也就是说对象的初始化. 我 ...