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 简介 ...
随机推荐
- 关于TCP的三次握手和四次分手(整理)
这个协议非常重要,这里把它的链接和释放整理一下 首先是三次握手: 1. 客户端发起,像服务器发送的报文SYN=1,ACK=0,然后选择了一个初始序号:seq=x. SYN是干什么用的? 在链接的时候 ...
- mysql c haracter
基本概念 • 字符(Character)是指人类语言中最小的表义符号.例如’A'.’B'等:• 给定一系列字符,对每个字符赋予一个数值,用数值来代表对应的字符,这一数值就是字符的编码(Encoding ...
- PHP中文汉字验证码
hb.ttf换成随便你自己下载的ttf Header("Content-type: image/PNG"); $str="的一是在了不和有大这主中人上为们地个用工时要动国 ...
- pyqt颜色字符
from PyQt4.QtGui import QPlainTextEdit, QWidget, QVBoxLayout, QApplication, \ QFileDialog, QMessageB ...
- FileUtil.java
package com.founder.util.file; import java.io.BufferedReader; import java.io.File; import java.io.Fi ...
- CentOS系统、Jdk、Tomcat安装实战
CentOS系统.Jdk.Tomcat安装实战 第一次接触Liunx系统,都说J2EE系统在Li ...
- [Angular 2] WebStorm - Managing Imports
Some tips for import libaray by using webstorm: // Alt + Enter --> Auto Import // Ctrl + Alt + o ...
- Android Fragment详解(六):Fragement示例
把条目添加到动作栏 你的fragment们可以向activity的菜单(按Manu键时出现的东西)添加项,同时也可向动作栏(界面中顶部的那个区域)添加条目,这都需通过实现方法onCreateOptio ...
- jquery获取当前元素坐标
1. jquery获取当前元素坐标 A) 获取对象
- python进阶之路4.1---生成器与迭代器
*:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important; } /* ...