解题报告

求最长路。

用SPFA求最长路,初始化图为零,dis数组也为零

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#define inf 99999999
#define N 110
using namespace std;
int mmap[N][N],dis[N],vis[N],n;
void spfa(int s)
{
int i;
memset(dis,0,sizeof(dis));
memset(vis,0,sizeof(vis));
queue<int>Q;
vis[s]=1;
Q.push(s);
while(!Q.empty())
{
int u=Q.front();
Q.pop();
vis[u]=0;
for(i=1; i<=n; i++)
{
if(mmap[u][i])
if(dis[i]<dis[u]+1)
{
dis[i]=dis[u]+1;
if(!vis[i])
{
vis[i]=1;
Q.push(i);
}
}
}
}
}
int main()
{
int u,v,s,i,j,k=1;
while(~scanf("%d",&n))
{
if(!n)break;
scanf("%d",&s);
memset(dis,0,sizeof(dis));
memset(vis,0,sizeof(vis));
memset(mmap,0,sizeof(mmap));
while(~scanf("%d%d",&u,&v))
{
if(!u&&!v)break;
mmap[u][v]=1;
}
spfa(s);
int maxx=0,u=1000;
for(i=1; i<=n; i++)
{
if(maxx<dis[i])
maxx=dis[i];
}
for(i=1;i<=n;i++)
{
if(maxx==dis[i]&&u>i)
u=i;
}
printf("Case %d: The longest path from %d has length %d, finishing at %d.\n\n",k++,s,maxx,u);
}
return 0;
}

 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 qafter 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.

UVa10000_Longest Paths(最短路SPFA)的更多相关文章

  1. 最短路模板(Dijkstra & Dijkstra算法+堆优化 & bellman_ford & 单源最短路SPFA)

    关于几个的区别和联系:http://www.cnblogs.com/zswbky/p/5432353.html d.每组的第一行是三个整数T,S和D,表示有T条路,和草儿家相邻的城市的有S个(草儿家到 ...

  2. L - Subway(最短路spfa)

    L - Subway(最短路spfa) You have just moved from a quiet Waterloo neighbourhood to a big, noisy city. In ...

  3. 【POJ】3255 Roadblocks(次短路+spfa)

    http://poj.org/problem?id=3255 同匈牙利游戏. 但是我发现了一个致命bug. 就是在匈牙利那篇,应该dis2单独if,而不是else if,因为dis2和dis1相对独立 ...

  4. POJ 3255 Roadblocks (次短路 SPFA )

    题目链接 Description Bessie has moved to a small farm and sometimes enjoys returning to visit one of her ...

  5. ACM/ICPC 之 最短路-SPFA+正逆邻接表(POJ1511(ZOJ2008))

    求单源最短路到其余各点,然后返回源点的总最短路长,以构造邻接表的方法不同分为两种解法. POJ1511(ZOJ2008)-Invitation Cards 改变构造邻接表的方法后,分为两种解法 解法一 ...

  6. POJ 1847 Tram --set实现最短路SPFA

    题意很好懂,但是不好下手.这里可以把每个点编个号(1-25),看做一个点,然后能够到达即为其两个点的编号之间有边,形成一幅图,然后求最短路的问题.并且pre数组记录前驱节点,print_path()方 ...

  7. 【wikioi】1269 匈牙利游戏(次短路+spfa)

    http://www.wikioi.com/problem/1269/ 噗,想不到.. 次短路就是在松弛的时候做下手脚. 设d1为最短路,d2为次短路 有 d1[v]>d1[u]+w(u, v) ...

  8. POJ 1511 最短路spfa

    题很简单 就是有向图中求给出的源点到其余所有点的最短路的和与其余所有点到源点的最短路之和 一开始以为dij对于正权图的单源最短路是最快的 写了一发邻接表的dij 结果超时 把所有的cin改成scanf ...

  9. Layout---poj3169(差分约束+最短路spfa)

    题目链接:http://poj.org/problem?id=3169 有n头牛站成一排 在他们之间有一些牛的关系比较好,所以彼此之间的距离不超过一定距离:也有一些关系不好的牛,希望彼此之间的距离大于 ...

随机推荐

  1. hdu1503(最长公共子序列)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1503 题意:由两个字符串构造出另一个字符串,该字符串包含前两个字符串(按字符顺序,但不一定连续),使该 ...

  2. 不可不知的DIP、IoC、DI以及IoC容器

    面向对象设计(OOD)有助于我们开发出高性能.易扩展以及易复用的程序.当中.OOD有一个重要的思想那就是依赖倒置原则(DIP),并由此引申出IoC.DI以及Ioc容器等概念. 本文首先用实例阐述四个概 ...

  3. SQLServer 复制中移除和加入公布而不初始化全部项目

    -- 若提前"禁止架构更改".新增的列不会自己主动加入大公布.此时应使用 sp_articlecolumn 加入列 EXEC sp_changepublication @publi ...

  4. Spring相框

    1.什么是Spring相框?Spring有哪些主要模块框架? Spring框架是一个为Java应用程序的开发提供了综合.广泛的基础性支持的Java平台. Spring帮助开发人员攻克了开发中基础性的问 ...

  5. HDU 3830 Checkers

    意甲冠军: 有三件  所有其他棋子可以跳  不能分开的两个跳跃  当被问及状态u为了国家v最低短跳转 思路: 对于一个状态三个棋子的位置能够设为 x y z  (小到大) 仅仅有当y-x=z-y的时候 ...

  6. 让Linux开机运行命令

    开机的时候需要linux 自动执行命令很简单 只需要把要执行的命令输入操作系统启动的时候要加载的文件里面就行了,一般写在 /etc/rc.local里面 #vim /etc/rc.local 按o键  ...

  7. poj 3280 Cheapest Palindrome ---(DP 回文串)

    题目链接:http://poj.org/problem?id=3280 思路: dp[i][j] :=第i个字符到第j个字符之间形成回文串的最小费用. dp[i][j]=min(dp[i+1][j]+ ...

  8. 文件下载-SpringMVC中測试

    直接改动文件路径就能够.其它都不须要改动,帮助类已经为大家写好,可直接使用 1.Scroller: /** * 下载文件 * @author liupeng * @param request * @p ...

  9. Java 并发编程(三)为线程安全类中加入新的原子操作

    Java 类库中包括很多实用的"基础模块"类.通常,我们应该优先选择重用这些现有的类而不是创建新的类.:重用能减少开发工作量.开发风险(由于现有类都已经通过測试)以及维护成本.有时 ...

  10. windows phone (25) Canvas元素B

    原文:windows phone (25) Canvas元素B  ZIndex 这也是一个附加属性,表示canvas的children集合内的子元素的显示顺序,在canvas中的元素默认情况下是后面的 ...