uva10201-dp

Meaning of the questions:

Two rented a car from city A to city B, oil began 100L, 200L fuel tank can hold a total oil, with 1KM to 1L oil, gas station on the way, there are different distances from A, different prices per liter of oil.

He asked the city B 100L oil tank there, how much it cost the least.

DP [i] [j] represents the minimum cost at the site of j i oil. Then

dp [i] [j] = min (dp [i-1] [j + d [i]], dp [i] [j]), d [i] indicates the distance i-1 to i.

DP [i] [j] has two sources, one from dp [i-1] [j + d [i]], dp [i] [j] represents the refueling station i to j is spent.

java.io.ByteArrayInputStream Import; 
Import java.io.File; 
Import java.io.FileNotFoundException; 
Import java.util.Scanner; 

public  class the Main 
{ 
    static Scanner Scanner; 

    Final static  int G = 110 ;
     // Final static int G = . 8; 
    Final static  int T = 206 ;
     // Final static int. 5 = N;
     // Dij represents the gas station i j is the time it takes the minimum 
    static  int [] [] DP = new new  int [G] [T];
     static  int DIS;
    static int[] sd = new int[G];
    static int[] sp = new int[G];
    static int[] disSum = new int[G];
    static int gt;
    static int INF = Integer.MAX_VALUE;
    static int maxDis = -1;
    static int tt = 1;

    public static void main(String[] args) throws FileNotFoundException
    {
         scanner = new Scanner(System.in);
        //scanner = new Scanner(new File("/Users/caicai/in"));
        String str = scanner.nextLine();
        int val = Integer.valueOf(str);
        // empty line
        tt = 1;
        while (val-- != 0)
        {
            gt = 1;
            maxDis=-1;
            dis = scanner.nextInt();
            sd[0] = 0;
            sp[0] = 0;
            int preDis = 0;
            str = scanner.nextLine();
            while (scanner.hasNextLine())
            {
                // split
                str = scanner.nextLine();
                if (str.isEmpty())
                {
                    break;
                }
                ByteArrayInputStream si = new ByteArrayInputStream(str.getBytes());
                Scanner scanner2 = new Scanner(si);
                int diss = scanner2.nextInt();
                int price = scanner2.nextInt();
                scanner2.close();
                if (diss > dis) continue;
                sd[gt] = diss - preDis;
                preDis = diss;
                sp[gt] = price;
                ++gt;
                if (diss > maxDis) maxDis = diss;
            }
            // dp
            dp();

        }
    }

    static void init()
    {
        for (int i = 0; i < G; i++)
        {
            for (int j = 0; j < T; j++)
            {
                if (i == 0 || j == 0) dp[i][j] = INF - 1;
                else
                    dp[i][j] = INF;
            }
        }

    }

    static void dp()
    {
        init();
        if (sd[1] > 100)
        {
            if (tt != 1) System.out.println();
            System.out.println("Impossible");
            ++tt;
            return;
        }
        dp[1][100 - sd[1]] = 0;
        // 多余的gas
        int h = 100 - sd[1];
        for (int i = h; i >= 0; i--)
        {
            dp[1][i] = (h - i) * -1 * sp[1];
        }
        for (int i = 1; i < gt; i++)
        {
            for (int j = 0; j <= 200; j++)
            {
                // 当前的站点gas是j
                int diss = sd[i];
                if (diss + j > 200 || dp[i - 1][diss + j] == INF)
                {
                    continue;
                }
                dp[i][j] = Math.min(dp[i - 1][diss + j], dp[i][j]);
                for ( int k = J + . 1 ; k <= 200 is ; k ++ ) 
                { 
                    // this site was added to the gas money k 
                    int curPrice SP = [I] * (k - J) + DP [I] [J]; 
                    DP [I] [K] = Math.min (DP [I] [K], curPrice);
 //                     IF (DISS + K> 200 is) Continue;
 //                     int preCurPrice DP = [I -. 1] [+ K DISS ];
 //                     DP [I] [K] = Math.min (DP [I] [K], preCurPrice); 
                } 
            } 
        } 
        // Check Val 
        int Half = 100 ;
        // Check the last site 
        int lastDis = DIS - maxDis;
         int Gas lastDis + = Half;
         IF (Gas> 200 is || DP [gt - . 1 ] [Gas] == INF) 
        { 
            IF (! = TT . 1 ) the System. OUT .println (); 
            . the System OUT .println ( " Impossible " );
             ++ TT;
             return ; 
        } 
        the else 
        { 
            IF (! = TT . 1 ) the System. OUT.println();
            ++tt;
            System.out.println(dp[gt - 1][gas]);
        }
    }

}

 

Guess you like

Origin www.cnblogs.com/shuiyonglewodezzzzz/p/11359910.html