Java,最短路径之floyd算法实现

首先,作为一种求最短路径的算法,floyd算法属于比较暴力的,算法复杂度为O(n^3),仅适用于n比较小的问题(n不超过1000)。

其模板如下:
for(int k=0; k<=n; ++k)
for(int i=0; i<=n; ++i)
for(int j=0; j<=n; ++j)
map[i][j] = min{ d[i][j], d[i][k]+d[k][j]};

其次,解决困扰我的一个问题,为什么新增节点 k 的循环要放在最外层呢?

Floyd算法本质上是DP,即对于每个(可能的)新增的节点k,来更新(可能的)节点i到j的最短距离。这是由其算法本身所决定的,其每一步求出任意一对顶点之间仅通过中间节点1,2,…,k的最短距离,当1,2,…,k扩展到所有顶点时,算法解出任意一对顶点间的最短距离,故顺序自然是:

        for(k=1;k<n;++k)
       //枚举任意一对顶点


举个例子:
共有四个节点
d[1][4] = 1;d[4][2]= 3 ; d[4][3] = 1; d[3][2] = 1;
其他不通,设为无限大;
如果我们按k循环在最内层来计算,
那么1,2节点的最短路径为
min( d[1][2], d[1][3]+map[3][1],d[1][4]+d[4][2]) = 4;

而且之后不再更新1,2节点的最短距,
然而实际上min(map[1][2]) = map[1][4]+map[4][3]+map[3][2] = 3;

因而也从反面证明了新增节点 k 的循环要放在最外层。

最后,

例题:

C - Tram

 POJ - 1847 

Tram network in Zagreb consists of a number of intersections and rails connecting some of them. In every intersection there is a switch pointing to the one of the rails going out of the intersection. When the tram enters the intersection it can leave only in the direction the switch is pointing. If the driver wants to go some other way, he/she has to manually change the switch. 

When a driver has do drive from intersection A to the intersection B he/she tries to choose the route that will minimize the number of times he/she will have to change the switches manually. 

Write a program that will calculate the minimal number of switch changes necessary to travel from intersection A to intersection B. 

Input

The first line of the input contains integers N, A and B, separated by a single blank character, 2 <= N <= 100, 1 <= A, B <= N, N is the number of intersections in the network, and intersections are numbered from 1 to N. 

Each of the following N lines contain a sequence of integers separated by a single blank character. First number in the i-th line, Ki (0 <= Ki <= N-1), represents the number of rails going out of the i-th intersection. Next Ki numbers represents the intersections directly connected to the i-th intersection.Switch in the i-th intersection is initially pointing in the direction of the first intersection listed. 

Output

The first and only line of the output should contain the target minimal number. If there is no route from A to B the line should contain the integer "-1".

Sample Input

3 2 1
2 2 3
2 3 1
2 1 2

Sample Output

0

AC代码如下:

import java.util.Scanner;

public class Main{

    final static int INF=10000;
    final static int maxn=100;
    static int d[][]=new int[maxn+5][maxn+5];
    
    static void init(int n)//被static修饰的成员变量和成员方法独立于该类的任何对象。也就是说,//它不依赖类特定的实例,被类的所有实例共享。
    {
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n;j++)
            {
                if(i==j) d[i][j]=0;
                else d[i][j]=INF;
            }
        }
    }
    
    static 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++)
                {
                    if(d[i][k]<INF&&d[k][j]<INF)
                    {
                        d[i][j]=Math.min(d[i][j], d[i][k]+d[k][j]);
                    }
                }
            }
        }
    }
    
    public static void main(String[] args) {
        
        Scanner cin=new Scanner(System.in);
        int n=cin.nextInt();
        int a=cin.nextInt();
        int b=cin.nextInt();
        init(n);
        for(int i=1;i<=n;i++)
        {
            int k=cin.nextInt();
            for(int j=0;j<k;j++)
            {
                int temp=cin.nextInt();
                if(j==0)    d[i][temp]=0;//第一个值不需要变道
                else d[i][temp]=1;
            }
        }
        floyd(n);
        if(d[a][b]==INF)    System.out.println(-1);
        else System.out.println(d[a][b]);
    }
}

 

  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值