Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <div>
    <img src="https://ask.qcloudimg.com/http-save/yehe-3992881/9tdr1zgvyt.png" alt="" />
  </div>
</body>

</html>
              
            
!

CSS

              
                
              
            
!

JS

              
                // 邻接矩阵表示法
const graph = [
  [0, 2, 4, 0, 0, 0],
  [0, 0, 2, 4, 2, 0],
  [0, 0, 0, 0, 3, 0],
  [0, 0, 0, 0, 0, 2],
  [0, 0, 0, 3, 0, 2],
  [0, 0, 0, 0, 0, 0]
];

// 找出dist中的最小值
// const extractMinKey = (set) => {
//   let minValue = Number.MAX_SAFE_INTEGER;
//   let minKey = null;
//   Object.keys(set).forEach((k) => {
//     if (!set[k].visited && set[k].key < minValue) {
//       minValue = set[k].key;
//       minKey = k;
//     }
//   });
//   if (minKey) set[minKey].visited = true;
//   return minKey;
// };

// 接受图的邻接矩阵,源点作为参数
// const dijkstra = (graph, src) => {
//   const unReachSet1 = {};
//   const INF = Number.MAX_SAFE_INTEGER;
//   for (let i = 0; i < graph.length; i++) {
//     unReachSet1[i] = {
//       key: INF,
//       previous: null,
//       visited: false
//     };
//   }
//   unReachSet1[src].key = 0;
//   const unReachSet = { ...unReachSet1 };
//   while (Object.keys(unReachSet).length) {
//     const v = extractMinKey(unReachSet);
//     for (let i = 0; i < graph.length; i++) {
//       // 找到邻居节点
//       if (graph[v] && graph[v][i] !== 0) {
//         // 松弛操作
//         if (
//           unReachSet[i] &&
//           unReachSet[v] &&
//           unReachSet[i].key > unReachSet[v].key + graph[v][i]
//         ) {
//           unReachSet[i].key = unReachSet[v].key + graph[v][i];
//           unReachSet[i].previous = v;
//         }
//       }
//     }
//     if (v) delete unReachSet[v];
//   }
//   return unReachSet1;
// };

function extractMin(set) {
  let minKey,
    minValue = Number.MAX_SAFE_INTEGER;
  for (const i in set) {
    if (set[i].key < minValue) {
      minKey = i;
      minValue = set[i].key;
    }
  }
  return minKey;
}

function dijkstra(graph, s) {
  const unReachSet = {};
  const result = [];
  graph.forEach((item, index) => {
    unReachSet[index] = { key: Number.MAX_SAFE_INTEGER, previous: null };
  });
  unReachSet[s].key = 0;
  result[0] = unReachSet[s];
  while (Object.keys(unReachSet).length) {
    const v = extractMin(unReachSet);
    for (let i = 0; i < graph.length; i++) {
      if (!graph[v][i]) continue;
      if (unReachSet[i].key > unReachSet[v].key + graph[v][i]) {
        unReachSet[i].key = unReachSet[v].key + graph[v][i];
        unReachSet[i].previous = v;
        result[i] = unReachSet[i];
      }
    }

    delete unReachSet[v];
  }
  return result;
}

console.log(dijkstra(graph, 0));

              
            
!
999px

Console