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. js 数组方法比较

    js 数组方法比较 table th:first-of-type { width: 80px; } table th:nth-of-type(2) { width: 120px; } table th ...

  2. 【appium】根据UIAutomator定位元素

    text属性的方法 driver.find_element_by_android_uiautomator('new UiSelector().text("Custom View") ...

  3. python 异常类型----后期需理解调整

    1.Python内建异常体系结构The class hierarchy for built-in exceptions is: BaseException +-- SystemExit +-- Key ...

  4. ORACLE11g 安装中xhost: unable to open display 问题解决纪实 (go)

    http://blog.csdn.net/mchdba/article/details/62235761 1,Xhosts报错 安装好vncserver,本地pc笔记本能通过vnc viewer远程连 ...

  5. linux 守护进程(daemon process)代码-详细注释

    1. 进程组 组长不能创建新的 会话. 其它进程可以创建新的会话,创建后既成为会话首领,同时失去控制终端. 2. 会话首领可以重新打开控制终端 1 #include <stdio.h> 2 ...

  6. win8设置开机启动项

    c:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp

  7. java 解析pdm文档

    前面展示了pdm 的xml结构,既然知道了结构,用java来解析也不会太难,这就为代码自动生成奠定了基础 package com.core.reader.pdmreader.imp; import j ...

  8. bzoj1833 数字计数

    Description 给定两个正整数a和b,求在[a,b]中的所有整数中,每个数码(digit)各出现了多少次. Input 输入文件中仅包含一行两个整数a.b,含义如上所述. Output 输出文 ...

  9. [转]winform CEF

    下载地址: http://opensource.spotify.com/cefbuilds/index.html 来自: NanUI 作者

  10. Valgrind使用指南和错误分析

    Valgrind使用指南和错误分析 Valgrind是一个GPL的软件,用于Linux(For x86, amd64 and ppc32)程序的内存调试和代码剖析.你可以在它的环境中运行你的程序来监视 ...