参考代码:
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); int[] boom = new int[n + 1]; for(int i = 1; i <= n; i++) boom[i] = scanner.nextInt(); boolean[][] map = new boolean[n + 1][n + 1];//邻接矩阵存储 while(true) { int t1 = scanner.nextInt(); int t2 = scanner.nextInt(); if(t1 == 0 && t2 == 0) break; map[t1][t2] = true; } scanner.close(); int[] dp = new int[n + 1];//dp[i]为从第i个洞开始挖,可以挖到最多的地雷数 dp[n] = boom[n]; int[] index = new int[n + 1]; for(int i = n - 1; i > 0; i--) { for(int j = i + 1; j <= n; j++) { if(dp[j] > dp[i] && map[i][j]) { dp[i] = dp[j]; index[i] = j; } } dp[i] += boom[i]; } int res = 0; int start = 0; for(int i = 1; i <= n; i++) { if(dp[i] > res) { res = dp[i]; start = i; } } while(index[start] > 0) { System.out.print(start + "-"); start = index[start]; } System.out.println(start); System.out.print(res); } }
0.0分
5 人评分
P1000 (C语言代码)浏览:911 |
陈教主的三角形 (C语言代码)浏览:1201 |
C语言程序设计教程(第三版)课后习题5.7 (C语言代码)浏览:416 |
C语言程序设计教程(第三版)课后习题1.5 (C语言代码)浏览:497 |
母牛的故事 (C语言代码)浏览:496 |
求圆的面积 (C语言代码)浏览:714 |
C语言程序设计教程(第三版)课后习题1.5 (C语言代码)浏览:522 |
三进制小数 (C语言代码)浏览:903 |
C语言程序设计教程(第三版)课后习题5.7 (C语言代码)浏览:607 |
C语言程序设计教程(第三版)课后习题5.7 (C语言代码)浏览:622 |