/*
任意两点间的最短路问题(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. Android项目中实现native调用

    转载自搜狗测试公众号,本人学习使用,侵权删 最近小编在做公司输入法项目中java与native交互部分的测试,先简单学习了java代码调用native代码的实现原理,本次与大家一起分享jni协议,了解 ...

  2. 再也不用字符串拼接dom元素了

    <script type="text/html" id="tmp"> <div class="cla"> <u ...

  3. 解决mybatisplus saveBatch 或者save 无法插入主键问题

    解决mybatisplus saveBatch 或者save 无法插入主键问题 通过跟踪源码后得出结论,由于插入的表的主键不是自增的,而是手动赋值的,所以在调用saveBatch 执行的sql语句是没 ...

  4. vue插件安装

    百度云下载插件   https://pan.baidu.com/s/13QhPilzJa8yu3HvKCt47Pw 学习Vue.js时,Chrome浏览器安装Vue.js devtool能很方便的查看 ...

  5. python输入问题

    1.关于python的输入问题: 在2.x版本单行单输入input,单行多输入raw_input 在3.x版本中就已经没有raw_input,只有input,单行单输入多输入都可以. 类似2 3 4的 ...

  6. mysql-一行分隔成多行数据

    mysql将某个字段有分隔符号分隔成多行数据 SELECT a.id, a. NAME, substring_index( substring_index( a.name, ',', b.help_t ...

  7. 6383. 【NOIP2019模拟2019.10.07】果实摘取

    题目 题目大意 给你一个由整点组成的矩形,坐标绝对值范围小于等于\(n\),你在\((0,0)\),一开始面向\((1,0)\),每次转到后面第\(k\)个你能看到的点,然后将这条线上的点全部标记删除 ...

  8. vue 运行项目时,Uncaught (in promise) DOMException: Failed to execute 'open' on 'XMLHttpRequest': Invalid URL

    npm  run dev 运行项目后 验证码显示不出来 并报错 Uncaught (in promise) DOMException: Failed to execute 'open' on 'XML ...

  9. Delphi ComboBox组件 style=csDropDownlist 的赋值方法

    赋值方法: ComboBox1.Items.Add( '文本 '); ComboBox1.ItemIndex := ComboBox1.Items.IndexOf( '文本 '); 清空: Combo ...

  10. ajax 实战使用

    注意ajax 必须放在script脚本中使用 ajax用于前端朝后端提交数据,并且后端函数处理好结果返回给success函数作为回调函数给前端,前端拿到后端传来的值,比如code==0 来做相应的前端 ...