华为OD 电脑病毒感染

题目描述:

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

输入描述:

4
3
2 1 1
2 3 1
3 4 1
2

输出描述:

2

补充说明:

第一个参数:局域网内电脑个数N 1<=N<=200;
第二个参数:总共多少条网络连接
第三个 1 2 1 表示1->2时间为1
第七行:表示病毒最开始所在的电脑号1

 收起

示例1

输入:

4
3
2 1 1
2 3 1
3 4 1
2

输出:

2

代码 

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();//电脑数
        int m = in.nextInt();//网络数
        List<Pas> pas = new ArrayList<Pas>();
        for (int i = 0; i < m; i++) {
            int beg = in.nextInt();
            int end = in.nextInt();
            int t = in.nextInt();
            pas.add(new Pas(beg, end, t));
        }
        int x=in.nextInt();//开始感染编号
        //0 1 2 3 下标
        //1 2 3 4 电脑编号
        int[] a = new int[n];//每台电脑感染最短时间
        a[x-1]=0;
        int tx=0;
        int maxT=0;
        for (int i = 0; i < n; i++) {
            s=1<<30;
            int t=f(x,i+1,pas,0);
            if(t==1<<30){
                t=0;
                tx++;
            }
            maxT=Math.max(maxT,t);
            a[i]=t;
        }
        if(tx>1){
            System.out.println(-1);
        }else {
            System.out.println(maxT);
        }

    }
    static int s=1<<30;
    static int f(int b,int end,List<Pas> pas,int t){
        for(Pas p:pas){
            if(p.beg==b&&p.end==end){
                t+= p.t;
                s=Math.min(s,t);
                return t;
            }
            if(p.beg==b&&p.end!=end){
                f(p.end,end,pas,t+p.t);
            }
        }
        return s;
    }
}

//网络感染类
class Pas {
    int beg;
    int end;
    int t;

    public Pas() {
    }

    public Pas(int beg, int end, int t) {
        this.end = end;
        this.t = t;
        this.beg = beg;
    }

    @Override
    public String toString() {
        return "Pas{" +
                "beg=" + beg +
                ", end=" + end +
                ", t=" + t +
                '}';
    }
}

  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

LJY码云

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值