Advertisement
Guest User

test

a guest
Dec 21st, 2014
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.42 KB | None | 0 0
  1. package Opdracht3;
  2.  
  3. import java.io.InputStream;
  4. import java.util.*;
  5.  
  6.  
  7. class Solution{
  8.    
  9.     public Solution(){ }
  10.  
  11.     public static String run(InputStream in){
  12.         Scanner sc = new Scanner(in);
  13.          int n = sc.nextInt();
  14.          int constantCost = sc.nextInt();
  15.          if(n == 0){
  16.              return "0";
  17.          }
  18.          
  19.          if(n == 1){
  20.              return Integer.toString(constantCost);
  21.          }
  22.          int[][] risk = new int[n+1][n+1]; // double array for all the risks
  23.          while(sc.hasNextInt()){
  24.             int from = sc.nextInt();
  25.             int to = sc.nextInt();
  26.             int cost = sc.nextInt();
  27.             risk[from][to] = cost;
  28.          }
  29.          
  30.          int[] list = new int[n+1];
  31.          list[0] = 0;
  32.          list[1] = constantCost;
  33.          
  34.          for(int i=2; i<=n; i++){
  35.              int minimal = Integer.MAX_VALUE;
  36.              
  37.              for(int p=1; p<=i-1; p++){
  38.                 int newcost = risk[p][i] + list[p-1] + constantCost;
  39.                 int previous = list[i-1] + constantCost;
  40.                 int min;
  41.                 if(newcost < previous)
  42.                     min = newcost;
  43.                 else{
  44.                     min = previous;
  45.                 }
  46.                
  47.                 if(min < minimal)
  48.                     minimal = min;
  49.              }
  50.              list[i] = minimal;
  51.              
  52.          }
  53.          String result = Integer.toString(list[n]);
  54.          return result;
  55.          
  56.     }  
  57.        
  58.      
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement