1
- //pakage joney_000[let_me_start]
2
- //
3
1
import java .util .*;
4
2
import java .lang .*;
5
3
import java .io .*;
6
4
import java .math .*;
7
- /*
8
- * Author : joney_000[let_me_start]
9
- * Algorithm : Dijkstra O(V + E log V)
10
- * Platform : HackerRank
11
- *
5
+
6
+ /**
7
+ * Author : joney_000 [ developer.jaswant@gmail.com ]
8
+ * Algorithm : Dijkstra, Time: O((V + E) * log V), Space: O(N)
9
+ * Platform : Codeforces
10
+ * Ref : https://codeforces.com/contest/20/problem/C
12
11
*/
13
12
14
- class Edge implements Comparable <Edge >{
15
- public int v = -1 ;
16
- public long w = Long .MAX_VALUE / 100 ;
17
- public Edge (int v , long w ){
18
- this .v = v ;
19
- this .w = w ;
20
- }
21
- public Edge (Edge e ){
22
- this .v = e .v ;
23
- this .w = e .w ;
24
- }
13
+ class Solution {
25
14
26
- @ Override
27
- public int compareTo (Edge e ){
28
- if (this .w < e .w )return -1 ;
29
- else if (this .w > e .w )return 1 ;
30
- else return 0 ;
31
- }
32
- @ Override
33
- public String toString (){
34
- return "v = " +this .v +" , w = " +this .w ;
35
- }
36
- }
37
- class A
38
- {
39
15
private InputStream inputStream ;
40
16
private OutputStream outputStream ;
41
17
private FastReader in ;
42
18
private PrintWriter out ;
43
- /*
44
- Overhead [Additional Temporary Strorage] but provides memory reusibility for multiple test cases.
45
- Size Limit : 10^5 + 4
46
- */
47
- private final int BUFFER = 50000 ;
48
- private int tempints [] = new int [BUFFER ];
49
- private long templongs [] = new long [BUFFER ];
50
- private double tempdoubles [] = new double [BUFFER ];
51
- private char tempchars [] = new char [BUFFER ];
52
- //private final long mod = 1000000000+7;
53
- private final int INF = Integer .MAX_VALUE / 10 ;
19
+
20
+ private final int BUFFER = 100005 ;
21
+
22
+ private final long mod = 1000000000 +7 ;
23
+ private final int INF = Integer .MAX_VALUE ;
54
24
private final long INF_L = Long .MAX_VALUE / 10 ;
55
25
56
- public A (){}
57
- public A (boolean stdIO )throws FileNotFoundException {
58
- stdIO = false ;
26
+ public Solution (){}
27
+ public Solution (boolean stdIO )throws FileNotFoundException {
28
+ // stdIO = false;
59
29
if (stdIO ){
60
30
inputStream = System .in ;
61
31
outputStream = System .out ;
62
32
}else {
63
33
inputStream = new FileInputStream ("input.txt" );
64
- outputStream = new FileOutputStream ("output1 .txt" );
34
+ outputStream = new FileOutputStream ("output .txt" );
65
35
}
66
36
in = new FastReader (inputStream );
67
37
out = new PrintWriter (outputStream );
68
-
69
38
}
70
- final int MAX_N = 100005 ;
71
- final int MAX_M = 100005 ;
72
- int n = 0 ; int m = 0 ;
73
- boolean vis [] = new boolean [MAX_N ]; // The Frontier
74
- long d [] = new long [MAX_N ]; // distance vector
75
-
76
- LinkedList <Edge > adj [] = new LinkedList [MAX_N ];
77
39
78
- void run ()throws Exception {
79
- long st = System .currentTimeMillis ();
80
- int tests = i ();
81
- for (int t = 1 ; t <= tests ; t ++){
82
- n = i (); m = i ();
83
- clear ();
84
- for (int i = 1 ; i <= m ; i ++){
85
- int u = i (); int v = i (); long w = l ();
86
- adj [u ].add (new Edge (v , w ));
87
- adj [v ].add (new Edge (u , w )); // if graph is undirected
88
- }
89
-
90
- LinkedList <Edge > adj1 [] = getCopy (adj , n );
91
- int source = i ();
92
- djkstra (source , adj1 , n );
93
- for (int i = 1 ; i <= n ; i ++){
94
- if (i == source )continue ;
95
- if (d [i ]==INF_L )d [i ] = -1 ;
96
- out .write ("" +d [i ]+" " );
97
- }
98
- out .write ("\n " );
99
- //out.write(""+ans+"\n");
40
+ class Edge {
41
+ int from , to , weight ;
42
+ public Edge (int from , int to , int weight ){
43
+ this .from = from ;
44
+ this .to = to ;
45
+ this .weight = weight ;
100
46
}
101
- out .flush ();
102
- long end = System .currentTimeMillis ();
103
- // out.write("\n\nTime: "+(end - st)/1000.0);
104
- }// end run
105
-
106
- void clear (){
107
- for (int i = 1 ; i <= n ; i ++){
108
- vis [i ] = false ;
109
- adj [i ] = new LinkedList <Edge >();
110
- d [i ] = INF_L ;
47
+ @ Override
48
+ public String toString (){
49
+ return "from : " + from + " ,to: " + to + " weight: " + weight ;
50
+ }
51
+ }
52
+
53
+ class Vertex {
54
+ int nodeId ;
55
+ long cost ; // can overflow int
56
+ public Vertex (int nodeId , long cost ){
57
+ this .nodeId = nodeId ;
58
+ this .cost = cost ;
111
59
}
112
60
}
113
61
114
- LinkedList <Edge >[] getCopy ( LinkedList < Edge > adj [] , int n ){
115
- LinkedList <Edge > adj_copy [] = new LinkedList [n + 1 ];
62
+ private LinkedList <Vertex >[] getGraphFromEdges ( Edge [] edges , int n ){
63
+ LinkedList <Vertex >[] graph = new LinkedList [n + 1 ];
116
64
for (int i = 1 ; i <= n ; i ++){
117
- adj_copy [i ] = new LinkedList <Edge >();
118
- for ( Edge j : adj [ i ]){
119
- adj_copy [ i ]. add ( new Edge ( j ));
120
- }
65
+ graph [i ] = new LinkedList <Vertex >();
66
+ }
67
+ for ( Edge edge : edges ){
68
+ graph [ edge . from ]. add ( new Vertex ( edge . to , edge . weight ));
121
69
}
122
- return adj_copy ;
123
- }
124
-
125
- void djkstra (int source , LinkedList <Edge >adj [], int n )throws IOException {
126
- d [source ] = 0 ;
127
- PriorityQueue <Edge > pq = new PriorityQueue <Edge >();
128
- pq .add (new Edge (source , 0L ));
129
- while (!pq .isEmpty ()){
130
- Edge e = (Edge )pq .poll ();
131
- int u = e .v ;
132
- if (vis [u ])continue ;
133
- for (Edge x : adj [u ]){
134
- int v = x .v ;
135
- if (vis [v ])continue ;
136
- if (d [v ] > d [u ] + x .w ){
137
- d [v ] = d [u ] + x .w ;
138
- pq .add (new Edge (v , d [v ]));
70
+ return graph ;
71
+ }
72
+
73
+ // basically running Dijkstra from root 1
74
+ // Space: O(N)
75
+ // Time: O((V + E) log N)
76
+ private LinkedList <Integer > getShortestPath (Edge [] edges , int rootNode , int n ){
77
+ boolean fronteer [] = new boolean [n + 1 ];// assuming base 1 index
78
+ int path [] = new int [n + 1 ];
79
+ Arrays .fill (path , -1 );
80
+ long distance [] = new long [n + 1 ];
81
+ Arrays .fill (distance , Long .MAX_VALUE /10 );
82
+ PriorityQueue <Vertex > pQueue = new PriorityQueue <Vertex >(n , (a , b ) -> Long .compare (a .cost ,b .cost ));
83
+ fronteer [rootNode ] = true ;
84
+ distance [rootNode ] = 0 ;
85
+ LinkedList <Vertex >[] graph = getGraphFromEdges (edges , n );
86
+ pQueue .add (new Vertex (1 , 0 ));
87
+ while (!pQueue .isEmpty ()){
88
+ Vertex u = (Vertex )pQueue .poll ();
89
+ for (Vertex v : graph [u .nodeId ]){
90
+ if (!fronteer [v .nodeId ] && distance [u .nodeId ] + v .cost < distance [v .nodeId ]){
91
+ distance [v .nodeId ] = distance [u .nodeId ] + v .cost ;
92
+ path [v .nodeId ] = u .nodeId ;
93
+ pQueue .add (new Vertex (v .nodeId , distance [v .nodeId ]));
139
94
}
140
95
}
141
- vis [ u ] = true ; // add u to frontier
96
+ fronteer [ u . nodeId ] = true ;// add it to frounter
142
97
}
98
+ LinkedList <Integer > shortestPath = new LinkedList <>();
99
+ shortestPath .add (n );
100
+ int idx = n ;
101
+ while (path [idx ] != -1 ){
102
+ shortestPath .addFirst (path [idx ]);
103
+ idx = path [idx ];
104
+ }
105
+ return shortestPath ;
106
+ }
107
+
108
+ void run ()throws Exception {
109
+ // int tests = i();
110
+ // for(int t = 1; t <= tests; t++){
111
+ int n = i (); int m = i ();
112
+ Edge edges [] = new Edge [2 * m ];
113
+ for (int i = 0 ; i < m ; i ++){
114
+ int from = i (); int to = i (); int weight = i ();
115
+ edges [i ] = new Edge (from , to , weight );
116
+ edges [m + i ] = new Edge (to , from , weight );
117
+ }
118
+ LinkedList <Integer > path = getShortestPath (edges , 1 , n );
119
+ if (path .size () == 0 || path .getFirst () != 1 ){
120
+ out .write ("-1" );
121
+ return ;
122
+ }
123
+ for (int x : path ){
124
+ out .write ("" + x +" " );
125
+ }
126
+ out .write ("\n " );
127
+ // }
143
128
}
144
129
145
- //****************************** Gerenal Utilities ***********************//
130
+ void clear (){
146
131
147
- void print_r (Object ... o ){
148
- out .write ("\n " +Arrays .deepToString (o )+"\n " );
149
- out .flush ();
150
132
}
151
133
152
- boolean isPrime (long n ){
153
- if (n ==1 )return false ;
154
- if (n <=3 )return true ;
155
- if (n %2 ==0 )return false ;
156
- for (int i =2 ;i <= Math .sqrt (n ); i ++){
157
- if (n %i ==0 )return false ;
158
- }
159
- return true ;
160
- }
161
- // sieve
162
- int [] primes (int n ){ // for(int i=1;i<=arr.length-1;i++)out.write(""+arr[i]+" ");
163
- boolean arr [] = new boolean [n +1 ];
164
- Arrays .fill (arr ,true );
165
- arr [1 ]=false ;
166
- for (int i =2 ;i <=Math .sqrt (n );i ++){
167
- if (!arr [i ])continue ;
168
- for (int j = 2 *i ;j <=n ;j +=i ){
169
- arr [j ]=false ;
170
- }
134
+ long gcd (long a , long b ){
135
+ if (b == 0 )return a ;
136
+ return gcd (b , a % b );
171
137
}
172
- LinkedList <Integer > ll = new LinkedList <Integer >();
173
- for (int i =1 ;i <=n ;i ++){
174
- if (arr [i ])ll .add (i );
138
+
139
+ long lcm (long a , long b ){
140
+ if (a == 0 || b == 0 )return 0 ;
141
+ return (a * b )/gcd (a , b );
175
142
}
176
- n = ll .size ();
177
-
178
- int primes [] = new int [n +1 ];
179
- for (int i =1 ;i <=n ;i ++){
180
- primes [i ]=ll .removeFirst ();
143
+
144
+ long mulMod (long a , long b , long mod ){
145
+ if (a == 0 || b == 0 )return 0 ;
146
+ if (b == 1 )return a ;
147
+ long ans = mulMod (a , b /2 , mod );
148
+ ans = (ans * 2 ) % mod ;
149
+ if (b % 2 == 1 )ans = (a + ans )% mod ;
150
+ return ans ;
181
151
}
182
- return primes ;
183
- }
184
- long gcd (long a , long b ){
185
- if (b ==0 )return a ;
186
- return gcd (b , a %b );
187
- }
188
- long lcm (long a , long b ){
189
- if (a ==0 ||b ==0 )return 0 ;
190
- return (a *b )/gcd (a ,b );
191
- }
192
- long mulmod (long a , long b ,long mod ){
193
- if (a ==0 ||b ==0 )return 0 ;
194
- if (b ==1 )return a ;
195
- long ans = mulmod (a ,b /2 ,mod );
196
- ans = (ans *2 )% mod ;
197
- if (b %2 ==1 )ans = (a + ans )% mod ;
198
- return ans ;
199
- }
200
- long pow (long a , long b ,long mod ){
201
- if (b ==0 )return 1 ;
202
- if (b ==1 )return a ;
203
- long ans = pow (a ,b /2 ,mod );
204
- ans = (ans * ans );
205
- if (ans >= mod )ans %= mod ;
206
152
207
- if (b %2 ==1 )ans = (a * ans );
208
- if (ans >= mod )ans %= mod ;
153
+ long pow (long a , long b , long mod ){
154
+ if (b == 0 )return 1 ;
155
+ if (b == 1 )return a ;
156
+ long ans = pow (a , b /2 , mod );
157
+ ans = mulMod (ans , ans , mod );
158
+ if (ans >= mod )ans %= mod ;
159
+
160
+ if (b % 2 == 1 )ans = mulMod (a , ans , mod );
161
+ if (ans >= mod )ans %= mod ;
162
+
163
+ return ans ;
164
+ }
209
165
210
- return ans ;
211
- }
212
166
// 20*20 nCr Pascal Table
213
- long [][] ncrTable (){
214
- long ncr [][] = new long [21 ][21 ];
215
- for (int i =0 ;i <=20 ;i ++){ncr [i ][0 ]=1 ;ncr [i ][i ]=1 ;}
216
- for (int j =0 ;j <=20 ;j ++){
217
- for (int i =j +1 ;i <= 20 ;i ++){
218
- ncr [i ][j ] = ncr [i -1 ][j ]+ncr [i -1 ][j -1 ];
167
+ long [][] ncrTable (){
168
+ long ncr [][] = new long [21 ][21 ];
169
+
170
+ for (int i = 0 ; i <= 20 ; i ++){
171
+ ncr [i ][0 ] = ncr [i ][i ] = 1L ;
172
+ }
173
+
174
+ for (int j = 0 ; j <= 20 ; j ++){
175
+ for (int i = j + 1 ; i <= 20 ; i ++){
176
+ ncr [i ][j ] = ncr [i -1 ][j ] + ncr [i -1 ][j -1 ];
219
177
}
220
178
}
179
+
221
180
return ncr ;
222
181
}
223
- //*******************************I/O******************************//
182
+
224
183
int i ()throws Exception {
225
- //return Integer.parseInt(br.readLine().trim());
226
184
return in .nextInt ();
227
185
}
228
- int [] is (int n )throws Exception {
229
- //int arr[] = new int[n+1];
230
- for (int i =1 ; i <= n ;i ++)tempints [i ] = in .nextInt ();
231
- return tempints ;
232
- }
186
+
233
187
long l ()throws Exception {
234
188
return in .nextLong ();
235
189
}
236
- long [] ls (int n )throws Exception {
237
- for (int i =1 ; i <= n ;i ++)templongs [i ] = in .nextLong ();
238
- return templongs ;
239
- }
240
190
241
191
double d ()throws Exception {
242
192
return in .nextDouble ();
243
193
}
244
- double [] ds (int n )throws Exception {
245
- for (int i =1 ; i <= n ;i ++)tempdoubles [i ] = in .nextDouble ();
246
- return tempdoubles ;
247
- }
194
+
248
195
char c ()throws Exception {
249
196
return in .nextCharacter ();
250
197
}
251
- char [] cs (int n )throws Exception {
252
- for (int i =1 ; i <= n ;i ++)tempchars [i ] = in .nextCharacter ();
253
- return tempchars ;
254
- }
198
+
255
199
String s ()throws Exception {
256
200
return in .nextLine ();
257
201
}
202
+
258
203
BigInteger bi ()throws Exception {
259
204
return in .nextBigInteger ();
260
205
}
261
- //***********************I/O ENDS ***********************//
262
- //*********************** 0.3%f [precision]***********************//
263
- /* roundoff upto 2 digits
264
- double roundOff = Math.round(a * 100.0) / 100.0;
265
- or
266
- System.out.printf("%.2f", val);
267
-
268
- */
269
- /*
270
- print upto 2 digits after decimal
271
- val = ((long)(val * 100.0))/100.0;
272
206
273
- */
274
-
275
- private void closeResources (){
276
- out .flush ();
277
- out .close ();
278
- return ;
279
- }
280
- public static void main (String [] args ) throws java .lang .Exception {
281
- //let_me_start Shinch Returns
282
-
207
+ private void closeResources (){
208
+ out .flush ();
209
+ out .close ();
210
+ return ;
211
+ }
283
212
284
- /*
285
- // Old Reader Writer
286
- BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
287
- BufferedWriter out=new BufferedWriter(new OutputStreamWriter(System.out));
288
- BufferedReader br=new BufferedReader(new FileReader("input.txt"));
289
- BufferedWriter out=new BufferedWriter(new FileWriter("output.txt"));
290
- */
291
- A driver = new A (true );
292
-
293
- driver .run ();
213
+ // IMP: roundoff upto 2 digits
214
+ // double roundOff = Math.round(a * 100.0) / 100.0;
215
+ // or
216
+ // System.out.printf("%.2f", val);
294
217
295
- driver . closeResources ();
296
- return ;
218
+ // print upto 2 digits after decimal
219
+ // val = ((long)(val * 100.0))/100.0;
297
220
298
- }
221
+ public static void main (String [] args ) throws java .lang .Exception {
222
+
223
+ Solution driver = new Solution (true );
224
+ driver .run ();
225
+ driver .closeResources ();
226
+ }
227
+ }
299
228
300
- }
229
+ class FastReader {
301
230
302
- class FastReader {
231
+ private boolean finished = false ;
303
232
304
- private boolean finished = false ;
233
+ private InputStream stream ;
234
+ private byte [] buf = new byte [4 * 1024 ];
235
+ private int curChar ;
236
+ private int numChars ;
237
+ private SpaceCharFilter filter ;
305
238
306
- private InputStream stream ;
307
- private byte [] buf = new byte [4 * 1024 ];
308
- private int curChar ;
309
- private int numChars ;
310
- private SpaceCharFilter filter ;
239
+ public FastReader (InputStream stream ){
240
+ this .stream = stream ;
241
+ }
311
242
312
- public FastReader (InputStream stream ){
313
- this .stream = stream ;
243
+ public int read (){
244
+ if (numChars == -1 ){
245
+ throw new InputMismatchException ();
246
+ }
247
+ if (curChar >= numChars ){
248
+ curChar = 0 ;
249
+ try {
250
+ numChars = stream .read (buf );
251
+ } catch (IOException e ){
252
+ throw new InputMismatchException ();
314
253
}
315
-
316
- public int read (){
317
- if (numChars == -1 ){
318
- throw new InputMismatchException ();
319
- }
320
- if (curChar >= numChars ){
321
- curChar = 0 ;
322
- try {
323
- numChars = stream .read (buf );
324
- } catch (IOException e ){
325
- throw new InputMismatchException ();
326
- }
327
- if (numChars <= 0 ){
328
- return -1 ;
329
- }
330
- }
331
- return buf [curChar ++];
254
+ if (numChars <= 0 ){
255
+ return -1 ;
332
256
}
257
+ }
258
+ return buf [curChar ++];
259
+ }
333
260
334
- public int peek (){
335
- if (numChars == -1 ){
336
- return -1 ;
337
- }
338
- if (curChar >= numChars ){
339
- curChar = 0 ;
340
- try {
341
- numChars = stream .read (buf );
342
- } catch (IOException e ){
343
- return -1 ;
344
- }
345
- if (numChars <= 0 ){
346
- return -1 ;
347
- }
348
- }
349
- return buf [curChar ];
261
+ public int peek (){
262
+ if (numChars == -1 ){
263
+ return -1 ;
264
+ }
265
+ if (curChar >= numChars ){
266
+ curChar = 0 ;
267
+ try {
268
+ numChars = stream .read (buf );
269
+ } catch (IOException e ){
270
+ return -1 ;
350
271
}
351
-
352
- public int nextInt (){
353
- int c = read ();
354
- while (isSpaceChar (c ))
355
- c = read ();
356
- int sgn = 1 ;
357
- if (c == '-' ){
358
- sgn = -1 ;
359
- c = read ();
360
- }
361
- int res = 0 ;
362
- do {
363
- if (c ==',' ){
364
- c = read ();
365
- }
366
- if (c < '0' || c > '9' ){
367
- throw new InputMismatchException ();
368
- }
369
- res *= 10 ;
370
- res += c - '0' ;
371
- c = read ();
372
- } while (!isSpaceChar (c ));
373
- return res * sgn ;
272
+ if (numChars <= 0 ){
273
+ return -1 ;
374
274
}
275
+ }
276
+ return buf [curChar ];
277
+ }
375
278
376
- public long nextLong (){
377
- int c = read ();
378
- while (isSpaceChar (c ))
379
- c = read ();
380
- int sgn = 1 ;
381
- if (c == '-' ){
382
- sgn = -1 ;
383
- c = read ();
384
- }
385
- long res = 0 ;
386
- do {
387
- if (c < '0' || c > '9' ){
388
- throw new InputMismatchException ();
389
- }
390
- res *= 10 ;
391
- res += c - '0' ;
392
- c = read ();
393
- } while (!isSpaceChar (c ));
394
- return res * sgn ;
279
+ public int nextInt (){
280
+ int c = read ();
281
+ while (isSpaceChar (c ))
282
+ c = read ();
283
+ int sgn = 1 ;
284
+ if (c == '-' ){
285
+ sgn = -1 ;
286
+ c = read ();
287
+ }
288
+ int res = 0 ;
289
+ do {
290
+ if (c ==',' ){
291
+ c = read ();
395
292
}
396
-
397
- public String nextString (){
398
- int c = read ();
399
- while (isSpaceChar (c ))
400
- c = read ();
401
- StringBuilder res = new StringBuilder ();
402
- do {
403
- res .appendCodePoint (c );
404
- c = read ();
405
- } while (!isSpaceChar (c ));
406
- return res .toString ();
293
+ if (c < '0' || c > '9' ){
294
+ throw new InputMismatchException ();
407
295
}
296
+ res *= 10 ;
297
+ res += c - '0' ;
298
+ c = read ();
299
+ } while (!isSpaceChar (c ));
300
+ return res * sgn ;
301
+ }
408
302
409
- public boolean isSpaceChar (int c ){
410
- if (filter != null ){
411
- return filter .isSpaceChar (c );
412
- }
413
- return isWhitespace (c );
303
+ public long nextLong (){
304
+ int c = read ();
305
+ while (isSpaceChar (c ))
306
+ c = read ();
307
+ int sgn = 1 ;
308
+ if (c == '-' ){
309
+ sgn = -1 ;
310
+ c = read ();
311
+ }
312
+ long res = 0 ;
313
+ do {
314
+ if (c < '0' || c > '9' ){
315
+ throw new InputMismatchException ();
414
316
}
317
+ res *= 10 ;
318
+ res += c - '0' ;
319
+ c = read ();
320
+ } while (!isSpaceChar (c ));
321
+ return res * sgn ;
322
+ }
415
323
416
- public static boolean isWhitespace (int c ){
417
- return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1 ;
418
- }
324
+ public String nextString (){
325
+ int c = read ();
326
+ while (isSpaceChar (c ))
327
+ c = read ();
328
+ StringBuilder res = new StringBuilder ();
329
+ do {
330
+ res .appendCodePoint (c );
331
+ c = read ();
332
+ } while (!isSpaceChar (c ));
333
+ return res .toString ();
334
+ }
419
335
420
- private String readLine0 (){
421
- StringBuilder buf = new StringBuilder ();
422
- int c = read ();
423
- while (c != '\n' && c != -1 ){
424
- if (c != '\r' ){
425
- buf .appendCodePoint (c );
426
- }
427
- c = read ();
428
- }
429
- return buf .toString ();
430
- }
336
+ public boolean isSpaceChar (int c ){
337
+ if (filter != null ){
338
+ return filter .isSpaceChar (c );
339
+ }
340
+ return isWhitespace (c );
341
+ }
431
342
432
- public String nextLine (){
433
- String s = readLine0 ();
434
- while (s .trim ().length () == 0 )
435
- s = readLine0 ();
436
- return s ;
437
- }
343
+ public static boolean isWhitespace (int c ){
344
+ return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1 ;
345
+ }
438
346
439
- public String nextLine ( boolean ignoreEmptyLines ){
440
- if ( ignoreEmptyLines ){
441
- return nextLine ();
442
- } else {
443
- return readLine0 ();
444
- }
347
+ private String readLine0 ( ){
348
+ StringBuilder buf = new StringBuilder ();
349
+ int c = read ();
350
+ while ( c != '\n' && c != - 1 ) {
351
+ if ( c != '\r' ){
352
+ buf . appendCodePoint ( c );
445
353
}
354
+ c = read ();
355
+ }
356
+ return buf .toString ();
357
+ }
446
358
447
- public BigInteger nextBigInteger (){
448
- try {
449
- return new BigInteger (nextString ());
450
- } catch (NumberFormatException e ){
451
- throw new InputMismatchException ();
452
- }
453
- }
359
+ public String nextLine (){
360
+ String s = readLine0 ();
361
+ while (s .trim ().length () == 0 )
362
+ s = readLine0 ();
363
+ return s ;
364
+ }
454
365
455
- public char nextCharacter (){
456
- int c = read ();
457
- while (isSpaceChar (c ))
458
- c = read ();
459
- return (char ) c ;
460
- }
366
+ public String nextLine (boolean ignoreEmptyLines ){
367
+ if (ignoreEmptyLines ){
368
+ return nextLine ();
369
+ }else {
370
+ return readLine0 ();
371
+ }
372
+ }
461
373
462
- public double nextDouble (){
463
- int c = read ();
464
- while (isSpaceChar (c ))
465
- c = read ();
466
- int sgn = 1 ;
467
- if (c == '-' ){
468
- sgn = -1 ;
469
- c = read ();
470
- }
471
- double res = 0 ;
472
- while (!isSpaceChar (c ) && c != '.' ){
473
- if (c == 'e' || c == 'E' ){
474
- return res * Math .pow (10 , nextInt ());
475
- }
476
- if (c < '0' || c > '9' ){
477
- throw new InputMismatchException ();
478
- }
479
- res *= 10 ;
480
- res += c - '0' ;
481
- c = read ();
374
+ public BigInteger nextBigInteger (){
375
+ try {
376
+ return new BigInteger (nextString ());
377
+ } catch (NumberFormatException e ){
378
+ throw new InputMismatchException ();
379
+ }
380
+ }
381
+
382
+ public char nextCharacter (){
383
+ int c = read ();
384
+ while (isSpaceChar (c ))
385
+ c = read ();
386
+ return (char ) c ;
387
+ }
388
+
389
+ public double nextDouble (){
390
+ int c = read ();
391
+ while (isSpaceChar (c ))
392
+ c = read ();
393
+ int sgn = 1 ;
394
+ if (c == '-' ){
395
+ sgn = -1 ;
396
+ c = read ();
397
+ }
398
+ double res = 0 ;
399
+ while (!isSpaceChar (c ) && c != '.' ){
400
+ if (c == 'e' || c == 'E' ){
401
+ return res * Math .pow (10 , nextInt ());
402
+ }
403
+ if (c < '0' || c > '9' ){
404
+ throw new InputMismatchException ();
405
+ }
406
+ res *= 10 ;
407
+ res += c - '0' ;
408
+ c = read ();
409
+ }
410
+ if (c == '.' ){
411
+ c = read ();
412
+ double m = 1 ;
413
+ while (!isSpaceChar (c )){
414
+ if (c == 'e' || c == 'E' ){
415
+ return res * Math .pow (10 , nextInt ());
482
416
}
483
- if (c == '.' ){
484
- c = read ();
485
- double m = 1 ;
486
- while (!isSpaceChar (c )){
487
- if (c == 'e' || c == 'E' ){
488
- return res * Math .pow (10 , nextInt ());
489
- }
490
- if (c < '0' || c > '9' ){
491
- throw new InputMismatchException ();
492
- }
493
- m /= 10 ;
494
- res += (c - '0' ) * m ;
495
- c = read ();
496
- }
417
+ if (c < '0' || c > '9' ){
418
+ throw new InputMismatchException ();
497
419
}
498
- return res * sgn ;
420
+ m /= 10 ;
421
+ res += (c - '0' ) * m ;
422
+ c = read ();
499
423
}
424
+ }
425
+ return res * sgn ;
426
+ }
500
427
501
- public boolean isExhausted (){
502
- int value ;
503
- while (isSpaceChar (value = peek ()) && value != -1 )
504
- read ();
505
- return value == -1 ;
506
- }
428
+ public boolean isExhausted (){
429
+ int value ;
430
+ while (isSpaceChar (value = peek ()) && value != -1 )
431
+ read ();
432
+ return value == -1 ;
433
+ }
507
434
508
- public String next (){
509
- return nextString ();
510
- }
435
+ public String next (){
436
+ return nextString ();
437
+ }
511
438
512
- public SpaceCharFilter getFilter (){
513
- return filter ;
514
- }
439
+ public SpaceCharFilter getFilter (){
440
+ return filter ;
441
+ }
515
442
516
- public void setFilter (SpaceCharFilter filter ){
517
- this .filter = filter ;
518
- }
443
+ public void setFilter (SpaceCharFilter filter ){
444
+ this .filter = filter ;
445
+ }
519
446
520
- public interface SpaceCharFilter {
521
- public boolean isSpaceChar (int ch );
522
- }
523
- }
524
- /******************** Pair class ***********************/
525
-
526
- class Pair implements Comparable <Pair >{
527
- public int a ;
528
- public int b ;
529
- public int c ;
530
- public Pair (){
531
- this .a = 0 ;
532
- this .b = 0 ;
533
- }
534
- public Pair (int a ,int b , int c ){
535
- this .a = a ;
536
- this .b = b ;
537
- this .c = c ;
538
- }
539
- public int compareTo (Pair p ){
540
- if (this .a ==p .a ){
541
- return this .b -p .b ;
542
- }
543
- return p .a -this .a ;
544
- }
545
- public String toString (){
546
- return "a=" +this .a +" b=" +this .b ;
447
+ public interface SpaceCharFilter {
448
+ public boolean isSpaceChar (int ch );
449
+ }
450
+ }
451
+
452
+ class Pair implements Comparable <Pair >{
453
+ public int a ;
454
+ public int b ;
455
+
456
+ public Pair (int a ,int b ){
457
+ this .a = a ;
458
+ this .b = b ;
459
+ }
460
+
461
+ @ Override
462
+ public int compareTo (Pair other ){
463
+ return Integer .compare (this .a , other .a );
547
464
}
548
465
549
- }
466
+ @ Override
467
+ public String toString (){
468
+ return "a = " + this .a + " b = " + this .b ;
469
+ }
470
+ }
0 commit comments