UVa10000_Longest Paths(最短路SPFA)
解题报告
求最长路。
用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)的更多相关文章
- 最短路模板(Dijkstra & Dijkstra算法+堆优化 & bellman_ford & 单源最短路SPFA)
关于几个的区别和联系:http://www.cnblogs.com/zswbky/p/5432353.html d.每组的第一行是三个整数T,S和D,表示有T条路,和草儿家相邻的城市的有S个(草儿家到 ...
- L - Subway(最短路spfa)
L - Subway(最短路spfa) You have just moved from a quiet Waterloo neighbourhood to a big, noisy city. In ...
- 【POJ】3255 Roadblocks(次短路+spfa)
http://poj.org/problem?id=3255 同匈牙利游戏. 但是我发现了一个致命bug. 就是在匈牙利那篇,应该dis2单独if,而不是else if,因为dis2和dis1相对独立 ...
- POJ 3255 Roadblocks (次短路 SPFA )
题目链接 Description Bessie has moved to a small farm and sometimes enjoys returning to visit one of her ...
- ACM/ICPC 之 最短路-SPFA+正逆邻接表(POJ1511(ZOJ2008))
求单源最短路到其余各点,然后返回源点的总最短路长,以构造邻接表的方法不同分为两种解法. POJ1511(ZOJ2008)-Invitation Cards 改变构造邻接表的方法后,分为两种解法 解法一 ...
- POJ 1847 Tram --set实现最短路SPFA
题意很好懂,但是不好下手.这里可以把每个点编个号(1-25),看做一个点,然后能够到达即为其两个点的编号之间有边,形成一幅图,然后求最短路的问题.并且pre数组记录前驱节点,print_path()方 ...
- 【wikioi】1269 匈牙利游戏(次短路+spfa)
http://www.wikioi.com/problem/1269/ 噗,想不到.. 次短路就是在松弛的时候做下手脚. 设d1为最短路,d2为次短路 有 d1[v]>d1[u]+w(u, v) ...
- POJ 1511 最短路spfa
题很简单 就是有向图中求给出的源点到其余所有点的最短路的和与其余所有点到源点的最短路之和 一开始以为dij对于正权图的单源最短路是最快的 写了一发邻接表的dij 结果超时 把所有的cin改成scanf ...
- Layout---poj3169(差分约束+最短路spfa)
题目链接:http://poj.org/problem?id=3169 有n头牛站成一排 在他们之间有一些牛的关系比较好,所以彼此之间的距离不超过一定距离:也有一些关系不好的牛,希望彼此之间的距离大于 ...
随机推荐
- SQL Server 2008备份数据库失败,拒绝访问的原因
原文:SQL Server 2008备份数据库失败,拒绝访问的原因 备份数据到特定目录是出现拒绝访问,然后测试备份到C盘根目录正常. 查了下原因: 是因为那个目录没有Authenticated Use ...
- 登录oracle时,scott is locked (帐户被锁定) 的解决方法
登录Oracle时,用scott/tiger 通常此时会报一个错误: scott is locked (帐户被锁定) 现在就要用超级用户system将scott帐户进行解锁. cmd->sql ...
- 实战:sqlserver 2008 扩展事件-XML转换为标准的table格式
--假设已经存在Event Session删除 IF EXISTS (SELECT * FROM sys.server_event_sessions WHERE name='MonitorLongQu ...
- hdu4585 & BestCoder Round #1 项目管理(vector应用)
主题链接:pid=4858">http://acm.hdu.edu.cn/showproblem.php?pid=4858 项目管理 Time Limit: 2000/1000 MS ...
- ListView IllegalStateException
贴出源代码: android.widget.ListView ... if(mItemCount == 0){ resetList(); invokeOnItemScrollListener(); r ...
- 基于Office 365 无代码工作流分析-需求基本分析!
客户需求分析: 嘉昊信息是一家IT创业型公司,因为公司初创,有较多的招聘员工的需求,公司近期购买了Office 365,因为招聘工作繁琐,HR人员须要做非常多反复繁琐工作,HR主管提议开发一个招 ...
- Java并发学习之中的一个——线程的创建
本文是学习网络上的文章时的总结,感谢大家无私的分享. 1.与每一个Java语言中的元素一样,线程是对象.在Java中,我们有两种方式创建线程: a.通过直接继承thread类,然后覆盖run方法. b ...
- .net Work Flow 4.0
工作流 依据 WfMC 的定义.工作流(WorkFlow)就是自己主动运作的业务过程部分或总体.表现为參与者对文件.信息或任务依照规程採取行动,并令其在參与者之间传递.官方的总是非常抽象,抽象是为了可 ...
- HTML contact form with CAPTCHA
http://www.html-form-guide.com/contact-form/html-contact-form-captcha.html#codedownload
- IOS私人API用法
先要使用class-dump 和dumpFrameworks.pl 工具 将ios的framework导出来. 下面是工具的下载地址: class-dump下载地址http://www.codethe ...