回复: 3
跳转到指定楼层
上一主题 下一主题
收起左侧

空起床店面 面经 有向图最短距离

头像被屏蔽
提示: 作者被禁止或删除 内容自动屏蔽

上一篇:求Akuna Capital Junior C++ dev 电面面经
下一篇:近期面的google onsite挂经
🔗
runningMajia 2017-4-8 05:42:52 | 只看该作者
全局:
多谢lz,这题目的input是什么啊?
回复

使用道具 举报

🔗
swissashley 2017-5-10 02:39:25 | 只看该作者
全局:
JS Solution,

Please give me any comments :)

// Implement a priority queue, sorted by cost
class PQueue {
  constructor() {
    this.nodes = [];
  }
  enqueue(key, cost) {
    this.nodes.push({cost: cost, key: key});
    this.sort();
  }
  sort() {
    this.nodes.sort((a,b) => a.cost - b.cost);
  }
  dequeue() {
    let node = this.nodes.shift();
    return node.key;
  }
  isEmpty() {
    return this.nodes.length === 0;
  }

}

// Define a Wizard class with id and friends
class Wizard {
  constructor(id) {
    this.id = id;
    this.friends = new Set();
  }
}

// Define a friend relationship and the 'cost' lol
class Relationship {
  constructor(id,cost) {
    this.id = id;
    this.cost = cost;
  }
}

function introducingWizard(wizards, start, end) {
  let visited = new Set();
  let cost = {};
  let graph = {};
  let previous = {};
  let path = [];
  let nodes = new PQueue();
  let MAX = Number.MAX_SAFE_INTEGER;

  // Put the first node into our PQ, with cost 0.
  nodes.enqueue(start, 0);
  cost[start] = 0;

  // Build wizard relationship.
  for (let wizard of wizards) {
    if (wizard[1] !== start ) cost[wizard[1]] = MAX;
    let relationship = new Relationship(wizard[1], wizard[2]);
    if (graph[wizard[0]] === undefined) graph[wizard[0]] = new Wizard(wizard[0]);
    graph[wizard[0]].friends.add(relationship);
  }
  // Dijkstra
  while (!nodes.isEmpty()) {
    let curr = nodes.dequeue();

    if (!visited.has(curr)) {
      visited.add(curr);
      // If we get to the end wizard, we construct the path via previous visited table 'previous'
      if (curr === end) {
        while (previous[curr] !== undefined) {
          path.push(curr);
          curr = previous[curr];
        }
        break;
      }
      // Otherwise we keep searching for the next friend with minCost
      for (let friend of graph[curr].friends) {
        if (!visited.has(friend.id)) {
          let minCost = cost[curr] + friend.cost;
          if (minCost < cost[friend.id]) {
            cost[friend.id] = minCost;
            previous[friend.id] = curr;
            nodes.enqueue(friend.id, minCost);
          }
        }
      }
    }
  }
  // Push the start to the path and reverse it; return the path as well as the minCost for end.
  path.push(start);
  path.reverse();
  return [path, cost[end]];
}

let wizards = [
  [0,1,0],
  [2,3,0],
  [1,4,9],
  [3,2,1],
  [5,6,1],
  [2,6,16],
  [5,3,0],
  [7,9,4],
  [8,9,1],
  [4,6,4],
  [1,3,0],
  [6,3,0],
  [6,8,4],
  [4,2,0],
  [5,8,9]
];

console.log(introducingWizard(wizards, 0, 6));


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表