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. [LOJ6029~6052]雅礼集训 2017 选做

    Link 代码可以在loj上看我的提交记录. Day 1 [LOJ6029]市场 对于一次除法操作,若区间内所有数的减少量均相同则可视作区间减法,否则暴力递归下去.显然一个线段树节点只会被暴力递归进去 ...

  2. ZH奶酪:最简单的Django安装方法(Windows 7)

    前提是你已经在机器上安装了python~并且你的机器能够上网~ 1.进入:https://bootstrap.pypa.io/get-pip.py,ctrl+s保存网页(其实是.py文件)到某一目录: ...

  3. Vquery PHP 简单爬虫类

    http://www.thinkphp.cn/topic/36693.html 在使用php进行网页抓取的时候你有没有感觉到用起来比较麻烦呢?目前我还没有发现php有这样针对网页抓取的类,每次用到这个 ...

  4. 【转】每天一个linux命令(20):find命令之exec

    原文网址:http://www.cnblogs.com/peida/archive/2012/11/14/2769248.html find是我们很常用的一个Linux命令,但是我们一般查找出来的并不 ...

  5. zstack(一)运行及开发环境搭建及说明(转载)

    本篇介绍zstack的部署环境,以及二次开发环境 运行环境 讲真,ZStack的安装做的还是不错的,提供多种安装模式,如离线安装.在线安装.一键安装.分布式安装等.安装的过程其实都很简单,当然这也是z ...

  6. DevExpress GridControl控件行内新增、编辑、删除添加选择框(转)

    http://blog.csdn.net/m1654399928/article/details/21951519 1.首先到GridControl控件设计里设置属性Repository    (In ...

  7. springcould

     [Spring For All 社区周报] 「社区活动」(送书哦)Spring For All 第 1 期高手 QA 环节 — Spring Cloud 微服务实战http://spring4all ...

  8. MySQL数据库服务器整体规划(go)

    我们在搭建MySQL数据库服务器的开始阶段就合理的规划,可以避免以后的很多问题的产生,大大节省我们的时间和精力,在一定幅度上降低成本.当然,这会涉及很多方面.比如机器的选型.业务评估和系统规划等. 所 ...

  9. asp.net 退出登陆(解决退出后点击浏览器后退问题仍然可回到页面问题)

    代码如下: Session.Abandon(); Response.Redirect("Login.aspx"); 但是这样点点击浏览器的后退仍然可以回到刚才的页面,这可不行,在网 ...

  10. c#数据类型 与sql的对应关系 以及 取值范围

    Short Name .NET Class Type Width Range (bits) SQL Datatype (Closest Match) Constraint to use (if nee ...