File tree Expand file tree Collapse file tree 1 file changed +78
-0
lines changed
Expand file tree Collapse file tree 1 file changed +78
-0
lines changed Original file line number Diff line number Diff line change 1+ ``` java
2+
3+ import java.util.* ;
4+ import java.io.* ;
5+
6+ class Main {
7+
8+ // IO field
9+ static BufferedReader br = new BufferedReader (new InputStreamReader (System . in));
10+ static BufferedWriter bw = new BufferedWriter (new OutputStreamWriter (System . out));
11+ static StringTokenizer st;
12+
13+ static void nextLine () throws Exception {st = new StringTokenizer (br .readLine ());}
14+ static int nextInt() {return Integer . parseInt(st. nextToken());}
15+ static long nextLong() {return Long . parseLong(st. nextToken());}
16+ static void bwEnd() throws Exception {bw. flush();bw. close();}
17+
18+ // Additional field
19+ static int [] dp;
20+ static int [][] E , P ;
21+ static int C , T , L ;
22+
23+ public static void main(String [] args) throws Exception {
24+
25+ ready();
26+ // solve();
27+
28+ bwEnd();
29+ }
30+
31+ static void ready() throws Exception {
32+
33+ int Z = Integer . parseInt(br. readLine());
34+ while (Z -- > 0 ) {
35+ nextLine();
36+ C = nextInt();
37+ T = nextInt();
38+ L = nextInt();
39+ dp = new int [T ];
40+ E = new int [C ][T ];
41+ P = new int [C ][T ];
42+ for (int i= 0 ;i< C ;i++ ) {
43+ for (int j= 0 ;j< T ;j++ ) {
44+ nextLine();
45+ P [i][j] = nextInt();
46+ E [i][j] = nextInt();
47+ }
48+ }
49+
50+ solve();
51+ }
52+
53+ }
54+
55+ static void solve() throws Exception {
56+
57+ for (int j= 0 ;j< T ;j++ ) dp[j] = P [0 ][j] + E [0 ][j];
58+
59+ for (int i= 1 ;i< C ;i++ ) {
60+ int [] ndp = new int [T ];
61+ Arrays . fill(ndp, Integer . MAX_VALUE );
62+ for (int j= 0 ;j< T ;j++ ) {
63+ for (int k= 0 ;k< T ;k++ ) {
64+ ndp[j] = Math . min(ndp[j], dp[k] + E [i][j] + Math . abs(P [i][j] - P [i- 1 ][k]));
65+ }
66+ }
67+ dp = ndp;
68+ }
69+
70+ int ans = Integer . MAX_VALUE ;
71+ for (int j= 0 ;j< T ;j++ ) ans = Math . min(ans, dp[j] + Math . abs(L - P [C - 1 ][j]));
72+ bw. write(ans+ " \n " );
73+
74+ }
75+
76+ }
77+
78+ ```
You can’t perform that action at this time.
0 commit comments