电脑病毒感染-图论

一、题目描述

一个局域网内有很多台电脑,分别标注为0~N-1的数字。相连接的电脑距离不一样,所以感染时间不一样,感染时间用t表示。其中网络内一台电脑被病毒感染,求其感染网络内所有的电脑最少需要多长时间。如果最后有电脑不会感染,则返回-1。给定一个数组times表示一台电脑把相邻电脑感染所用的时间。pathi={i, j, t}表示:电脑i->j,电脑i上的病毒感染j,需要时间t。

输入描述

第一行输入一个整数N,表示局域网内电脑个数N,1≤N≤200;

第二行输入一个整数M ,表示有 M 条网络连接曰;接下来M行,每行输入为i,j,t。表示电脑ì感染电脑j需要时间t。(1≤i,j≤N)最后一行为病毒所在的电脑编号

输出描述

输出最少需要多少时间才能感染全部电脑,如果不存在输出-1

示例1:

输入

复制代码
4
3
2 1 1
2 3 1
3 4 1
2

输出:2
示例2:

输入

复制代码
3
3
2 1 1
2 3 1
3 4 1
2

输出:

复制代码
2

代码思路

华为OD机试真题---电脑病毒感染_一个局域网内有很多台电脑,分别标注为0 - n-1的数字-CSDN博客

java 复制代码
package com.shitou;

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n= in.nextInt();//n个电脑 0~n-1
        int m= in.nextInt();//m条线
        int[][] ver = new int[m][3];
        for(int i=0;i<m;i++){
            ver[i][0] = in.nextInt();//带感染的电脑
            ver[i][1] = in.nextInt();//被感染的电脑
            ver[i][2] = in.nextInt();//时间
        }
        int k = in.nextInt();//感染源的电脑
        int time = networkDeleyTime(ver,k,n,m);
        System.out.println(time);
    }
    public static int networkDeleyTime(int[][] ver,int k,int n,int m){
        Map<Integer,List<Integer[]>> graph = new HashMap<>();
        for(int i=0;i<m;i++){
            if(graph.containsKey(ver[i][0])){
                graph.get(ver[i][0]).add(new Integer[]{ver[i][1],ver[i][2]});
            }else{
                graph.put(ver[i][0],new ArrayList<Integer[]>());
                graph.get(ver[i][0]).add(new Integer[]{ver[i][1],ver[i][2]});
            }
        }
        Map<Integer,Integer> dist = new HashMap<>();
        dist.put(k,0);
        PriorityQueue<Integer[]> queue =new PriorityQueue<>(Comparator.comparingInt(a->a[1]));
        queue.offer(new Integer[]{k,0});

        while(!queue.isEmpty()){
            Integer[] cur = queue.poll();
            int node =cur[0];
            int time=cur[1];
            if(graph.containsKey(node)){
                List<Integer[]> l1 =graph.get(node);
                for (int i=0;i<l1.size();i++){
                    Integer[] t=l1.get(i);

                    if(!dist.containsKey(t[0])||dist.get(t[0])>time+t[1]){
                        dist.put(t[0],time+t[1]);
                        queue.offer(new Integer[]{t[0],time+t[1]});
                    }
                }
            }
        }
        if(dist.size()<n){
            return -1;
        }
        int maxdelay =0;
        for(Integer time: dist.values()){
            maxdelay =Math.max(time,maxdelay);
        }
        return maxdelay;
    }
}
相关推荐
-dzk-5 天前
【图论】LC 207.课程表
图论
wuyk5556 天前
18.Kruskal 算法:用最短的边连成一张网
开发语言·算法·图论
-dzk-6 天前
【图论】LC 994.腐烂的橘子
图论
-dzk-7 天前
【图论】LC 200.岛屿数量
深度优先·图论
Lyyaoo.8 天前
【图论】岛屿数量/腐烂的橘子/课程表/实现前缀树
图论
lvwangshu9 天前
图论:LCA、树的直径、树的重心、二分图与 Tarjan 缩点
算法·图论
hansang_IR11 天前
【题解】[APIO2023] 赛博乐园 / cyberland
c++·算法·图论
aqiu11111112 天前
【并查集 / 图论】蓝桥云课 - 魔法大陆的城市群(求无向图连通块数量)题解
蓝桥杯·图论
lvwangshu17 天前
P6534 [COCI 2015/2016 #1] UZASTOPNI 等差树列 题解
动态规划·图论·题解·性质题
positive_zpc20 天前
进阶数据结构图——关键路径(四)
数据结构·图论·关键路径