解決策が必要な場合は、練習用に書きました。これは動的計画法の例です。これは、ソリューションが反復計算を控えていることを意味します。問題がサブユニットに分割でき、それらのサブユニットがいくつかのシナリオで繰り返される場合、それらを複数回解決する必要はありません。このような場合は、最初に解決策を保存し、後で再利用します。
package com.zafar;
import java.util.Scanner;
public class AssemblyLineProblem {
public static void main(String arg[]){
Scanner scanner =new Scanner(System.in);
int numberOfStations=scanner.nextInt();
Station firstLine[]=new Station[numberOfStations];
Station secondLine[]=new Station[numberOfStations];
for(int i=0;i<numberOfStations;i++){
firstLine[i]=new Station(i,scanner.nextInt());
firstLine[i].setIndex(i);
firstLine[i].setTransferCost(scanner.nextInt());
firstLine[i].setNameOfLine("line 1");
}
for(int i=0;i<numberOfStations;i++){
secondLine[i]=new Station(i,scanner.nextInt());
secondLine[i].setIndex(i);
secondLine[i].setTransferCost(scanner.nextInt());
secondLine[i].setNameOfLine("line 2");
}
int entryCostLine1=scanner.nextInt();
int entryCostLine2=scanner.nextInt();
Station beginStation=findOptimalRoute(numberOfStations, firstLine, secondLine, entryCostLine1, entryCostLine2);
System.out.println("The optimal route is");
print(beginStation);
scanner.close();
}
public static void print(Station station){
if(station==null)
return;
System.out.println(station.getNameOfLine()+", station "+station.getIndex()+", cost from here: "+station.getOptimalCostFromThisStation());
print(station.getNextOptimalStation());
}
public static Station findOptimalRoute(int numberOfStations,
Station[] firstLine, Station[] secondLine, int entryCostLine1,
int entryCostLine2) {
for(int i=numberOfStations-1;i>=0;i--){
if(i==numberOfStations-1)
{
firstLine[i].setOptimalCostFromThisStation(firstLine[i].getTransferCost());
secondLine[i].setOptimalCostFromThisStation(secondLine[i].getTransferCost());
}else{
calculateOptimalStation(firstLine, secondLine, i);
calculateOptimalStation(secondLine, firstLine, i);
}
}
if((entryCostLine1+firstLine[0].getCost()+firstLine[0].getOptimalCostFromThisStation())>= (entryCostLine2+secondLine[0].getCost()+secondLine[0].getOptimalCostFromThisStation())){
return secondLine[0];
}else
return firstLine[0];
}
public static void calculateOptimalStation(Station[] currentLine, Station[] otherLine, int indexOfCurrentStation){
int costOnContinuingOnSameLine= (currentLine[indexOfCurrentStation+1].getCost()+currentLine[indexOfCurrentStation+1].getOptimalCostFromThisStation());
int costOnSwitchingLines=(currentLine[indexOfCurrentStation].getTransferCost()+otherLine[indexOfCurrentStation+1].getCost()+otherLine[indexOfCurrentStation+1].getOptimalCostFromThisStation()) ;
if((costOnContinuingOnSameLine <= costOnSwitchingLines)){
currentLine[indexOfCurrentStation].setOptimalCostFromThisStation(costOnContinuingOnSameLine);
currentLine[indexOfCurrentStation].setNextOptimalStation(currentLine[indexOfCurrentStation+1]);
}
else{
currentLine[indexOfCurrentStation].setOptimalCostFromThisStation(costOnSwitchingLines);
currentLine[indexOfCurrentStation].setNextOptimalStation(otherLine[indexOfCurrentStation+1]);
}
}
}
class Station{
private String nameOfLine;
private int index;
private int cost;
private int transferCost;
private Station nextOptimalStation;
private int optimatCostFromThisStation=0;
public Station(int index, int cost){
this.index=index;
this.cost=cost;
}
public int getIndex() {
return index;
}
public void setIndex(int index) {
this.index = index;
}
public int getCost() {
return cost;
}
public void setCost(int cost) {
this.cost = cost;
}
public Station getNextOptimalStation() {
return nextOptimalStation;
}
public void setNextOptimalStation(Station nextOptimalStation) {
this.nextOptimalStation = nextOptimalStation;
}
public int getTransferCost() {
return transferCost;
}
public void setTransferCost(int transferCost) {
this.transferCost = transferCost;
}
public int getOptimalCostFromThisStation() {
return optimatCostFromThisStation;
}
public void setOptimalCostFromThisStation(int optimatCostFromThisStation) {
this.optimatCostFromThisStation = optimatCostFromThisStation;
}
public String getNameOfLine() {
return nameOfLine;
}
public void setNameOfLine(String nameOfLine) {
this.nameOfLine = nameOfLine;
}
}
入力: 6
7 2 9 3 3 1 4 3 8 4 4 3
8 2 5 1 6 2 4 2 5 1 7 2
2 4
出力:
最適ルートは
路線 1、駅 0、ここからのコスト: 29
ライン 2、駅 1、ここからのコスト: 22
ライン 1、駅 2、ここからのコスト: 18
2 号線、3 駅、ここからの料金: 13
2 号線、4 駅、ここからの料金: 8
1番線、5番駅、ここからの料金: 3
その他のリソース: https://parasol.tamu.edu/~welch/teaching/411.f14/dp.pdf