LeetCode周赛#107 Q4 Minimize Malware Spread II(bfs)

题目来源:https://leetcode.com/contest/weekly-contest-107/problems/minimize-malware-spread-ii/

问题描述

928. Minimize Malware Spread II

 (This problem is the same as Minimize Malware Spread, with the differences bolded.)

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, completely removing it and any connections from this node to any other node.  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.

 

Example 1:

Input: graph = [[1,1,0],[1,1,0],[0,0,1]], initial = [0,1]
Output: 0

Example 2:

Input: graph = [[1,1,0],[1,1,1],[0,1,1]], initial = [0,1]
Output: 1

Example 3:

Input: graph = [[1,1,0,0],[1,1,1,0],[0,1,1,1],[0,0,1,1]], initial = [0,1]
Output: 1

 

Note:

  1. 1 < graph.length = graph[0].length <= 300
  2. 0 <= graph[i][j] == graph[j][i] <= 1
  3. graph[i][i] = 1
  4. 1 <= initial.length < graph.length
  5. 0 <= initial[i] < graph.length

------------------------------------------------------------

题意

类似LeetCode周赛#106 Q4 Minimize Malware Spread (bfs)。给定图的邻接矩阵和初始感染节点,问去掉哪个节点(此题的“去掉”是指从图中删去该节点而非从初始感染节点集中去掉),最后被感染的节点数最少。定义感染为与感染节点相邻的节点都会被感染,并具有传递性。

------------------------------------------------------------

思路

LeetCode周赛#106 Q4 Minimize Malware Spread (bfs)。bfs,只是注意bfs的时候永远不要经过那个被删去的节点。

------------------------------------------------------------

代码

class Solution {
public:
    int bfs(vector<vector<int>>& graph, vector<int>& initial, int rem)
    {
        queue<int> q;
        int i, ninit = initial.size(), n = graph.size(), cnt = 0, h;
        bool vis[305] = {};
        for (i=0; i<ninit; i++)
        {
            if (i != rem)
            {
                q.push(initial[i]);
                vis[initial[i]] = 1;
                cnt++;
            }
        }
        while (!q.empty())
        {
            h = q.front();
            q.pop();
            for (i=0; i<n; i++)
            {
                if (i!=initial[rem] && graph[h][i] == 1 && !vis[i])
                {
                    q.push(i);
                    vis[i] = 1;
                    cnt++;
                }
            }
        }
        return cnt;
    }
    int minMalwareSpread(vector<vector<int>>& graph, vector<int>& initial) {
        int i, ninit = initial.size(), minv = 0x3f3f3f3f, minid = -1, ans;
        sort(initial.begin(), initial.end());
        for (i=0; i<ninit; i++)
        {
            ans = bfs(graph, initial, i);
            if (ans < minv)
            {
                minv = ans;
                minid = initial[i];
            }
        }
        return minid;
    }
};

猜你喜欢

转载自blog.csdn.net/da_kao_la/article/details/83268292
Q4