UVa10000_Longest Paths(最短路SPFA)

来源:互联网 发布:三星note8画图软件 编辑:程序博客网 时间:2024/05/22 17:41

解题报告

求最长路。

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

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <queue>#define inf 99999999#define N 110using 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 ( $1 < n \le 100$) 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 ( $1 \le s \le n$). 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 ``$p \ q$" 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 

211 20 0531 23 53 12 44 50 0555 15 25 35 44 14 20 00

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.





0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 塑料瓶盖滑丝了怎么办? 塑料杯盖滑丝了怎么办 洗衣机上的订子很难扭出来怎么办 滚筒洗衣机坏了打不开门怎么办 海尔滚筒洗衣机打不开门怎么办 格兰仕滚筒洗衣机门打不开怎么办? lg洗衣机洗完衣服门打不开怎么办 lg洗衣机不能冼衣服怎么办 vivo账户密码忘记了怎么办 oppo账户密码忘记了怎么办 74160从1开始计数怎么办 微信录音声音小怎么办 word文档已停止工作怎么办 怀孕30天不想要怎么办 一楼墙体起碱怎么办 农村平房顶四墙面渗水怎么办 被蝎子精蛰了怎么办 小娃头摔包要怎么办抹个什么 老人死了银行钱存死期怎么办 飙酷车神2一进画面就闪退怎么办 电脑开机键盘鼠标没反应怎么办 玩gta4自由城卡怎么办 gta5游戏服务当前不可用怎么办 健身后胳膊变粗怎么办 健身后手臂变粗怎么办 家猫见到我就跑怎么办 走路多了小腿酸痛怎么办 脚有脚气很痒怎么办 地板精油喷多了怎么办 史莱姆泥变硬了怎么办 dnf剑帝命中率低怎么办 无意中讲话伤害了人怎么办 高中上不下去了怎么办 高三坚持不下去怎么办 word点了不保存怎么办 做了ppt没保存怎么办 中班安全教案迷眼了怎么办 美的空调尘满怎么办 高考报名系统密码忘记怎么办 高考理综8题怎么办 高三了语文很差怎么办