租用游艇
题目描述
长江游艇俱乐部在长江上设置了 $n$ 个游艇出租站 $1,2,\cdots,n$。游客可在这些游艇出租站租用游艇,并在下游的任何一个游艇出租站归还游艇。游艇出租站 $i$ 到游艇出租站 $j$ 之间的租金为 $r(i,j)$($1\le i\lt j\le n$)。试设计一个算法,计算出从游艇出租站 $1$ 到游艇出租站 $n$ 所需的最少租金。
输入格式
第一行中有一个正整数 $n$,表示有 $n$ 个游艇出租站。接下来的 $n-1$ 行是一个半矩阵 $r(i,j)$($1\le i<j\le n$)。
输出格式
输出计算出的从游艇出租站 $1$ 到游艇出租站 $n$ 所需的最少租金。
样例 #1
样例输入 #1
3
5 15
7
样例输出 #1
12
提示
$n\le 200$,保证计算过程中任何时刻数值都不超过 $10^6$。
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;
public class Main {
/**************** 以下为快读 ****************/
static BufferedReader r = new BufferedReader(new InputStreamReader(System.in));
static StringTokenizer in;
// 在没有其他输入时返回 null
static String next() {
try {
while (in == null || !in.hasMoreTokens())
in = new StringTokenizer(r.readLine());
return in.nextToken();
} catch (Exception e) {
}
return null;
}
static int nextInt() {
return Integer.parseInt(next());
}
static double nextDouble() {
return Double.parseDouble(next());
}
static long nextLong() {
return Long.parseLong(next());
}
static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
static final int N = 210;
static int n;
static int[][] a = new int[N][N];
static int[] dp = new int[N];//存1到i的最短距离
public static void main(String[] args) throws Exception{
n = nextInt();//距离矩阵
Arrays.fill(dp, Integer.MAX_VALUE / 2);
for(int i = 1;i <= n;i ++) {
for(int j = i + 1;j <= n;j ++) {
a[i][j] = nextInt();
if(i == 1) dp[j] = a[1][j];
dp[j] = Math.min(dp[j], dp[i] + a[i][j]);
}
}
out.print(dp[n]);
out.flush();
out.close();
}
}