2 1 1 2 3 3 3 1 2 5 2 3 5 3 1 2 0 0
3 2
import java.util.Arrays;
import java.util.Scanner;
public class HDU2554_ieayoio {
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
int n = cin.nextInt();
int m = cin.nextInt();
while (n != 0 && m != 0) {
int[][] map = new int[n + 5][n + 5];
for (int i = 0; i < map.length; i++)
Arrays.fill(map[i], Integer.MAX_VALUE / 3);
for (int i = 1; i <= m; i++) {
int a = cin.nextInt();
int b = cin.nextInt();
int c = cin.nextInt();
if (map[a][b] > c) {
map[a][b] = c;
map[b][a] = c;
}
}
for (int i = 1; i <= n; i++) {
map[i][i] = 0;
}
for (int k = 1; k <= n; k++)
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++) {
if (map[i][j] > map[i][k] + map[k][j])
map[i][j] = map[i][k] + map[k][j];
}
System.out.println(map[1][n]);
n = cin.nextInt();
m = cin.nextInt();
}
}
}
原文地址:http://blog.csdn.net/ieayoio/article/details/38497523