class Node{
int[] dis;
int val = Integer.MAX_VALUE;
public Node(int[] dis) {
this.dis = dis;
}
}
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[][] dis = new int[6][6];
Node[] node = new Node[6];
for (int i = 0; i < 6; i ++) {
String[] temp = sc.nextLine().split(" ");
for (int j = 0; j < 6; j ++) {
dis[i][j] = Integer.parseInt(temp[j]);
}
node[i] = new Node(dis[i]);
}
Queue<Node> queue = new LinkedList<>();
queue.offer(node[0]);
node[0].val = 0;
while (! queue.isEmpty()) {
Node temp = queue.poll();
for (int i = 0; i < 6; i ++) {
if (temp.dis[i] != 0 && temp.dis[i] != -1) {
node[i].val = Math.min(node[i].val, temp.val + temp.dis[i]);
queue.offer(node[i]);
}
}
}
for (Node x: node) {
System.out.println(x.val);
}
}
}
第二题仅作参考
🙁