Java 在不超过给定能量的情况下,完成一段距离的最短时间
我试图用BFS解决这个问题,但在很多情况下都被卡住了。对于给定的问题陈述,我希望有一个有效的工作解决方案 问题:给定总物理能量H和总距离D。给出了可用于行驶的各种速度和相应的物理能量消耗。找出覆盖总距离D所需的最短时间,确保总能耗不超过H 以下是我编写的JAVA代码:Java 在不超过给定能量的情况下,完成一段距离的最短时间,java,dynamic-programming,graph-theory,graph-algorithm,breadth-first-search,Java,Dynamic Programming,Graph Theory,Graph Algorithm,Breadth First Search,我试图用BFS解决这个问题,但在很多情况下都被卡住了。对于给定的问题陈述,我希望有一个有效的工作解决方案 问题:给定总物理能量H和总距离D。给出了可用于行驶的各种速度和相应的物理能量消耗。找出覆盖总距离D所需的最短时间,确保总能耗不超过H 以下是我编写的JAVA代码: public class physicalEnergy { public static class nodes{ int energy; int distance; int
public class physicalEnergy {
public static class nodes{
int energy;
int distance;
int time;
public nodes(int energy , int distance ,int time) {
this.energy=energy;
this.distance=distance;
this.time=time;
}
}
public static int getMinTime(int arr[] , HashMap<Integer,Integer> hm , int dist , int energy) {
Arrays.sort(arr);
Queue<nodes> q= new LinkedList<nodes>();
for(int i=arr.length-1;i>=0;i--) {
if(arr[i]==dist && hm.get(arr[i])<=energy) {
return 1;
}
if(arr[i]<dist && hm.get(arr[i])<energy) {
q.add(new nodes(hm.get(arr[i]),arr[i],1));
}
}
while(!q.isEmpty()) {
nodes temp= q.remove();
for(int i=arr.length-1;i>=0;i--) {
if(temp.distance+arr[i]==dist && temp.energy+hm.get(arr[i])<=energy) {
return temp.time+1;
}
if(temp.distance+arr[i]<dist && temp.energy+hm.get(arr[i])<energy) {
q.add(new nodes(temp.energy+hm.get(arr[i]),temp.distance+arr[i],temp.time+1));
}
}
}
return -1;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int arr[]= { 10,30,50,20,70}; //speed per hour
int energy[]= {2,25,35,15,50}; //energy per hour
int distance= 70; //total distance
int e= 45; //total energy should not exceed this limit
HashMap<Integer,Integer> hm= new HashMap<Integer,Integer>();
for(int i=0;i<arr.length;i++) {
hm.put(arr[i],energy[i]);
}
int result= getMinTime(arr,hm,distance,e);
System.out.println(result);
}
}
公共级物理能{
公共静态类节点{
国际能源;
整数距离;
整数时间;
公共节点(整数能量、整数距离、整数时间){
这个。能量=能量;
这个。距离=距离;
这个。时间=时间;
}
}
公共静态int-getMinTime(int-arr[],HashMap-hm,int-dist,int-energy){
数组。排序(arr);
队列q=新的LinkedList();
对于(int i=arr.length-1;i>=0;i--){
如果(arr[i]==dist&&hm.get(arr[i])我看不到广度优先搜索实现“有效工作解决方案”的价值,您可以使用简单的单循环解决方案:
//returned value is not necessarily int
public static double getMinTime(HashMap<Integer,Integer> speedEnerhgyMap
, int dist , int energyLimit) {
List<Integer> speeds = new ArrayList<>(speedEnerhgyMap.keySet());
Collections.sort(speeds, Collections.reverseOrder()); //highest speed first
//iterate starting at highest speed = shortest time
for(int speed : speeds) {
double time = (double)dist / speed; //code verbose to make it clear
double energyUsed = time * speedEnerhgyMap.get(speed);
if(energyUsed <= energyLimit) {
return time;
}
}
return Double.NaN;
}
//返回的值不一定是int
公共静态双getMinTime(HashMap speedEnerhgyMap
,int dist,int energyLimit){
列表速度=新的数组列表(speedEnerhgyMap.keySet());
Collections.sort(速度,Collections.reverseOrder());//最高速度优先
//以最高速度开始迭代=最短时间
用于(整数速度:速度){
double time=(double)dist/speed;//详细编码以使其清晰
双能量消耗=时间*速度能量平均值(速度);
如果复制粘贴出现问题,您可以单击“编辑”解决您的问题。@m69已解决。您现在可以看一看。如果这个问题来自在线评委,您可以发布一个链接吗?您使用的方法是正确的-请注意不要选中等于dist,而是=>
,因为您可能会超出此距离,而不仅仅返回临时时间+1
,而是保存min va所有这些选项的lue,并最终返回最小值