杂题 UVAoj 10000 Longest Paths
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的更多相关文章
- UVA 10000 Longest Paths (SPFA算法,模板题)
题意:给出源点和边,边权为1,让你求从源点出发的最长路径,求出路径长度和最后地点,若有多组,输出具有最小编号的最后地点. #include <iostream> #include < ...
- 杂题 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 ...
- 2016 10 26考试 NOIP模拟赛 杂题
Time 7:50 AM -> 11:15 AM 感觉今天考完后,我的内心是崩溃的 试题 考试包 T1: 首先看起来是个贪心,然而,然而,看到那个100%数据为n <= 2000整个人就虚 ...
- 贪心/构造/DP 杂题选做Ⅱ
由于换了台电脑,而我的贪心 & 构造能力依然很拉跨,所以决定再开一个坑( 前传: 贪心/构造/DP 杂题选做 u1s1 我预感还有Ⅲ(欸,这不是我在多项式Ⅱ中说过的原话吗) 24. P5912 ...
- 正睿OI DAY3 杂题选讲
正睿OI DAY3 杂题选讲 CodeChef MSTONES n个点,可以构造7条直线使得每个点都在直线上,找到一条直线使得上面的点最多 随机化算法,check到答案的概率为\(1/49\) \(n ...
- dp杂题(根据个人进度选更)
----19.7.30 今天又开了一个新专题,dp杂题,我依旧按照之前一样,这一个专题更在一起,根据个人进度选更题目; dp就是动态规划,本人认为,动态规划的核心就是dp状态的设立以及dp转移方程的推 ...
- wangkoala杂题总集(根据个人进度选更)
CQOI2014 数三角形 首先一看题,先容斥一波,求出网格内选三个点所有的情况,也就是C(n*m,3);然后抛出行里三点共线的方案数:C(n,3)*m; 同理就有列中三点共线的方案数:n*C(m,3 ...
- 2019暑期金华集训 Day6 杂题选讲
自闭集训 Day6 杂题选讲 CF round 469 E 发现一个数不可能取两次,因为1,1不如1,2. 发现不可能选一个数的正负,因为1,-1不如1,-2. hihoCoder挑战赛29 D 设\ ...
- Atcoder&CodeForces杂题11.7
Preface 又自己开了场CF/Atcoder杂题,比昨天的稍难,题目也更有趣了 昨晚炉石检验血统果然是非洲人... 希望这是给NOIP2018续点rp吧 A.CF1068C-Colored Roo ...
随机推荐
- 9.20 noip模拟试题
Problem 1 双色球(ball.cpp/c/pas) [题目描述] 机房来了新一届的学弟学妹,邪恶的chenzeyu97发现一位学弟与他同名,于是他当起了善良的学长233 “来来来,学弟,我 ...
- PL/SQL 批量SQL
批量SQL包括: FORALL语句 BULK COLLECT子句 FORALL语句 FORALL具有如下结构: FORALL loop_counter IN bounds_clause [SAVE E ...
- html 新元素
html5新元素 html5语义元素 <header> 定义了文档或者文档的一部分区域的页眉 <nav> 定义了导航链接的部分 <section> 定义了文档的某个 ...
- C# 二叉查找树实现
BuildTree 代码1次CODE完,没有BUG. 在画图地方debug了很多次.第一次画这种图. 一开始用treeview显示,但发现不是很好看出树结构,于是自己动手画了出来. using Sys ...
- android入门系列- TextView EditText Button ImageView 的简单应用
第一篇原创,其实自己就是一菜鸟,简单分享点基本知识吧.希望能有所帮助吧. TextView EditText Button ImageView 这几个控件可能是Android开发中最常用.最基本的几个 ...
- VS2015 Cordova Ionic移动开发(三)
一.基础设置 1.修改App名称和程序起始页 打开config.xml配置文件显示如下,在[通用]选项卡中,将显示名称和起始页,修改为自己想要的名称即可. 如需直接在xml文件中修改:右击config ...
- wpf 大控件 打印 将控件转成 xps格式 并分页打印
//PayRollPrintList:要打印的 list 可换成自己要打印的类型 private List<PayRoll> _PayRollPrintList = new List< ...
- 扩展欧几里得算法(extended Euclidean algorithm)的一个常犯错误
int exGcd(int x,int y,int& a,int& b) //ax+by=gcd(x,y) { ; b=; return x; } int res=exGcd(y,x% ...
- 【BZOJ3295】【块状链表+树状数组】动态逆序对
Description 对于序列A,它的逆序对数定义为满足i<j,且Ai>Aj的数对(i,j)的个数.给1到n的一个排列,按照某种顺序依次删除m个元素,你的任务是在每次删除一个元素之前统计 ...
- ThinkPHP框架下,给jq动态添加的标签添加点击事件移除标签
jq移除标签主要就是$("#要移除的id").remove();不再赘述,这里要提醒的是jq中动态添加标签后怎样添加点击事件.一般的jq添加点击事件是用这种方法$("#i ...