Network Attack

Nicola regularly inspects the local networks for security issues. He uses a smart and aggressive program which takes control of computers on the network. This program attacks all connected computers simultaneously, then uses the captured computers for further attacks. Nicola started the virus program in the first computer and took note of the time it took to completely capture the network. We can help him improve his process by modeling and improving his inspections.

We are given information about the connections in the network and the security level for each computer. Security level is the time (in minutes) that is required for the virus to capture a machine. Capture time is not related to the number of infected computers attacking the machine. Infection start from the 0th computer (which is already infected). Connections in the network are undirected. Security levels are not equal to zero.

Information about a network is represented as a matrix NxN size, where N is a number of computers. If ith computer connected with jth computer, then matrix[i][j] == matrix[j][i] == 1, else 0. Security levels are placed in the main matrix diagonal, so matrix[i][i] is the security level for the ith computer.

You should calculate how much time is required to capture the whole network in minutes.

Input: Network information as a list of lists with integers.

Output: The total time of taken to capture the network as an integer.

原题链接: http://www.checkio.org/mission/network-attack/

题目大义: 有N台电脑, 给定邻接矩阵, 每个电脑有被感染的时间, 计算感染整个网络电脑的时间

思路: 贪心算法, 每次感染距离0号电脑最近的电脑(时间最短), 最后感染的电脑时间即为网络感染时间

 def capture(matrix):
infected_time = {0:0} #key is the computer id and value is the 0 computer to id computer's minutes
infected_id = set() #store the infected computers id rel = 0 while infected_time:
#select the minimum minutes computer
top_computer = min(infected_time.items(), key=lambda d:d[1]) rel = top_computer[1] infected_id.add(top_computer[0]) #find the adjacent computers and update the minutes
for c_id, connect in enumerate(matrix[top_computer[0]]):
if connect and c_id != top_computer[0] and c_id not in infected_id:
tmp_minutes = infected_time[top_computer[0]] + matrix[c_id][c_id]
if infected_time.get(c_id) == None or infected_time[c_id] > tmp_minutes:
infected_time[c_id] = tmp_minutes #pop the top_computer
infected_time.pop(top_computer[0]) return rel

在思路成熟之后, 实际上我也考虑过优先队列的实现, 无奈搜网上的代码都感觉到比较复杂, 然而观摩Sim0000的代码后, 受益颇多

 from heapq import heappush, heappop

 def capture(matrix):
n = len(matrix)
q = [(0,0)] # time, node
tmin = {} # dictionary of min time for each node while(q):
time, node = heappop(q)
if node in tmin: continue # already visited
tmin[node] = time # first visit means min time of node
if len(tmin) == n: break
for i, connect in enumerate(matrix[node]):
if i != node and connect and i not in tmin:
heappush(q, (time + matrix[i][i], i))
return max(tmin.values()) # use heapq as priority queue
# BFS search

Network Attack的更多相关文章

  1. URAL 1557 Network Attack 图论,连通性,tarjain,dfs建树,分类讨论 难度:2

    http://acm.timus.ru/problem.aspx?space=1&num=1557 1557. Network Attack Time limit: 2.0 secondMem ...

  2. Reading SketchVisor Robust Network Measurement for Sofeware Packet Processing

    SIGCOMM17 摘要 在现有的网络测量任务中包括流量监测.数据收集和一系列网络攻击的预防.现有的基于sketch的测量算法存在严重性能损失.大量计算开销以及测量的精确性不足,而基于硬件的优化方法并 ...

  3. Model Inversion Attack Paper Indexpage

    Paper [1]: White-box neural network attack, adversaries have full access to the model. Using Gradien ...

  4. NOI08冬令营 数据结构的提炼与压缩

    无聊随手翻,翻到了一个这样的好东西--据结构的提炼与压缩: 为了防止以后忘记,这里把论文里的题目都纪录一下吧. 1.二维结构的化简 问题一:ural 1568 Train car sorting 定义 ...

  5. HTTPS中间人攻击实践(原理·实践)

      前言 很早以前看过HTTPS的介绍,并了解过TLS的相关细节,也相信使用HTTPS是相对安全可靠的.直到前段时间在验证https代理通道连接时,搭建了MITM环境,才发现事实并不是我想的那样.由于 ...

  6. Python全栈之路----常用模块----logging模块

    很多程序都有记录日志的需求,并且日志中包含的信息即有正常的程序访问日志,还可能有错误.警告等信息输出,python的logging模块提供了标准的日志接口,你可以通过它存储各种格式的日志,loggin ...

  7. f5 ddos cc——Mitigating DDoS Attacks with F5 Technology

    摘自:https://f5.com/resources/white-papers/mitigating-ddos-attacks-with-f5-technology Mitigating Appli ...

  8. Endless looping of packets in TCP/IP networks (Routing Loops)

    How endless looping of packets in a TCP/IP network might occur? Router is a device used to interconn ...

  9. BlackArch-Tools

    BlackArch-Tools 简介 安装在ArchLinux之上添加存储库从blackarch存储库安装工具替代安装方法BlackArch Linux Complete Tools List 简介 ...

随机推荐

  1. 在OCX初始化时获取其在网页中的DOM对象

    OCX初始化的时候会调用SetClientSite,会传入IOleClientSite对象. CComQIPtr<IOleControlSite, &IID_IOleControlSit ...

  2. c语言验证哥德巴赫猜想(从4开始 一个偶数由两个质数之和)

    #include <stdio.h> #include <stdlib.h> #include <math.h> int isit(int num) { int i ...

  3. c语言结构体1之定义

    这是在复习阶段随便小结的一些东西 别喷哦 结构体定义的三种方式 注意事项: 1结构体括号后面有分号 2#define得放在程序上面 3成员名可以和结构体名相同 4结构体类型不能直接访问成员,也不能赋值 ...

  4. 论 <%@taglib prefix="s" uri="/struts-tags" %> 的重要性

    前段时间在做项目的时候,碰到这个问题 结果是相应的内容显示不出来,原来是忘了这句很关键的引入:<%@taglib prefix="s" uri="/struts-t ...

  5. hdu 4869 Turn the pokers(组合数+费马小定理)

    Problem Description During summer vacation,Alice stay at home for a long time, with nothing to do. S ...

  6. python3-day4(yield)

    1.yield 迭代器是访问集合元素的一种方式.迭代器对象从集合的第一个元素开始访问,直到所有的元素被访问完结束.迭代器只能往前不会后退,不过这也没什么,因为人们很少在迭代途中往后退.另外,迭代器的一 ...

  7. WebService--使用 CXF 开发 REST 服务

    现在您已经学会了如何使用 CXF 开发基于 SOAP 的 Web 服务,也领略了 Spring + CXF 这个强大的组合,如果您错过了这精彩的一幕,请回头看看这篇吧: Web Service 那点事 ...

  8. 正则表达式获取URL参数

    使用到的正则表达式: [^\?&]?参数名=[^&]+ document.location.getURLPara = function (name) { var reg = new R ...

  9. C++——try、throw、catch实例学习程序

    #include<iostream> #include<stdexcept> //exception/stdexcept/new/type_info头文件里都有定义的标准异常类 ...

  10. 当chm文档点击左侧,右侧无内容时的解决方案

    右击chm文件->属性->安全选项卡,选择你登陆计算机的用户名,把权限改成完全控制就可以显示了