Advertisement
Guest User

Samu And Shopping

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