In a network of nodes, each node i is directly connected to another node j if and only if graph[i][j] = 1.
Some nodes initial are initially infected by malware. Whenever two nodes are directly connected and at least one of those two nodes is infected by malware, both nodes will be infected by malware. This spread of malware will continue until no more nodes can be infected in this manner.
Suppose M(initial) is the final number of nodes infected with malware in the entire network, after the spread of malware stops.
We will remove one node from the initial list. Return the node that if removed, would minimize M(initial). If multiple nodes could be removed to minimize M(initial), return such a node with the smallest index.
Note that if a node was removed from the initial list of infected nodes, it may still be infected later as a result of the malware spread.
Starting with an undirected graph (the “original graph”) with nodes from 0 to N-1, subdivisions are made to some of the edges.
The graph is given as follows: edges[k] is a list of integer pairs (i, j, n) such that (i, j) is an edge of the original graph,
and n is the total number of new nodes on that edge.
Then, the edge (i, j) is deleted from the original graph, n new nodes (x_1, x_2, ..., x_n) are added to the original graph,
and n+1 new edges (i, x_1), (x_1, x_2), (x_2, x_3), ..., (x_{n-1}, x_n), (x_n, j) are added to the original graph.
Now, you start at node 0 from the original graph, and in each move, you travel along one edge.
Return how many nodes you can reach in at most M moves.
Example 1:
Input: edge = [[0,1,10],[0,2,1],[1,2,2]], M = 6, N = 3Output: 13Explanation: The nodes that are reachable in the final graph after M = 6 moves are indicated below.
Example 2:
Input: edges = [[0,1,4],[1,2,6],[0,2,8],[1,3,1]], M = 10, N = 4
Output: 23
Note:
0 <= edges.length <= 10000
0 <= edges[i][0] < edges[i][1] < N
There does not exist any i != j for which edges[i][0] == edges[j][0] and edges[i][1] == edges[j][1].
The original graph has no parallel edges.
0 <= edges[i][2] <= 10000
0 <= M <= 10^9
1 <= N <= 3000
Solution: Dijkstra Shortest Path
Compute the shortest from 0 to rest of the nodes. Use HP to mark the maximum moves left to reach each node.
HP[u] = a, HP[v] = b, new_nodes[u][v] = c
nodes covered between a<->b = min(c, a + b)
Time complexity: O(ElogE)
Space complexity: O(E)
C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// Author: Huahua
// Running time: 88 ms
classSolution{
public:
intreachableNodes(vector<vector<int>>& edges, int M, int N) {
unordered_map<int, unordered_map<int, int>> g;
for(constauto& e : edges)
g[e[0]][e[1]] = g[e[1]][e[0]] = e[2];
priority_queue<pair<int,int>>q;// {hp, node}, sort by HP desc
unordered_map<int,int>HP;// node -> max HP left
q.push({M,0});
while(!q.empty()){
inthp=q.top().first;
intcur=q.top().second;
q.pop();
if(HP.count(cur))continue;
HP[cur]=hp;
for(constauto& pair : g[cur]) {
int nxt = pair.first;
intnxt_hp=hp-pair.second-1;
if(HP.count(nxt)||nxt_hp<0)continue;
q.push({nxt_hp,nxt});
}
}
intans=HP.size();// Original nodes covered.
for(constauto& e : edges) {
int uv = HP.count(e[0]) ? HP[e[0]] : 0;
intvu=HP.count(e[1])?HP[e[1]]:0;
ans+=min(e[2],uv+vu);
}
returnans;
}
};
Optimized Dijkstra (replace hashmap with vector)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// Author: Huahua
// Running time: 56 ms (beats 88%)
classSolution{
public:
intreachableNodes(vector<vector<int>>& edges, int M, int N) {