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.

时间: 2024-08-11 06:44:14

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

POJ3255:Roadblocks(次短路 SPFA+A星)

给出1-N 个点 的距离, 求从1号到N号的次短路, 直接用k短路来做了,,dj会TLE, 用spfa就过了 题目: I - RoadblocksTime Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Description Bessie has moved to a small farm and sometimes enjoys returning to visit o

最短路 spfa算法

问题描述 给定一个n个顶点,m条边的有向图(其中某些边权可能为负,但保证没有负环).请你计算从1号点到其他点的最短路(顶点从1到n编号). 输入格式 第一行两个整数n, m. 接下来的m行,每行有三个整数u, v, l,表示u到v有一条长度为l的边. 输出格式 共n-1行,第i行表示1号点到i+1号点的最短路. 样例输入 3 31 2 -12 3 -13 1 2 样例输出 -1-2 数据规模与约定 对于10%的数据,n = 2,m = 2. 对于30%的数据,n <= 5,m <= 10. 对

TOJ--2674--最短路(spfa)

厌死了......排位 晋级赛 两连跪 ... 三角形 的那题还是 无限WA  ... 还有 明天又要早起... 先还是来看下这题吧 话说 好久没写 最短路了 --------- spfa 是我最喜欢的最短路版本 touch me 这题 其实相比其它的最短路 还是有个很让人看不懂的地方---让我纠结了很久 首先 它告诉了我们城市的编号 是从 1~1000 然后 又突然告诉我们 哪些城市和我们的出发地相邻 假如是 1 10 100...之类的 最后 告诉我们 你想取哪些城市 我就在想 起点呢!!!

洛谷P1462 通往奥格瑞玛的道路 二分答案+最短路SPFA

洛谷P1462 通往奥格瑞玛的道路二分答案+最短路SPFA 二分交费最多的一次的钱数 然后只将符合要求的边加入图中 如果到终点的最短路大于等于血量 或者直接起点不能到达终点那么说明不符合要求 需要加大答案 时间复杂度 (log答案)* Ek 需要注意如果本来就不能到达 那么直接输出AFK 1 #include <bits/stdc++.h> 2 #define LL long long 3 #define For(i,j,k) for(int i=j;i<=k;i++) 4 using

hdu 2962 Trucking (二分+最短路Spfa)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2962 Trucking Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1763    Accepted Submission(s): 618 Problem Description A certain local trucking co

nyoj1006(最短路次短路spfa)

偷西瓜 时间限制:1000 ms  |  内存限制:65535 KB 难度:4 描述 对于农村的孩子来说最大的乐趣,莫过于和小伙伴们一块下地偷西瓜了,虽然孩子们条件不是很好,但是往往他们很聪明,他们总在计算着到达瓜田的距离,以及逃跑的路线,他们总是以最短的距离冲到瓜田里面,然后以最短的距离回到出发的地方,不过瓜田的大人们已经在他们来的路上等待他们.于是聪明的小伙伴们便不走过的路,即每条路只走一遍,如果小伙伴们回不到出发的地方,他们就说"eating", 我们假设 有 n (n<=

It&amp;#39;s not a Bug, It&amp;#39;s a Feature! (poj 1482 最短路SPFA+隐式图+位运算)

Language: Default It's not a Bug, It's a Feature! Time Limit: 5000MS   Memory Limit: 30000K Total Submissions: 1353   Accepted: 516 Description It is a curious fact that consumers buying a new software product generally do not expect the software to

uva 10099 The Tourist Guide(单源最短路/spfa/dijkstra)

题目: 链接:点击打开链接 题意: 思路: 代码: #include <iostream> #include <cstring> #include <cstdio> using namespace std; int map[101][101]; void floyd(int n) { for(int k=1; k<=n; k++) for(int i=1; i<=n; i++) for(int j=1; j<=n; j++) map[i][j] = m

poj1511 最短路 spfa

// poj1511 最短路 spfa // // Bellman-Ford 队列优化 // // 留个spfa模板,精髓就是不断松弛,并将可能会影响 // 结果的点,如果在队列中不用加,不在就加入. #include <cstdio> #include <algorithm> #include <cstring> #include <iostream> #include <queue> typedef long long ll; using n