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头牛站成一排 在他们之间有一些牛的关系比较好,所以彼此之间的距离不超过一定距离:也有一些关系不好的牛,希望彼此之间的距离大于 ...
随机推荐
- hdu1114(完全背包)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1114 分析:很裸的一道完全背包题,只是这里求装满背包后使得价值最少,只需初始化数组dp为inf:dp[ ...
- Urban Dictionary: psd
Urban Dictionary: psd psd Share on twitter Share on facebook Share on more 3 up, 1 down It means Poo ...
- POJ3050 Hopscotch 【DFS】
Hopscotch Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 2113 Accepted: 1514 Descrip ...
- WPF界面设计技巧(9)—使用UI自动化布局
原文:WPF界面设计技巧(9)-使用UI自动化布局 最近一直没时间更新这系列文章,因为我一直在埋头编写我的第一个WPF应用程序:MailMail 今天开始编写附属的加密/解密工具,对UI自动化布局有些 ...
- C# webservice初探
转载请注明出处Coder的不平庸:http://blog.csdn.net/pearyangyang/article/details/46348633 因为工作的终端曾经是直接对数据库进行操作,导致每 ...
- 屏蔽DataGridView控件DataError 事件提示的异常信息
DataGridView.DataError 事件简单介绍: 出现故障.则外部数据分析或验证操作引发异常,或者.当尝试提交数据写入数据源失败. 具体信息:參见MSDN this.dgvState.Da ...
- [欧拉回路] hdu 3018 Ant Trip
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3018 Ant Trip Time Limit: 2000/1000 MS (Java/Others) ...
- spring4.1+springmvc4.1+mybatis3.2.8+spring-security3.2.5集成环境建设
在最近使用的项目ssi+spring-security 结构体.建立你自己的家,这是什么环境. 只有记录的目的. 项目结构: 类文件: ...
- 重新启动IIS服务的方法
WINDOWS提供WEB服务的IIS有时候会出现訪问过大导致站点打不开,这时重新启动IIS是最好的选择. 1.界面操作 打开"控制面板"->"管理工具"- ...
- Codeforces Round#297 div2
B: 题意:给定一个字符串,然后给定m个数字 对于每个数字ai的含义是,将ai到n-ai+1的字符串给翻转一遍. 要求输出m次翻转之后的字符串. 想法就是判断第i个位置的字符是翻转了奇数次,还是偶数次 ...