Network Attack
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的更多相关文章
- 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 ...
- Reading SketchVisor Robust Network Measurement for Sofeware Packet Processing
SIGCOMM17 摘要 在现有的网络测量任务中包括流量监测.数据收集和一系列网络攻击的预防.现有的基于sketch的测量算法存在严重性能损失.大量计算开销以及测量的精确性不足,而基于硬件的优化方法并 ...
- Model Inversion Attack Paper Indexpage
Paper [1]: White-box neural network attack, adversaries have full access to the model. Using Gradien ...
- NOI08冬令营 数据结构的提炼与压缩
无聊随手翻,翻到了一个这样的好东西--据结构的提炼与压缩: 为了防止以后忘记,这里把论文里的题目都纪录一下吧. 1.二维结构的化简 问题一:ural 1568 Train car sorting 定义 ...
- HTTPS中间人攻击实践(原理·实践)
前言 很早以前看过HTTPS的介绍,并了解过TLS的相关细节,也相信使用HTTPS是相对安全可靠的.直到前段时间在验证https代理通道连接时,搭建了MITM环境,才发现事实并不是我想的那样.由于 ...
- Python全栈之路----常用模块----logging模块
很多程序都有记录日志的需求,并且日志中包含的信息即有正常的程序访问日志,还可能有错误.警告等信息输出,python的logging模块提供了标准的日志接口,你可以通过它存储各种格式的日志,loggin ...
- f5 ddos cc——Mitigating DDoS Attacks with F5 Technology
摘自:https://f5.com/resources/white-papers/mitigating-ddos-attacks-with-f5-technology Mitigating Appli ...
- 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 ...
- BlackArch-Tools
BlackArch-Tools 简介 安装在ArchLinux之上添加存储库从blackarch存储库安装工具替代安装方法BlackArch Linux Complete Tools List 简介 ...
随机推荐
- Cmake 脚本对项目输出路径和输出头文件的路径定义
对Lib项目的统一输出路径以下时解决方案: set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/Lib)set(CMAKE_LIBRARY_O ...
- C++ 中 struct和class 的区别
来自:http://hi.baidu.com/pengxiangbobin19890125/blog/item/b05586eee77300212df53411.html C++ prime 中 ...
- 线性时间构造普吕弗(Prüfer)序列
tree -> sequence 首先预处理数组 deg[N], deg[i]表示编号为i的节点的度数,我们每次要删除的节点肯定是 满足deg[i]=1 的编号最小节点, 首先找到所有叶子并选出 ...
- 编程之美2015初赛第一场 hihoCoder #1156 : 彩色的树(染色问题)
#1156 : 彩色的树 时间限制:2000ms 单点时限:1000ms 内存限制:256MB 描述 给定一棵n个节点的树,节点编号为1, , …, n.树中有n - 1条边,任意两个节点间恰好有一条 ...
- 在CentOS中编译安装VIM 7.3
默认安装的 Vim 不带有多字符支持,所以不支持中文.无论是将 CentOS 本来的语系改为中文还是将 Vim 的语系设置改为中文,都不能正常显示中文.为了在 Vim 中能够正常处理中文,我们需要在编 ...
- Laravel-表单篇-零散信息
1.asset('path'):用于引入静态文件,包括css.js.img 2.分页,调用模型的paginate(每页显示的行数)方法, 如$student = Student::paginate(2 ...
- linux文件权限解说
我们进入一个目录, 执行ls -l会显示该目录下所有档案(这里用"档案"这个词, 代表文件和文件夹)的详细信息. 如图: 一共有7列信息, 分别为: [权限], [连结数], [拥 ...
- swift 模式
原文:http://www.cocoachina.com/newbie/basic/2014/0612/8800.html 模式(pattern)代表了单个值或者复合值的结构.比如,元组(1, 2)的 ...
- C++中的对象数组
类是对象的抽象,我们可以使用一个类来定义很多的对象,然后每个对象都有自己的属性. 当我们使用类来定义很多相同结构的对象的时候,我们可以采取对象数组的方法. 例如,一个班有50个学生,我们定义了一个学生 ...
- Dynamics CRM 常用 JS 方法集合
JS部分 拿到字段的值 var value= Xrm.Page.getAttribute("attributename").getValue(); Xrm.Page.getAttr ...