蓝桥杯 作物杂交(java)

蓝桥杯 作物杂交(java)

参考大佬用c++写的https://blog.csdn.net/weixin_45919985/article/details/114293677
只拿到了40分,求大佬帮忙看下那里有问题,或者帮忙给以下VIP的测试用例,嘻嘻。

在这里插入图片描述

java代码

import java.util.*;

public class Main {
	static class Crop{
		int getTime;
		int selfTime;
	}
	static class Parent{
		int x;
		int y;
	}

	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		int n = scan.nextInt();
		boolean exist[] = new boolean[n+1];
		int m = scan.nextInt();
		int k = scan.nextInt();
		int t = scan.nextInt();
		Crop[] crop = new Crop[n+1];
		Vector<Parent>[] compound = new Vector[n+1];
		for(int i = 1;i<=n;i++){
			crop[i] = new Crop();
			compound[i] = new Vector();
			crop[i].selfTime = scan.nextInt();
			crop[i].getTime = 999;
			
		}
		for(int i= 1;i<=m;i++){
			int a = scan.nextInt();
			exist[a] = true;
			crop[a].getTime = 0;
		}
		for(int i = 0;i<k;i++){
			int a = scan.nextInt();
			int b = scan.nextInt();
			Parent temp = new Parent();
			temp.x = a;
			temp.y = b;
			int c = scan.nextInt();
			compound[c].add(temp);
				
		}
		for(int i=0;i<compound[t].size();i++){
			crop[t].getTime = Math.min(crop[t].getTime, dfs(compound[t].elementAt(i).x,compound[t].elementAt(i).y,t,exist,compound,crop));
		}
		System.out.println(crop[t].getTime);

	}
	public static int dfs(int a,int b,int t,boolean[] exist,Vector<Parent>[] compound,Crop[] crop){
		if(!exist[a]){//作物a还没有被合成,求出它的最短合成时间。
			for(int i=0;i<compound[a].size();i++){
				crop[a].getTime = Math.min(crop[a].getTime, dfs(compound[a].elementAt(i).x,compound[a].elementAt(i).y,a,exist,compound,crop));
			}
		}
		if(!exist[b]){//作物b还没有被合成,求出它的最短合成时间。
			for(int i=0;i<compound[b].size();i++){
				crop[b].getTime = Math.min(crop[b].getTime, dfs(compound[b].elementAt(i).x,compound[b].elementAt(i).y,b,exist,compound,crop));
			}
		}
		if(!exist[a]||!exist[b]){//作物a或者作物b还没有被合成则无法合成作物t,直接返回999;
			return 999;
		}else{
		exist[t] = true;
		return (Math.max(crop[a].getTime, crop[b].getTime)+Math.max(crop[a].selfTime, crop[b].selfTime));
		}
	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值