/*
任意两点间的最短路问题(Floyd-Warshall算法) */ import java.util.Scanner; public class Main {
//图的顶点数,总边数
static int V, E;
//存储所有的边,大小为顶点数
static int[][] Edges;
static int[][] d;
static final int MAX_VALUE = 999999; public static void main(String[] args) {
creatGraph();
shortPath();
for (int i = 0; i < V; i++) {
for (int j = 0; j < V; j++) {
System.out.print(d[i][j] + " ");
}
System.out.println();
}
} static void shortPath() {
d = new int[V][V];
for (int i = 0; i < V; i++) {
for (int j = 0; j < V; j++) {
d[i][j] = Edges[i][j];
}
}
for (int k = 0; k < V; k++)
for (int i = 0; i < V; i++) {
for (int j = 0; j < V; j++) {
d[i][j] = Math.min(d[i][j], d[i][k] + d[k][j]);
}
}
} static void creatGraph() {
Scanner sc = new Scanner(System.in);
V = sc.nextInt();
E = sc.nextInt();
Edges = new int[V][V];
for (int i = 0; i < V; i++) {
for (int j = 0; j < V; j++) {
Edges[i][j] = MAX_VALUE;
if (i == j) Edges[i][j] = 0;
}
}
for (int i = 0; i < E; i++) {
int u = sc.nextInt();
int v = sc.nextInt();
int w = sc.nextInt();
Edges[u][v] = w;
Edges[v][u] = w;
}
}
}

任意两点间的最短路问题(Floyd-Warshall算法)的更多相关文章

  1. 任意两点间的最短路问题(Floyd-Warshall算法)

    #define _CRT_SECURE_NO_WARNINGS /* 7 10 0 1 5 0 2 2 1 2 4 1 3 2 2 3 6 2 4 10 3 5 1 4 5 3 4 6 5 5 6 9 ...

  2. 【算法】Floyd-Warshall算法(任意两点间的最短路问题)(判断负圈)

    求解所有两点间的最短路问题叫做任意两点间的最短路问题. 可以用动态规划来解决, d[k][i][j] 表示只用前k个顶点和顶点i到顶点j的最短路径长度. 分两种情况讨论: 1.经过顶点k,  d[k] ...

  3. AOJ GRL_1_C: All Pairs Shortest Path (Floyd-Warshall算法求任意两点间的最短路径)(Bellman-Ford算法判断负圈)

    题目链接:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=GRL_1_C All Pairs Shortest Path Input ...

  4. Dijkstra算法:任意两点间的最短路问题 路径还原

    #define _CRT_SECURE_NO_WARNINGS /* 7 10 0 1 5 0 2 2 1 2 4 1 3 2 2 3 6 2 4 10 3 5 1 4 5 3 4 6 5 5 6 9 ...

  5. Floyd—Warshall算法

    我们用DP来求解任意两点间的最短路问题 首先定义状态:d[k][i][k]表示使用顶点1~k,i,j的情况下,i到j的最短路径 (d[0][i][j]表示只使用i和j,因此d[0][i][j] = c ...

  6. 图算法之Floyd-Warshall 算法-- 任意两点间最小距离

    1.Floyd-Warshall 算法 给定一张图,在o(n3)时间内求出任意两点间的最小距离,并可以在求解过程中保存路径 2.Floyd-Warshall 算法概念 这是一个动态规划的算法. 将顶点 ...

  7. LCA - 求任意两点间的距离

    There are n houses in the village and some bidirectional roads connecting them. Every day peole alwa ...

  8. 任意两点间最短距离floyd-warshall ---- POJ 2139 Six Degrees of Cowvin Bacon

    floyd-warshall算法 通过dp思想 求任意两点之间最短距离 重复利用数组实现方式dist[i][j] i - j的最短距离 for(int k = 1; k <= N; k++) f ...

  9. POJ 3660 Cow Contest 任意两点之间的关系 Floyd

    题意:牛之间有绝对的强弱,给出一些胜负关系,问有多少头牛可以确定其绝对排名. #include <iostream> #include <cstdio> #include &l ...

随机推荐

  1. linux常用命令-1系统相关命令

    hostname #计算机名 passwd #修改密码 reboot #重启 shutdown –r now #立刻重启(root用户使用) shutdown –r 10 #过10分钟自动重启(roo ...

  2. shell script 二 判断符号【】 shift 偏移量 if then fi

    判断符号[]类似于test.但是[]有通配符及正则表达式,为了区分,利用[]来做判断时,前后都需要加空格来区分.又一个坑 [ -z "$HOME" ];echo $? 例: 1 r ...

  3. secureCRT The remote system refused the connection.解决办法

    使用远程登录工具SecureCRT登陆ubuntu的时候遇到了这个问题: secureCRT The remote system refused the connection 这个问题的原因是是Ubu ...

  4. 在CMake中启用VS2017的C++17特性

    VS2017的C++17特性默认并未开启,需要在编译参数中手动开启.找到项目的CMakeLists.txt,在查找编译器的代码后面加入如下内容即可. ") include(CheckCXXC ...

  5. C/C++ clock()

    { clock_t start = clock(); for(int i = 0; i < 1000000; i++) { static int var = 0; var++; } clock_ ...

  6. 【2017中国大学生程序设计竞赛 - 网络选拔赛】Friend-Graph

    [链接]http://acm.hdu.edu.cn/showproblem.php?pid=6152 [题意] 有一个队伍,如果队伍里有三个或三个以上的人互相认识 或者队伍里有三个或三个以上的人互相不 ...

  7. vscode 编写Python走过的坑

    1,在使用vscode 中import turtle 这个模块, 再调用t = turtle.Pen(),始终提示无法找到turtle模块 2.可是使用terminal 中调用turtle模块,没有问 ...

  8. java lambda filter写法

    datas.stream().filter(m->!m.getSerialId().equals(setting.getSerialId())).collect(Collectors.toLis ...

  9. dubbo使用multicast注册方式消费者无法发现服务的一种情况(我遇到的情况)

    今天做dubbo测试的时候,翻出以前的代码,使用multicast广播地址的方式消费者居然无法发现服务.我的情况是因为启用了vmware虚拟机的网卡,导致了消费者无法发现服务,禁用vmware网卡后可 ...

  10. IdentityServer4认证服务器集成Identity&配置持久化数据库

    文章简介 asp.net core的空Web项目集成相关dll和页面文件配置IdnetityServer4认证服务器 Ids4集成Identity Ids4配置持久化到数据库 写在最前面,此文章不详细 ...