LeetCode周赛#106 Q4 Minimize Malware Spread (bfs)

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

问题描述

924. Minimize Malware Spread

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.

 

Example 1:

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

Example 2:

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

Example 3:

Input: graph = [[1,1,1],[1,1,1],[1,1,1]], initial = [1,2]
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

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

题意

给定一个图的邻接矩阵和初始点集的数组,问从初始点集中去掉哪个点,最终从初始点集派生出的最大连通分量包含的点数最小。如果有多个点作用相同,则输出索引最小的点。

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

思路

遍历去除初始点集中的每一个点,分别求最大连通分量(BFS),取最小值。这题不配LeetCode Hard难度。

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

代码

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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值