Advertisement
Guest User

Untitled

a guest
Nov 8th, 2015
683
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.79 KB | None | 0 0
  1. import java.io.BufferedWriter;
  2. import java.io.IOException;
  3. import java.io.InputStream;
  4. import java.io.OutputStream;
  5. import java.io.OutputStreamWriter;
  6. import java.io.PrintWriter;
  7. import java.io.Writer;
  8. import java.util.Arrays;
  9. import java.util.InputMismatchException;
  10.  
  11. class SamuAndShopping implements Runnable
  12. {
  13.     static int t, n;
  14.     static int shops[][], dp[][];
  15.     static InputReader in;
  16.     static OutputWriter out;
  17.    
  18.     public static void main(String[] args) throws InterruptedException
  19.     {
  20.         in = new InputReader(System.in);
  21.         out = new OutputWriter(System.out);
  22.        
  23.         //solve();
  24.        
  25.         t = in.nextInt();
  26.        
  27.         for (int i = 0; i < t; i++)
  28.         {
  29.             Thread t = new Thread(null, new SamuAndShopping(), "SamuAndShopping", 1 << 30);
  30.             t.start();
  31.             t.join();
  32.         }
  33.        
  34.         out.flush();
  35.        
  36.         in.close();
  37.         out.close();
  38.     }
  39.    
  40.     public SamuAndShopping()
  41.     {
  42.         n = in.nextInt();
  43.        
  44.         shops = new int[n][3];
  45.        
  46.         for (int j = 0; j < n; j++)
  47.         {
  48.             shops[j][0] = in.nextInt();
  49.             shops[j][1] = in.nextInt();
  50.             shops[j][2] = in.nextInt();
  51.         }
  52.        
  53.     }
  54.    
  55.     @Override
  56.     public void run()
  57.     {
  58.         if (n == 1)
  59.         {
  60.             out.println(Math.min(shops[0][0], Math.min(shops[0][1], shops[0][2])));
  61.            
  62.             return;
  63.         }
  64.        
  65.         dp = new int[n][3];
  66.        
  67.         for (int j = 0; j < n; j++)
  68.             Arrays.fill(dp[j], -1);
  69.        
  70.         long min = Math.min(
  71.                 findBest(1, 0) + shops[0][0],
  72.                 Math.min(findBest(1, 1) + shops[0][1], findBest(1, 2)
  73.                         + shops[0][2]));
  74.        
  75.         out.println(min);
  76.     }
  77.  
  78.     static int findBest(int currIndex, int previousTaken)
  79.     {
  80.         int plusOne, plusTwo;
  81.  
  82.         if (dp[currIndex][(previousTaken + 1) % 3] != -1
  83.                 && dp[currIndex][(previousTaken + 2) % 3] != -1)
  84.             return Math.min(dp[currIndex][(previousTaken + 1) % 3],
  85.                     dp[currIndex][(previousTaken + 2) % 3]);
  86.  
  87.         if (currIndex == n - 1)
  88.         {
  89.             plusOne = shops[currIndex][(previousTaken + 1) % 3];
  90.             plusTwo = shops[currIndex][(previousTaken + 2) % 3];
  91.  
  92.             dp[currIndex][(previousTaken + 1) % 3] = plusOne;
  93.             dp[currIndex][(previousTaken + 2) % 3] = plusTwo;
  94.            
  95.             return Math.min(plusOne, plusTwo);
  96.         }
  97.  
  98.         plusOne = shops[currIndex][(previousTaken + 1) % 3]
  99.                 + findBest(currIndex + 1, (previousTaken + 1) % 3);
  100.         plusTwo = shops[currIndex][(previousTaken + 2) % 3]
  101.                 + findBest(currIndex + 1, (previousTaken + 2) % 3);
  102.  
  103.         dp[currIndex][(previousTaken + 1) % 3] = plusOne;
  104.         dp[currIndex][(previousTaken + 2) % 3] = plusTwo;
  105.  
  106.         return Math.min(plusOne, plusTwo);
  107.     }
  108.  
  109.     static class InputReader
  110.     {
  111.         private InputStream stream;
  112.         private byte[] buf = new byte[1024];
  113.         private int curChar;
  114.         private int numChars;
  115.  
  116.         public InputReader(InputStream stream)
  117.         {
  118.             this.stream = stream;
  119.         }
  120.  
  121.         public int read()
  122.         {
  123.             if (numChars == -1)
  124.                 throw new InputMismatchException();
  125.  
  126.             if (curChar >= numChars)
  127.             {
  128.                 curChar = 0;
  129.                 try
  130.                 {
  131.                     numChars = stream.read(buf);
  132.                 }
  133.                 catch (IOException e)
  134.                 {
  135.                     throw new InputMismatchException();
  136.                 }
  137.                 if (numChars <= 0)
  138.                     return -1;
  139.             }
  140.  
  141.             return buf[curChar++];
  142.         }
  143.  
  144.         public int nextInt()
  145.         {
  146.             int c = read();
  147.  
  148.             while (isSpaceChar(c))
  149.                 c = read();
  150.  
  151.             int sgn = 1;
  152.  
  153.             if (c == '-')
  154.             {
  155.                 sgn = -1;
  156.                 c = read();
  157.             }
  158.  
  159.             int res = 0;
  160.  
  161.             do
  162.             {
  163.                 if (c < '0' || c > '9')
  164.                     throw new InputMismatchException();
  165.  
  166.                 res *= 10;
  167.                 res += c & 15;
  168.  
  169.                 c = read();
  170.             } while (!isSpaceChar(c));
  171.  
  172.             return res * sgn;
  173.         }
  174.        
  175.         public int[] nextIntArray(int arraySize)
  176.         {
  177.             int array[] = new int[arraySize];
  178.            
  179.             for (int i = 0; i < arraySize; i++)
  180.                 array[i] = nextInt();
  181.            
  182.             return array;
  183.         }
  184.  
  185.         public long nextLong()
  186.         {
  187.             int c = read();
  188.  
  189.             while (isSpaceChar(c))
  190.                 c = read();
  191.  
  192.             int sign = 1;
  193.  
  194.             if (c == '-')
  195.             {
  196.                 sign = -1;
  197.  
  198.                 c = read();
  199.             }
  200.  
  201.             long result = 0;
  202.  
  203.             do
  204.             {
  205.                 if (c < '0' || c > '9')
  206.                     throw new InputMismatchException();
  207.  
  208.                 result *= 10;
  209.                 result += c & 15;
  210.  
  211.                 c = read();
  212.             } while (!isSpaceChar(c));
  213.  
  214.             return result * sign;
  215.         }
  216.        
  217.         public long[] nextLongArray(int arraySize)
  218.         {
  219.             long array[] = new long[arraySize];
  220.            
  221.             for (int i = 0; i < arraySize; i++)
  222.                 array[i] = nextLong();
  223.            
  224.             return array;
  225.         }
  226.  
  227.         public float nextFloat() // problematic
  228.         {
  229.             float result, div;
  230.             byte c;
  231.  
  232.             result = 0;
  233.             div = 1;
  234.             c = (byte) read();
  235.  
  236.             while (c <= ' ')
  237.                 c = (byte) read();
  238.  
  239.             boolean isNegative = (c == '-');
  240.  
  241.             if (isNegative)
  242.                 c = (byte) read();
  243.  
  244.             do
  245.             {
  246.                 result = result * 10 + c - '0';
  247.             } while ((c = (byte) read()) >= '0' && c <= '9');
  248.  
  249.             if (c == '.')
  250.                 while ((c = (byte) read()) >= '0' && c <= '9')
  251.                     result += (c - '0') / (div *= 10);
  252.  
  253.             if (isNegative)
  254.                 return -result;
  255.  
  256.             return result;
  257.         }
  258.  
  259.         public double nextDouble() // not completely accurate
  260.         {
  261.             double ret = 0, div = 1;
  262.             byte c = (byte) read();
  263.  
  264.             while (c <= ' ')
  265.                 c = (byte) read();
  266.  
  267.             boolean neg = (c == '-');
  268.  
  269.             if (neg)
  270.                 c = (byte) read();
  271.  
  272.             do
  273.             {
  274.                 ret = ret * 10 + c - '0';
  275.             } while ((c = (byte) read()) >= '0' && c <= '9');
  276.  
  277.             if (c == '.')
  278.                 while ((c = (byte) read()) >= '0' && c <= '9')
  279.                     ret += (c - '0') / (div *= 10);
  280.  
  281.             if (neg)
  282.                 return -ret;
  283.  
  284.             return ret;
  285.         }
  286.  
  287.         public String next()
  288.         {
  289.             int c = read();
  290.  
  291.             while (isSpaceChar(c))
  292.                 c = read();
  293.  
  294.             StringBuilder res = new StringBuilder();
  295.  
  296.             do
  297.             {
  298.                 res.appendCodePoint(c);
  299.  
  300.                 c = read();
  301.             } while (!isSpaceChar(c));
  302.  
  303.             return res.toString();
  304.         }
  305.  
  306.         public boolean isSpaceChar(int c)
  307.         {
  308.             return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
  309.         }
  310.        
  311.         public String nextLine()
  312.         {
  313.             int c = read();
  314.            
  315.             StringBuilder result = new StringBuilder();
  316.            
  317.             do
  318.             {
  319.                 result.appendCodePoint(c);
  320.                
  321.                 c = read();
  322.             } while (!isNewLine(c));
  323.            
  324.             return result.toString();
  325.         }
  326.        
  327.         public boolean isNewLine(int c)
  328.         {
  329.             return c == '\n';
  330.         }
  331.  
  332.         public void close()
  333.         {
  334.             try
  335.             {
  336.                 stream.close();
  337.             }
  338.             catch (IOException e)
  339.             {
  340.                 e.printStackTrace();
  341.             }
  342.         }
  343.  
  344.     }
  345.  
  346.     static class OutputWriter
  347.     {
  348.         private PrintWriter writer;
  349.  
  350.         public OutputWriter(OutputStream stream)
  351.         {
  352.             writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(
  353.                     stream)));
  354.         }
  355.  
  356.         public OutputWriter(Writer writer)
  357.         {
  358.             this.writer = new PrintWriter(writer);
  359.         }
  360.  
  361.         public void println(int x)
  362.         {
  363.             writer.println(x);
  364.         }
  365.  
  366.         public void print(int x)
  367.         {
  368.             writer.print(x);
  369.         }
  370.  
  371.         public void println(int array[], int size)
  372.         {
  373.             for (int i = 0; i < size; i++)
  374.                 println(array[i]);
  375.         }
  376.  
  377.         public void print(int array[], int size)
  378.         {
  379.             for (int i = 0; i < size; i++)
  380.                 print(array[i] + " ");
  381.         }
  382.  
  383.         public void println(long x)
  384.         {
  385.             writer.println(x);
  386.         }
  387.  
  388.         public void print(long x)
  389.         {
  390.             writer.print(x);
  391.         }
  392.  
  393.         public void println(long array[], int size)
  394.         {
  395.             for (int i = 0; i < size; i++)
  396.                 println(array[i]);
  397.         }
  398.  
  399.         public void print(long array[], int size)
  400.         {
  401.             for (int i = 0; i < size; i++)
  402.                 print(array[i]);
  403.         }
  404.  
  405.         public void println(float num)
  406.         {
  407.             writer.println(num);
  408.         }
  409.  
  410.         public void print(float num)
  411.         {
  412.             writer.print(num);
  413.         }
  414.  
  415.         public void println(double num)
  416.         {
  417.             writer.println(num);
  418.         }
  419.  
  420.         public void print(double num)
  421.         {
  422.             writer.print(num);
  423.         }
  424.  
  425.         public void println(String s)
  426.         {
  427.             writer.println(s);
  428.         }
  429.  
  430.         public void print(String s)
  431.         {
  432.             writer.print(s);
  433.         }
  434.  
  435.         public void println()
  436.         {
  437.             writer.println();
  438.         }
  439.  
  440.         public void printSpace()
  441.         {
  442.             writer.print(" ");
  443.         }
  444.  
  445.         public void flush()
  446.         {
  447.             writer.flush();
  448.         }
  449.  
  450.         public void close()
  451.         {
  452.             writer.close();
  453.         }
  454.  
  455.     }
  456.  
  457.  
  458. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement