Advertisement
Guest User

TCO 2015 2B MinimumCuts

a guest
Jun 20th, 2015
348
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.59 KB | None | 0 0
  1.  
  2. public class MinimumCuts
  3. {
  4.     int N;
  5.     Node[] nodes;
  6.     static final int MXVAL = 10000;
  7.     static final int[][] orders = {
  8.         {0, 1, 2},
  9.         {0, 2, 1},
  10.         {1, 0, 2},
  11.         {1, 2, 0},
  12.         {2, 0, 1},
  13.         {2, 1, 0}
  14.     };
  15.  
  16.     private class Node
  17.     {
  18.         Node left, right;
  19.         int removeCost;
  20.         int[] dp;
  21.  
  22.         void give(Node n)
  23.         {
  24.             if (left == null)
  25.                 left = n;
  26.             else if (right == null)
  27.                 right = n;
  28.             else
  29.                 throw new RuntimeException();
  30.         }
  31.  
  32.         int f(int x)
  33.         {
  34.             return Math.min(dp[x], removeCost + x);
  35.         }
  36.  
  37.         void solve()
  38.         {
  39.             if  (left != null)  left.solve();
  40.             if (right != null) right.solve();
  41.             dp = new int[MXVAL * 2 + 1];
  42.             for (int x = 0; x < dp.length; x++)
  43.             {
  44.                 if (left == null)
  45.                     dp[x] = (int) 1e8;
  46.                 else if (right == null)
  47.                 {
  48.                     int x1 = left.f(Math.min(removeCost, x));
  49.                     int x2 = Math.min(x, removeCost + left.f(0));
  50.                     dp[x] = Math.max(dp[x], x1);
  51.                     dp[x] = Math.max(dp[x], x2);
  52.                 }
  53.                 else
  54.                 {
  55.                     for (int[] or : orders)
  56.                     {
  57.                         int v = 0;
  58.                         for (int n : or)
  59.                         {
  60.                             if (n == 0)
  61.                                 v = left.f(v);
  62.                             else if (n == 1)
  63.                                 v = right.f(v);
  64.                             else
  65.                                 v = Math.min(x, removeCost + v);
  66.                         }
  67.                         dp[x] = Math.max(dp[x], v);
  68.                     }
  69.                 }
  70.             }
  71.         }
  72.     }
  73.  
  74.     public int costPaid(int[] parent, int[] cost)
  75.     {
  76.         this.N = cost.length + 1;
  77.         nodes = new Node[N];
  78.         for (int i = 0; i < N; i++)
  79.             nodes[i] = new Node();
  80.         for (int i = 0; i < N-1; i++)
  81.         {
  82.             nodes[i+1].removeCost = cost[i];
  83.             nodes[parent[i]].give(nodes[i+1]);
  84.         }
  85.         nodes[0].solve();
  86.         return nodes[0].dp[0];
  87.     }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement