uva590-DP-Always on the run

Meaning of the questions:

There is a thief, in order to escape from the police, decided to take a flight every day to get rid of the police, a total of n cities, city i to j day flight prices are different (the price of 0 indicates that no flights).

A total of k need to take flight, and the last flight destination N, the minimum flight and asked how much it cost.

 

Problem-solving ideas:

Abandon all the conditions, as long as a thinking thing, the first day i reach the minimum cost of the j is the number of cities, the answer is dp [k] [n].

J calculated in city spending on day i, i-1 spend all day in other cities need to be calculated. Then dp [i] [j] = min (dp [i-1] [t]) {t! = J && city flights to the day t j} city

Note: the initialization condition dp [0] [1] = 0, the other is -1 indicates the absence of the program.

 

 

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class Main
{
     static final int N = 13;
     static final int K = 1003;
     static final int D = 32;
//    static final int N = 4;
//    static final int K = 7;
//    static final int D = 8;
    static int dp[][] = new int[K][N];
    static int num[][][] = new int[N][N][D];
    static int ds[][] = new int[N][N];
    static int n;
    static int k;
    static int cases;

    public static void main(String[] args) throws FileNotFoundException
    {
         Scanner scanner = new Scanner(System.in);
        //Scanner scanner = new Scanner(new File("/Users/caicai/in"));
        while (true)
        {
            n = scanner.nextInt();
            k = scanner.nextInt();
            if (n == 0 && k == 0) break;
            for (int i = 1; i <= n; i++)
            {
                for (int j = 1; j <= n; j++)
                {
                    if (i == j)
                    {
                        j++;
                    }
                    if (j > n)
                    {
                        break;
                    }
                    int d = scanner.nextInt();
                    ds[i][j] = d;
                    for (int di = 0; di < d; di++)
                    {
                        num[i][j][di] = scanner.nextInt();
                    }
                }
            }
            // init dp
            initdp();
            ++cases;
            dp();
        }
    }

    static void initdp()
    {
        for (int I = 0 ; I <K; I ++ ) 
        { 
            for ( int J = 0 ; J <N; J ++ ) 
            { 
                DP [I] [J] = - . 1 ; 
            } 
        } 

    } 

    static  void DP () 
    { 
        // the init 
        DP [ 0 ] [ . 1 ] = 0 ;
         // Dij on day i j city minimum cost 
        for ( int i = . 1 ; i <= K; i ++ ) 
        { 
            for (int e = 1; e <= n; e++)
            {
                int maxVal = 0x7fffffff;
                for (int s = 1; s <= n; s++)
                {
                    if (e == s)
                    {
                        // the same city
                        continue;
                    }
                    if (dp[i - 1][s] == -1)
                    {
                        // not start
                        continue;
                    }
                    // e is j
                    // s is t
                    int curd = (i - 1) % ds[s][e];
                    int cost = num[s][e][curd];
                    if (cost == 0) continue;
                    int val = dp[i - 1][s] + cost;
                    if (maxVal > val) maxVal = val;
                }
                if (maxVal != 0x7fffffff) dp[i][e] = maxVal;
            }
        }
        int val = dp[k][n];
        System.out.println("Scenario #" + cases);
        if (val == -1) System.out.println("No flight possible.");
        else
            System.out.println("The best flight costs " + val + ".");
        System.out.println();
    }

}

 

Guess you like

Origin www.cnblogs.com/shuiyonglewodezzzzz/p/11310912.html
run