Flask接口签名sign原理与实例代码浅析
299
2023-03-01
Java实现Floyd算法求最短路径
本文实例为大家分享了java实现Floyd算法求最短路径的具体代码,供大家参考,具体内容如下
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class TestMainIO {
/**
* @param args
* @throws FileNotFoundException
*/
public static void main(String[] args) throws FileNotFoundException {
TestMainIO test_print = new TestMainIO();
int[][] G = test_print.intputGragh("D:\\Users\\test.txt" , 6);
int[][] Dis = test_print.floyd(G, 6);
test_phttp://rint.printG(Dis, 6);
}
public void printG(int[][] G,int n){
for(int i=0;i for(int j=0;j System.out.println(i+"->"+j+" "+G[i][j]); } } } public int[][] intputGragh(String path , int num) throws FileNotFoundException{ int[][] G = new int[num][num]; for(int i=0;i for(int j=0;j G[i][j]=999; } } Scanner in = new Scanner(new FileInputStream(path)); while (in.hasNext()) { int i = in.nextInt(); int j = in.nextInt(); int weight = in.nextInt(); G[i][j] = weight; } return G; } public int[][] floyd(int[][] G,int n){ int[][] Dis= new int[n][n]; for(int q=0;q for(int w=0;w Dis[q][w]=G[q][w]; } } for(int k = 0; k < n; k++){ http:// for(int i=0; i < n; i++ ){ for(int j=0; j < n; j++){ if(Dis[i][j]>Dis[i][k]+Dis[k][j]){ Dis[i][j]=Dis[i][k]+Dis[k][j]; } } } } return Dis; } }
for(int j=0;j System.out.println(i+"->"+j+" "+G[i][j]); } } } public int[][] intputGragh(String path , int num) throws FileNotFoundException{ int[][] G = new int[num][num]; for(int i=0;i for(int j=0;j G[i][j]=999; } } Scanner in = new Scanner(new FileInputStream(path)); while (in.hasNext()) { int i = in.nextInt(); int j = in.nextInt(); int weight = in.nextInt(); G[i][j] = weight; } return G; } public int[][] floyd(int[][] G,int n){ int[][] Dis= new int[n][n]; for(int q=0;q for(int w=0;w Dis[q][w]=G[q][w]; } } for(int k = 0; k < n; k++){ http:// for(int i=0; i < n; i++ ){ for(int j=0; j < n; j++){ if(Dis[i][j]>Dis[i][k]+Dis[k][j]){ Dis[i][j]=Dis[i][k]+Dis[k][j]; } } } } return Dis; } }
System.out.println(i+"->"+j+" "+G[i][j]);
}
}
}
public int[][] intputGragh(String path , int num) throws FileNotFoundException{
int[][] G = new int[num][num];
for(int i=0;i for(int j=0;j G[i][j]=999; } } Scanner in = new Scanner(new FileInputStream(path)); while (in.hasNext()) { int i = in.nextInt(); int j = in.nextInt(); int weight = in.nextInt(); G[i][j] = weight; } return G; } public int[][] floyd(int[][] G,int n){ int[][] Dis= new int[n][n]; for(int q=0;q for(int w=0;w Dis[q][w]=G[q][w]; } } for(int k = 0; k < n; k++){ http:// for(int i=0; i < n; i++ ){ for(int j=0; j < n; j++){ if(Dis[i][j]>Dis[i][k]+Dis[k][j]){ Dis[i][j]=Dis[i][k]+Dis[k][j]; } } } } return Dis; } }
for(int j=0;j G[i][j]=999; } } Scanner in = new Scanner(new FileInputStream(path)); while (in.hasNext()) { int i = in.nextInt(); int j = in.nextInt(); int weight = in.nextInt(); G[i][j] = weight; } return G; } public int[][] floyd(int[][] G,int n){ int[][] Dis= new int[n][n]; for(int q=0;q for(int w=0;w Dis[q][w]=G[q][w]; } } for(int k = 0; k < n; k++){ http:// for(int i=0; i < n; i++ ){ for(int j=0; j < n; j++){ if(Dis[i][j]>Dis[i][k]+Dis[k][j]){ Dis[i][j]=Dis[i][k]+Dis[k][j]; } } } } return Dis; } }
G[i][j]=999;
}
}
Scanner in = new Scanner(new FileInputStream(path));
while (in.hasNext()) {
int i = in.nextInt();
int j = in.nextInt();
int weight = in.nextInt();
G[i][j] = weight;
}
return G;
}
public int[][] floyd(int[][] G,int n){
int[][] Dis= new int[n][n];
for(int q=0;q for(int w=0;w Dis[q][w]=G[q][w]; } } for(int k = 0; k < n; k++){ http:// for(int i=0; i < n; i++ ){ for(int j=0; j < n; j++){ if(Dis[i][j]>Dis[i][k]+Dis[k][j]){ Dis[i][j]=Dis[i][k]+Dis[k][j]; } } } } return Dis; } }
for(int w=0;w Dis[q][w]=G[q][w]; } } for(int k = 0; k < n; k++){ http:// for(int i=0; i < n; i++ ){ for(int j=0; j < n; j++){ if(Dis[i][j]>Dis[i][k]+Dis[k][j]){ Dis[i][j]=Dis[i][k]+Dis[k][j]; } } } } return Dis; } }
Dis[q][w]=G[q][w];
}
}
for(int k = 0; k < n; k++){
http:// for(int i=0; i < n; i++ ){
for(int j=0; j < n; j++){
if(Dis[i][j]>Dis[i][k]+Dis[k][j]){
Dis[i][j]=Dis[i][k]+Dis[k][j];
}
}
}
}
return Dis;
}
}
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~