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. openwrt l7过滤qos配置

    openwrt l7过滤qos配置     电梯直达 1# 本帖最后由 木鸟 于 2010-7-27 10:22 编辑 openwrt的qos基于hsfc.提供了分类标记,流量控制等功能,可能还有整形 ...

  2. VS2010之MFC串口通信的编写教程--转

    http://wenku.baidu.com/link?url=K1XPdj9Dcf2of_BsbIdbPeeZ452uJqiF-s773uQyMzV2cSaPRIq6RddQQH1zr1opqVBM ...

  3. Decimal

    Description 任意一个分数都是有理数,对于任意一个有限小数,我们都可以表示成一个无限循环小数的形式(在其末尾添加0),对于任意一个无限循环小数都可以转化成一个分数.现在你的任务就是将任意一个 ...

  4. The 2015 China Collegiate Programming Contest Game Rooms

    Game Rooms Time Limit: 4000/4000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Submi ...

  5. C# - List操作 - 按照字母排序

    有Family的类如下: public class FamilyModel { public string Name { set; get; } } 创建List List<FamilyMode ...

  6. select与epoll分析

    关于select与epoll的区别,网上的文章已是一大堆.不过别人的终究是别人的,总得自己去理解才更深刻.于是在阅读了大量的文章后,再装模作样的看下源码,写下了自己的一些理解. 在开始之前,要明白li ...

  7. html a标签打开邮件

    <a href="mailto:frotech@foxmail.com" target="_blank">frotech@foxmail.com&l ...

  8. javaweb文件下载

    最近搞了一下struts文件上传下载了,一个是通过struts自带的类实现的下载方法,一个是通用的下载方法: struts实现: FileDownloadAction.java package com ...

  9. Multithreading: How to Use the Synchronization Classes

    (Owed by: 春夜喜雨 http://blog.csdn.net/chunyexiyu 转载请标明来源) 翻译文章来源:  msdn - Multithreading: How to Use t ...

  10. 未能载入文件或程序集“DAL”或它的某一个依赖项。系统找不到指定的文件。

    这个一般出如今三层给B层与D层之间加抽象工厂-接口-映射.时候出的错.出错的地方是抽象工厂. --如图 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTA ...