AcWing
  • 首页
  • 活动
  • 题库
  • 竞赛
  • 应用
  • 更多
    • 题解
    • 分享
    • 商店
    • 问答
    • 吐槽
  • App
  • 登录/注册

AcWing 3512. 最短距离总和 0.03 AC币

作者: 作者的头像   LaLa_JUROU ,  2023-05-26 01:43:05 ,  所有人可见 ,  阅读 11


0


//Floyd算法 //逆序加入节点
import java.io.*;

public class Main{
    static StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));

    static int n,a[][];
    static long sum;
    public static void main(String[] args) throws IOException{
        n = next();
        a = new int[n][n];
        if(n == 1) {
            System.out.println(0);
            return; //特例
        }
        for(int i = 0; i < n; i ++) {//输入
            for(int v = 0; v < n; v ++) {
                a[i][v] = next();
            }
        }
        int x = n - 1;
        for(int y = n - 1; y > 0; y --, x --) {//迭代以x为起点的矩阵
            update(y);
            //累加到sum中
            for(int b = x; b < n; b ++) {
                for(int c = x; c < n; c ++) {
                    sum += a[b][c];
                }
            }
        }

        System.out.println(sum);
    }
    static void update(int v) {
        for(int x = 0; x < n; x ++) {
            for(int y = 0; y < n; y ++) {
                if(a[x][y] > a[x][v] + a[v][y] ) {
                    a[x][y] = a[x][v] + a[v][y]; //x->v->y 以v为中转节点迭代
                }
            }
        }
    }

    static int next() throws IOException{
        in.nextToken();
        return (int)in.nval;
    }
}

0 评论

你确定删除吗?

© 2018-2023 AcWing 版权所有  |  京ICP备17053197号-1
用户协议  |  隐私政策  |  常见问题  |  联系我们
AcWing
请输入登录信息
更多登录方式: 微信图标 qq图标 qq图标
请输入绑定的邮箱地址
请输入注册信息