class Solution {
public:
int networkDelayTime(vector<vector<int>>& times, int N, int K) {
vector<vector<int> > graph(N + , vector<int>(N + , ));
vector<int> cost(N + );
vector<bool> visited(N + , false); for (int i = ; i < times.size(); ++i) { graph[times[i][]][times[i][]] = times[i][]; }
for (int i = ; i <= N; ++i) { cost[i] = graph[K][i]; }
cost[K] = ;
visited[K] = true; for (int i = ; i <= N; ++i) {
int minNode = findMinNode(cost, visited);
if (minNode == ) { break; }
for (int j = ; j <= N; ++j) {
if (!visited[j] && cost[j] > cost[minNode] + graph[minNode][j]) {
cost[j] = cost[minNode] + graph[minNode][j];
}
}
visited[minNode] = true;
} return calSum(cost);
} int findMinNode(vector<int>& cost, vector<bool>& visited) {
int minIndex = , minV = ;
for (int i = ; i < cost.size(); ++i) {
if (!visited[i] && minV > cost[i]) {
minIndex = i;
minV = cost[i];
}
} return minIndex;
} int calSum(vector<int>& cost) {
int sum = ;
for (int i = ; i < cost.size(); ++i) {
if (cost[i] == ) { return -; }
if (sum < cost[i]) { sum = cost[i]; }
} return sum;
}
};

leetcode743的更多相关文章

  1. [Swift]LeetCode743. 网络延迟时间 | Network Delay Time

    There are N network nodes, labelled 1 to N. Given times, a list of travel times as directededges tim ...

  2. leetcode743 Network Delay Time

    """ here are N network nodes, labelled 1 to N. Given times, a list of travel times as ...

随机推荐

  1. 【JVM】jvm至jstack命令

    一.介绍 jstack是java虚拟机自带的一种堆栈跟踪工具.jstack用于打印出给定的java进程ID或core file或远程调试服务的Java堆栈信息,如果是在64位机器上,需要指定选项&qu ...

  2. day8 python学习 集合 深浅拷贝

    1.内存地址: 字符串在20位以内,没有空格,没有特殊字符的情况下,同样的字符串内存地址是一样的 2.元组中:在只有一个值的时在后边加逗号和没有逗号的区别 t1=(1) 不加逗号这个值是什么类型就打印 ...

  3. ExtJs 4.0 DeskTop集成 百度地图API

    经过3天的奋斗最终搞了出来, 网上的资料非常少,希望小⑦的文章对读者有点帮助,PS:小⑦非常努力的~. 不废话,上代码了. 首先.去百度官网Copy一个模版 http://api.map.baidu. ...

  4. IBM WebSphere MQ介绍安装以及配置服务详解(转)

    首先介绍一下MQ MQ消息队列的简称是一种应用程序对应用程序的通信方法.说白了也就是通过队列的方式来对应用程序进行数据通信.而无需专用链接来链接它们. MQ的通讯方式 1.数据报的方式 Datagra ...

  5. 环境搭建之JAVA项目部署步骤

    一.配置java环境 1.linux下安装jdk,在此处安装1.7_x64的jdk,解压缩  tar -zxvf xxxxxxx 2.将jdk移动到/usr下 mv java /user/ 3.配置环 ...

  6. [Python] 中文路径和中文文本文件乱码问题

    情景: Python首先读取名为log.txt的文本文件, 其中包含有文件名相对路径信息filename. 随后Python调用shutil.copy2(src, dst)对该filename文件进行 ...

  7. gphoto2 canon eos450d

    hjs@ubuntu:~$ gphoto2 --capture-image-and-download                                                   ...

  8. DllPlugin、DllReferencePlugin 可以提取的第三方库列表

    DllPlugin.DllReferencePlugin 可以提取的第三方库列表: 'vue/dist/vue.esm.js', // 'vue/dist/vue.common.js' for web ...

  9. Python——面向对象、绑定对象、组合

    1. 面向过程VS面向对象 (1)面向过程 核心是过程(流水线式思维),过程即解决问题的步骤,面向过程的设计就好比精心设计好一条流水线,考虑周全什么时候处理什么东西. 优点是:极大的降低了写程序的复杂 ...

  10. dede频道页实现三级栏目嵌套调用文章

      dede频道页实现三级栏目嵌套调用文章: //支持arclist标签开始--> $typeid = $row['id']; if((class_exists('PartView'))) { ...