DROP TABLE IF EXISTS node;

CREATE TABLE node(
    Origin VARCHAR(5),
    Destination VARCHAR(5),
    Cost INT,
    PRIMARY KEY (Origin, Destination) 
);

INSERT INTO node(Origin, Destination, Cost) VALUES ('A', 'O', 1000);
INSERT INTO node(Origin, Destination, Cost) VALUES ('B', 'O', 1500);
INSERT INTO node(Origin, Destination, Cost) VALUES ('B', 'J', 300);
INSERT INTO node(Origin, Destination, Cost) VALUES ('J', 'O', 200);
INSERT INTO node(Origin, Destination, Cost) VALUES ('J', 'L', 600);
INSERT INTO node(Origin, Destination, Cost) VALUES ('B', 'L', 2000);
INSERT INTO node(Origin, Destination, Cost) VALUES ('L', 'V', 500);
INSERT INTO node(Origin, Destination, Cost) VALUES ('J', 'C', 150);
INSERT INTO node(Origin, Destination, Cost) VALUES ('J', 'M', 300);
INSERT INTO node(Origin, Destination, Cost) VALUES ('B', 'W', 150);
INSERT INTO node(Origin, Destination, Cost) VALUES ('D', 'B', 200);
INSERT INTO node(Origin, Destination, Cost) VALUES ('L', 'S', 100);
INSERT INTO node(Origin, Destination, Cost) VALUES ('D', 'I', 800);
INSERT INTO node(Origin, Destination, Cost) VALUES ('I', 'E', 870);

WITH RECURSIVE Anc(Origin, Destination, Cost) AS
(
(SELECT * FROM node)
UNION ALL
(
SELECT F.Origin, AN.Destination, (AN.Cost+F.Cost)
FROM node F, Anc AN
WHERE F.Destination=AN.Origin)
)
SELECT Anc.Origin, Anc.Destination, Anc.Cost
FROM Anc
WHERE Anc.Origin='B'
ORDER BY Anc.Cost ASC;