[Leetcode] 743. Network Delay Time 解题报告
2018-03-12 13:27
435 查看
题目:
There are
Given
the target node, and
Now, we send a signal from a certain node
the signal? If it is impossible, return
Note:
The length of
All edges
思路:
1、BFS算法:我们首先构造二维邻接矩阵,表示结点和结点之间的连接关系。然后采用BFS的方法,遍历从结点K到所有其它结点的距离。最后选出距离最大者即可。由于需要构造邻接矩阵,所有算法的空间复杂度为O(n^2)。
2、Floyd算法:图论中有个Floyd算法特别简明。该算法是计算节点两两之间的最短距离的,我们也可以用来计算从结点K到所有其它结点之间的距离。也就是说,如果发现d[v] > d[u] + w,并且从u到v的距离是w,那么就更新d[v]的距离。最后我们遍历一遍从结点K到所有其它结点的最大距离,选出最大值即可。这里算法的空间复杂度为O(n),但是由于涉及到很多无效的计算,所有实际上算法的效率还不如BFS(但是该算法对于计算所有结点之间的两两距离很有效)。
代码:
1、BFS算法:
class Solution {
public:
int networkDelayTime(vector<vector<int>>& times, int N, int K) {
vector<int> sigTime(N, INT_MAX);
vector< vector<int> > mat(N, vector<int>(N, -1)); //adjacent matrix
for(auto edgeVec : times) {
mat[ edgeVec[0] - 1][ edgeVec[1] - 1] = edgeVec[2];
}
K = K - 1;
sigTime[K] = 0;
queue<int> nodeQ;
nodeQ.push(K);
while(!nodeQ.empty()) {
int nd = nodeQ.front();
for(int i=0; i<N; ++i) {
if (mat[nd][i] >= 0 && sigTime[i] > sigTime[nd] + mat[nd][i]) {
nodeQ.push(i);
sigTime[i] = sigTime[nd] + mat[nd][i];
}
}
nodeQ.pop();
}
int ans = 0;
for(auto t : sigTime) {
ans = max(t, ans);
}
return (ans == INT_MAX) ? -1 : ans;
}
};
2、Floyd算法:
There are
Nnetwork nodes, labelled
1to
N.
Given
times, a list of travel times as directed edges
times[i] = (u, v, w), where
uis the source node,
vis
the target node, and
wis the time it takes for a signal to travel from source to target.
Now, we send a signal from a certain node
K. How long will it take for all nodes to receive
the signal? If it is impossible, return
-1.
Note:
Nwill be in the range
[1, 100].
Kwill be in the range
[1, N].
The length of
timeswill be in the range
[1, 6000].
All edges
times[i] = (u, v, w)will have
1 <= u, v <= Nand
1 <= w <= 100.
思路:
1、BFS算法:我们首先构造二维邻接矩阵,表示结点和结点之间的连接关系。然后采用BFS的方法,遍历从结点K到所有其它结点的距离。最后选出距离最大者即可。由于需要构造邻接矩阵,所有算法的空间复杂度为O(n^2)。
2、Floyd算法:图论中有个Floyd算法特别简明。该算法是计算节点两两之间的最短距离的,我们也可以用来计算从结点K到所有其它结点之间的距离。也就是说,如果发现d[v] > d[u] + w,并且从u到v的距离是w,那么就更新d[v]的距离。最后我们遍历一遍从结点K到所有其它结点的最大距离,选出最大值即可。这里算法的空间复杂度为O(n),但是由于涉及到很多无效的计算,所有实际上算法的效率还不如BFS(但是该算法对于计算所有结点之间的两两距离很有效)。
代码:
1、BFS算法:
class Solution {
public:
int networkDelayTime(vector<vector<int>>& times, int N, int K) {
vector<int> sigTime(N, INT_MAX);
vector< vector<int> > mat(N, vector<int>(N, -1)); //adjacent matrix
for(auto edgeVec : times) {
mat[ edgeVec[0] - 1][ edgeVec[1] - 1] = edgeVec[2];
}
K = K - 1;
sigTime[K] = 0;
queue<int> nodeQ;
nodeQ.push(K);
while(!nodeQ.empty()) {
int nd = nodeQ.front();
for(int i=0; i<N; ++i) {
if (mat[nd][i] >= 0 && sigTime[i] > sigTime[nd] + mat[nd][i]) {
nodeQ.push(i);
sigTime[i] = sigTime[nd] + mat[nd][i];
}
}
nodeQ.pop();
}
int ans = 0;
for(auto t : sigTime) {
ans = max(t, ans);
}
return (ans == INT_MAX) ? -1 : ans;
}
};
2、Floyd算法:
class Solution { public: int networkDelayTime(vector<vector<int>>& times, int N, int K) { vector<int> dist(N + 1, INT_MAX); // initial distances dist[K] = 0; for (int i = 0; i < N; ++i) { for (auto time : times) { int u = time[0], v = time[1], w = time[2]; if (dist[u] != INT_MAX && dist[v] > dist[u] + w) { dist[v] = dist[u] + w; } } } int max_wait = 0; for (int i = 1; i <= N; ++i) { max_wait = max(max_wait, dist[i]); } return max_wait == INT_MAX ? -1 : max_wait; } };
相关文章推荐
- Leetcode 115. Distinct Subsequences 计算子串 解题报告
- [Leetcode] 388. Longest Absolute File Path 解题报告
- [leetcode] 15. 3Sum 解题报告
- 【LeetCode】2. Add Two Numbers 解题报告(Python)
- Leetcode 463. Island Perimeter 求岛的周长 解题报告
- LeetCode Convert Sorted List to Binary Search Tree 解题报告
- leetcode解题报告:53. Maximum Subarray
- Leetcode 117. Populating Next Right Pointers in Each Node II 链接邻居2 解题报告
- 【LeetCode】11. Container With Most Water 解题报告
- [Leetcode] 395. Longest Substring with At Least K Repeating Characters 解题报告
- [leetcode] 154. Find Minimum in Rotated Sorted Array II 解题报告
- [LeetCode] Insert Interval 解题报告
- [LeetCode] Populating Next Right Pointers in Each Node II 解题报告
- [LeetCode] Median of Two Sorted Arrays 解题报告
- [LeetCode] Search for a Range 解题报告
- [LeetCode] Triangle 解题报告
- LeetCode: Letter Combinations of a Phone Number 解题报告
- [Leetcode] 403. Frog Jump 解题报告
- LeetCode解题报告 455. Assign Cookies [easy]
- LeetCode: Merge k Sorted Lists 解题报告