蓝桥杯 作物杂交(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));
}
}
}