names = {'1' '2' '3' '4' '5' '6' '7' '8' 'A' 'B' 'C'};
A1=[1 0 0 0 5 0 0 5 0 0 0; ...
0 1 0 0 0 0 5 5 0 0 0; ...
0 0 1 10 5 0 0 0 0 0 0;...
0 0 10 1 0 0 3 0 0 0 0;...
5 0 5 0 1 0 0 0 1 0 0; ...
0 0 0 0 0 1 0 5 0 1 0;...
0 5 0 3 0 0 1 0 0 0 1;...
5 5 0 0 0 5 0 1 0 0 0;...
0 0 0 0 1 0 0 0 1 0 0;...
0 0 0 0 0 1 0 0 0 1 0;...
start_node = names == 'A';
end_nodes = names == 'B' | names == 'C';
distances = Inf(size(names));
distances(start_node) = zeros(1);
visited = false(size(names));
while ~all(visited(end_nodes))
[~, current_node] = min(distances(~visited));
visited(current_node) = true;
for neighbor = find(A1(current_node,:))
new_distance = distances(current_node) + A1(current_node,neighbor);
if new_distance < distances(neighbor)
distances(neighbor) = new_distance;
fprintf('Shortest distances to ending nodes:\n');
fprintf('%s: %d\n', names{i}, distances(i));
if distances(names == 'B') < distances(names == 'C')
shortest_path = {'A' 'B'};
shortest_path = {'A' 'C'};
fprintf('Shortest path: %s', shortest_path{1});
for i = 2:length(shortest_path)
fprintf(' -> %s', shortest_path{i});