Advertisement
ptrawt

Problem B Burglary

Dec 1st, 2015
314
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.86 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Scanner;
  3.  
  4. public class HelloWorld {
  5.     public static ArrayList<Vertice> node;
  6.    
  7.     public static int budget;
  8.     public static void main(String[] args) {
  9.         Scanner sc = new Scanner(System.in);
  10.         int testcase = sc.nextInt();
  11.         sc.nextLine();
  12.         for(int i = 0;i != testcase;i++){
  13.             node = new ArrayList<Vertice>();
  14.             int vertice = sc.nextInt();
  15.             budget = sc.nextInt();
  16.             sc.nextLine();
  17.             for(int j = 0;j != vertice;j++){
  18.                 node.add(new Vertice());
  19.                 node.get(j).setPrize(sc.nextInt());
  20.                 node.get(j).setCost(0);
  21.             }
  22.             sc.nextLine();
  23.             String pathtemp = sc.nextLine();
  24.             String[] path = pathtemp.split(" ");
  25.             String pathcosttemp = sc.nextLine();
  26.             String[] pathcost = pathcosttemp.split(" ");
  27.             for(int j = 0;j != path.length;j = j +2){
  28.                 node.get(Integer.parseInt(path[j])-1).child.add(Integer.parseInt(path[j+1])-1);
  29.                 node.get(Integer.parseInt(path[j+1])-1).setCost(Integer.parseInt(pathcost[j/2]));
  30.                        
  31.             }
  32.             IntegerPair start = new IntegerPair(0,budget);
  33.             IntegerPair result = dfs(0,start);
  34.             System.out.print(result.prize+" ");
  35.             System.out.println(result.budgetleft);
  36.         }
  37.        
  38.     }
  39.    
  40.     public static IntegerPair dfs(int nodeindex,IntegerPair prizebudget){
  41.         Vertice currentnode = node.get(nodeindex);
  42.        
  43.         if(prizebudget.budgetleft - currentnode.cost < 0){
  44.             return new IntegerPair(prizebudget.prize,prizebudget.budgetleft);
  45.         }
  46.         prizebudget.prize += currentnode.prize;
  47.         prizebudget.budgetleft -= currentnode.cost;
  48.         System.out.println(nodeindex+" "+prizebudget.budgetleft+" "+prizebudget.prize);
  49.         IntegerPair best = new IntegerPair(prizebudget.prize,prizebudget.budgetleft);
  50.         IntegerPair result = new IntegerPair(0,0);
  51.         for(int i = 0;i != currentnode.child.size();i++){
  52.             result = dfs(currentnode.child.get(i),new IntegerPair(prizebudget.prize,prizebudget.budgetleft));
  53.             if(best.prize <= result.prize){
  54.                 best = result;
  55.             }
  56.         }
  57.         return best;
  58.     }
  59.    
  60. }
  61.  
  62. class Vertice{
  63.     public int prize;
  64.     public int cost;
  65.     public ArrayList<Integer> child;
  66.     public Vertice(){
  67.         child = new ArrayList<Integer>();
  68.     }
  69.     public void setPrize(int prize){
  70.         this.prize = prize;
  71.     }
  72.     public void setCost(int cost){
  73.         this.cost = cost;
  74.     }
  75. }
  76.  
  77. class IntegerPair{
  78.     public int prize;
  79.     public int budgetleft;
  80.     public IntegerPair(int prize,int budgetleft){
  81.         this.prize = prize;
  82.         this.budgetleft = budgetleft;
  83.     }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement