Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.Scanner;
- public class HelloWorld {
- public static ArrayList<Vertice> node;
- public static int budget;
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- int testcase = sc.nextInt();
- sc.nextLine();
- for(int i = 0;i != testcase;i++){
- node = new ArrayList<Vertice>();
- int vertice = sc.nextInt();
- budget = sc.nextInt();
- sc.nextLine();
- for(int j = 0;j != vertice;j++){
- node.add(new Vertice());
- node.get(j).setPrize(sc.nextInt());
- node.get(j).setCost(0);
- }
- sc.nextLine();
- String pathtemp = sc.nextLine();
- String[] path = pathtemp.split(" ");
- String pathcosttemp = sc.nextLine();
- String[] pathcost = pathcosttemp.split(" ");
- for(int j = 0;j != path.length;j = j +2){
- node.get(Integer.parseInt(path[j])-1).child.add(Integer.parseInt(path[j+1])-1);
- node.get(Integer.parseInt(path[j+1])-1).setCost(Integer.parseInt(pathcost[j/2]));
- }
- IntegerPair start = new IntegerPair(0,budget);
- IntegerPair result = dfs(0,start);
- System.out.print(result.prize+" ");
- System.out.println(result.budgetleft);
- }
- }
- public static IntegerPair dfs(int nodeindex,IntegerPair prizebudget){
- Vertice currentnode = node.get(nodeindex);
- if(prizebudget.budgetleft - currentnode.cost < 0){
- return new IntegerPair(prizebudget.prize,prizebudget.budgetleft);
- }
- prizebudget.prize += currentnode.prize;
- prizebudget.budgetleft -= currentnode.cost;
- System.out.println(nodeindex+" "+prizebudget.budgetleft+" "+prizebudget.prize);
- IntegerPair best = new IntegerPair(prizebudget.prize,prizebudget.budgetleft);
- IntegerPair result = new IntegerPair(0,0);
- for(int i = 0;i != currentnode.child.size();i++){
- result = dfs(currentnode.child.get(i),new IntegerPair(prizebudget.prize,prizebudget.budgetleft));
- if(best.prize <= result.prize){
- best = result;
- }
- }
- return best;
- }
- }
- class Vertice{
- public int prize;
- public int cost;
- public ArrayList<Integer> child;
- public Vertice(){
- child = new ArrayList<Integer>();
- }
- public void setPrize(int prize){
- this.prize = prize;
- }
- public void setCost(int cost){
- this.cost = cost;
- }
- }
- class IntegerPair{
- public int prize;
- public int budgetleft;
- public IntegerPair(int prize,int budgetleft){
- this.prize = prize;
- this.budgetleft = budgetleft;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement