package 最短路径问题;
import java.util.Scanner;
public class Main {
private static int [][]e;
private static int k,i,j,n,m,t1,t2,t3;
private static int inf = 99999999;
public static void main(String[] args) {
e = new int[10][10];
Scanner input = new Scanner(System.in);
n = input.nextInt();//表示顶点
m = input.nextInt();//表示边
//初始化
for(i = 1;i<=n;i++)
{
for(j = 1;j<=n;j++)
{
if(i == j)
{
e[i][j] = 0;
}else{
e[i][j] = inf;
}
}
}
//读入边
for(i = 1;i<=m;i++){
t1 = input.nextInt();
t2 = input.nextInt();
t3 = input.nextInt();
e[t1][t2] = t3;
}
//Floyd-Warshall算法
for(k = 1;k<=n;k++)
{
for(i=1;i<=n;i++){
for(j=1;j<=n;j++)
{
if(e[i][j] > e[i][k]+e[k][j])
{
e[i][j] = e[i][k]+e[k][j];
}
}
}
}
//输出结果
for(i = 1;i<=n;i++)
{
for(j = 1;j<=n;j++)
{
System.out.print(e[i][j]+" ");
}
System.out.println();
}
}
}
原文:http://www.cnblogs.com/aicpcode/p/4396183.html