QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#97569#5613. Road To SavingsNicolas125841AC ✓153ms55972kbJava112.3kb2023-04-17 10:15:012023-04-17 10:15:03

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-04-17 10:15:03]
  • 评测
  • 测评结果:AC
  • 用时:153ms
  • 内存:55972kb
  • [2023-04-17 10:15:01]
  • 提交

answer

import java.io.*;
import java.util.*;

public class Rts {

	public static void main(String args[]) throws Exception {

		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
		StringTokenizer st = new StringTokenizer(br.readLine());
		
		int n = Integer.parseInt(st.nextToken()), m = Integer.parseInt(st.nextToken()), a = Integer.parseInt(st.nextToken()), b = Integer.parseInt(st.nextToken());

		a--;
		b--;
		
		int[][] w = new int[n][n];
		int[] lens = new int[n];
		HashSet<Edge>[] prevs = new HashSet[n];
		
		for(int i = 0; i < n; i++)
			prevs[i] = new HashSet<Edge>();
		
		for(int i = 0; i < n; i++)
			Arrays.fill(w[i], -1);
		
		int lsum = 0;
		
		for(int i = 0; i < m; i++) {
			st = new StringTokenizer(br.readLine());
			
			int u = Integer.parseInt(st.nextToken())-1;
			int v = Integer.parseInt(st.nextToken())-1;
			int l = Integer.parseInt(st.nextToken());
			
			lsum += l;
			
			w[u][v] = l;
			w[v][u] = l;
		}		
		
		PriorityQueue<int[]> pq = new PriorityQueue<int[]>(new Comparator<int[]>() {
			@Override
			public int compare(int[] a, int[] b) {
				// TODO Auto-generated method stub
				return Integer.compare(a[1], b[1]);
			}			
		});
		
		Arrays.fill(lens, 1000000000);
		
		lens[a] = 0;
		
		pq.add(new int[] {a, 0});
		
		while(!pq.isEmpty()) {
			int[] cur = pq.poll();
			
			if(lens[cur[0]] < cur[1])
				continue;
			
			for(int i = 0; i < n; i++) {
				if(w[cur[0]][i] == -1)
					continue;
				
				if(cur[1] + w[cur[0]][i] < lens[i]) {
					lens[i] = cur[1] + w[cur[0]][i];
					prevs[i].clear();
					
					prevs[i].addAll(prevs[cur[0]]);
					prevs[i].add(new Edge(cur[0], i));
					
					pq.add(new int[] {i, lens[i]});
				}else if(cur[1] + w[cur[0]][i] == lens[i]) {
					prevs[i].addAll(prevs[cur[0]]);
					prevs[i].add(new Edge(cur[0], i));
				}
			}
		}
				
		for(Edge e : prevs[b])
			lsum -= w[e.from][e.to];
		
		pw.println(lsum);
		pw.close();
	}
	
	static class Edge{
		int to, from;
		
		public Edge(int f, int t) {
			to = t;
			from = f;
		}

		@Override
		public int hashCode() {
			// TODO Auto-generated method stub
			return to * 100 + from;
		}				
	}
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 56ms
memory: 51628kb

input:

4 5 1 4
1 2 1
1 3 2
1 4 2
4 2 1
3 4 1

output:

3

result:

ok single line: '3'

Test #2:

score: 0
Accepted
time: 50ms
memory: 51872kb

input:

4 5 1 4
1 2 1
1 3 2
1 4 1
4 2 1
3 4 1

output:

5

result:

ok single line: '5'

Test #3:

score: 0
Accepted
time: 55ms
memory: 49864kb

input:

2 1 1 2
1 2 10

output:

0

result:

ok single line: '0'

Test #4:

score: 0
Accepted
time: 61ms
memory: 49764kb

input:

3 3 1 2
1 2 10
2 3 10
3 1 10

output:

20

result:

ok single line: '20'

Test #5:

score: 0
Accepted
time: 68ms
memory: 51980kb

input:

3 3 1 2
1 2 10
2 3 5
3 1 5

output:

0

result:

ok single line: '0'

Test #6:

score: 0
Accepted
time: 153ms
memory: 53868kb

input:

100 4950 74 24
1 2 8
1 3 6
1 4 5
1 5 6
1 6 2
1 7 6
1 8 1
1 9 10
1 10 1
1 11 10
1 12 9
1 13 4
1 14 4
1 15 1
1 16 4
1 17 7
1 18 8
1 19 1
1 20 7
1 21 2
1 22 9
1 23 9
1 24 7
1 25 6
1 26 5
1 27 4
1 28 3
1 29 4
1 30 6
1 31 8
1 32 10
1 33 10
1 34 3
1 35 6
1 36 10
1 37 5
1 38 4
1 39 1
1 40 10
1 41 9
1 42 7
...

output:

27615

result:

ok single line: '27615'

Test #7:

score: 0
Accepted
time: 71ms
memory: 52360kb

input:

40 780 6 12
1 2 100
1 3 100
1 4 100
1 5 100
1 6 100
1 7 100
1 8 100
1 9 100
1 10 100
1 11 100
1 12 100
1 13 100
1 14 100
1 15 100
1 16 100
1 17 100
1 18 100
1 19 100
1 20 1
1 21 100
1 22 100
1 23 100
1 24 100
1 25 100
1 26 1
1 27 100
1 28 100
1 29 100
1 30 100
1 31 100
1 32 100
1 33 100
1 34 100
1 3...

output:

74100

result:

ok single line: '74100'

Test #8:

score: 0
Accepted
time: 62ms
memory: 52056kb

input:

40 780 6 12
1 2 100
1 3 100
1 4 100
1 5 100
1 6 1
1 7 100
1 8 100
1 9 100
1 10 100
1 11 100
1 12 100
1 13 100
1 14 100
1 15 100
1 16 100
1 17 100
1 18 100
1 19 100
1 20 100
1 21 100
1 22 100
1 23 100
1 24 100
1 25 100
1 26 1
1 27 100
1 28 100
1 29 100
1 30 100
1 31 100
1 32 100
1 33 100
1 34 100
1 3...

output:

74000

result:

ok single line: '74000'

Test #9:

score: 0
Accepted
time: 57ms
memory: 52228kb

input:

40 780 6 12
1 2 100
1 3 100
1 4 100
1 5 100
1 6 100
1 7 100
1 8 100
1 9 100
1 10 100
1 11 100
1 12 100
1 13 100
1 14 100
1 15 100
1 16 100
1 17 100
1 18 100
1 19 100
1 20 100
1 21 100
1 22 100
1 23 100
1 24 100
1 25 100
1 26 100
1 27 100
1 28 100
1 29 100
1 30 100
1 31 100
1 32 100
1 33 100
1 34 100...

output:

77900

result:

ok single line: '77900'

Test #10:

score: 0
Accepted
time: 53ms
memory: 51768kb

input:

13 16 1 6
1 2 1
1 7 2
1 8 2
1 9 2
1 10 1
2 3 1
3 4 1
4 5 1
5 6 1
7 6 2
8 6 2
9 6 2
10 11 1
11 12 1
12 13 1
13 6 1

output:

10

result:

ok single line: '10'

Test #11:

score: 0
Accepted
time: 57ms
memory: 53664kb

input:

54 60 1 2
1 2 24
1 3 12
3 2 12
1 4 8
4 5 8
5 2 8
1 6 6
6 7 6
7 8 6
8 2 6
1 9 4
9 10 4
10 11 4
11 12 4
12 13 4
13 2 4
1 14 3
14 15 3
15 16 3
16 17 3
17 18 3
18 19 3
19 20 3
20 2 3
1 21 2
21 22 2
22 23 2
23 24 2
24 25 2
25 26 2
26 27 2
27 28 2
28 29 2
29 30 2
30 31 2
31 2 2
1 32 1
32 33 1
33 34 1
34 3...

output:

0

result:

ok single line: '0'

Test #12:

score: 0
Accepted
time: 57ms
memory: 49804kb

input:

54 60 1 2
1 2 25
1 3 12
3 2 12
1 4 8
4 5 8
5 2 8
1 6 6
6 7 6
7 8 6
8 2 6
1 9 4
9 10 4
10 11 4
11 12 4
12 13 4
13 2 4
1 14 3
14 15 3
15 16 3
16 17 3
17 18 3
18 19 3
19 20 3
20 2 3
1 21 2
21 22 2
22 23 2
23 24 2
24 25 2
25 26 2
26 27 2
27 28 2
28 29 2
29 30 2
30 31 2
31 2 2
1 32 1
32 33 1
33 34 1
34 3...

output:

25

result:

ok single line: '25'

Test #13:

score: 0
Accepted
time: 125ms
memory: 55972kb

input:

96 4560 95 45
1 2 3
1 3 3
1 4 3
1 5 3
1 6 3
1 7 3
1 8 3
1 9 2
1 10 1
1 11 2
1 12 2
1 13 3
1 14 1
1 15 2
1 16 3
1 17 3
1 18 3
1 19 2
1 20 2
1 21 2
1 22 3
1 23 3
1 24 1
1 25 2
1 26 3
1 27 3
1 28 3
1 29 1
1 30 2
1 31 2
1 32 2
1 33 1
1 34 2
1 35 2
1 36 3
1 37 2
1 38 1
1 39 2
1 40 3
1 41 2
1 42 1
1 43 1
...

output:

9137

result:

ok single line: '9137'

Test #14:

score: 0
Accepted
time: 97ms
memory: 51980kb

input:

68 2278 60 17
1 2 1
1 3 1
1 4 1
1 5 2
1 6 1
1 7 1
1 8 1
1 9 1
1 10 1
1 11 3
1 12 3
1 13 3
1 14 3
1 15 3
1 16 2
1 17 3
1 18 2
1 19 3
1 20 2
1 21 1
1 22 1
1 23 2
1 24 3
1 25 1
1 26 3
1 27 3
1 28 3
1 29 1
1 30 3
1 31 1
1 32 1
1 33 1
1 34 2
1 35 3
1 36 2
1 37 1
1 38 2
1 39 1
1 40 1
1 41 3
1 42 1
1 43 2
...

output:

4581

result:

ok single line: '4581'

Test #15:

score: 0
Accepted
time: 115ms
memory: 55900kb

input:

92 4186 55 9
1 2 1
1 3 3
1 4 2
1 5 3
1 6 3
1 7 1
1 8 3
1 9 1
1 10 2
1 11 1
1 12 2
1 13 3
1 14 1
1 15 3
1 16 3
1 17 2
1 18 1
1 19 1
1 20 1
1 21 3
1 22 3
1 23 1
1 24 1
1 25 3
1 26 3
1 27 2
1 28 1
1 29 1
1 30 2
1 31 2
1 32 3
1 33 1
1 34 3
1 35 2
1 36 2
1 37 1
1 38 1
1 39 2
1 40 1
1 41 1
1 42 2
1 43 3
1...

output:

8396

result:

ok single line: '8396'

Test #16:

score: 0
Accepted
time: 141ms
memory: 55740kb

input:

98 4753 62 63
1 2 3
1 3 1
1 4 1
1 5 1
1 6 1
1 7 3
1 8 3
1 9 3
1 10 2
1 11 1
1 12 2
1 13 1
1 14 3
1 15 2
1 16 3
1 17 2
1 18 1
1 19 2
1 20 1
1 21 3
1 22 3
1 23 3
1 24 1
1 25 2
1 26 1
1 27 3
1 28 2
1 29 1
1 30 1
1 31 2
1 32 2
1 33 3
1 34 2
1 35 3
1 36 3
1 37 2
1 38 2
1 39 2
1 40 3
1 41 2
1 42 3
1 43 3
...

output:

9488

result:

ok single line: '9488'

Test #17:

score: 0
Accepted
time: 112ms
memory: 52032kb

input:

68 2278 62 34
1 2 2
1 3 1
1 4 1
1 5 1
1 6 3
1 7 2
1 8 3
1 9 3
1 10 3
1 11 1
1 12 2
1 13 2
1 14 1
1 15 2
1 16 3
1 17 1
1 18 2
1 19 1
1 20 2
1 21 1
1 22 1
1 23 2
1 24 1
1 25 2
1 26 3
1 27 3
1 28 1
1 29 3
1 30 2
1 31 2
1 32 3
1 33 1
1 34 2
1 35 3
1 36 1
1 37 2
1 38 2
1 39 1
1 40 3
1 41 3
1 42 1
1 43 1
...

output:

4605

result:

ok single line: '4605'

Test #18:

score: 0
Accepted
time: 91ms
memory: 52096kb

input:

67 2211 46 47
1 2 3
1 3 2
1 4 2
1 5 1
1 6 1
1 7 1
1 8 3
1 9 2
1 10 1
1 11 2
1 12 2
1 13 3
1 14 2
1 15 3
1 16 3
1 17 1
1 18 2
1 19 3
1 20 3
1 21 3
1 22 1
1 23 1
1 24 3
1 25 2
1 26 3
1 27 1
1 28 3
1 29 3
1 30 3
1 31 3
1 32 1
1 33 2
1 34 1
1 35 2
1 36 3
1 37 3
1 38 3
1 39 2
1 40 2
1 41 1
1 42 3
1 43 3
...

output:

4439

result:

ok single line: '4439'

Test #19:

score: 0
Accepted
time: 94ms
memory: 51952kb

input:

67 2211 17 50
1 2 3
1 3 3
1 4 1
1 5 1
1 6 3
1 7 2
1 8 2
1 9 2
1 10 1
1 11 3
1 12 3
1 13 2
1 14 3
1 15 3
1 16 1
1 17 2
1 18 2
1 19 1
1 20 3
1 21 2
1 22 3
1 23 2
1 24 2
1 25 3
1 26 2
1 27 1
1 28 2
1 29 3
1 30 3
1 31 3
1 32 1
1 33 3
1 34 2
1 35 1
1 36 3
1 37 2
1 38 2
1 39 1
1 40 3
1 41 3
1 42 3
1 43 1
...

output:

4428

result:

ok single line: '4428'

Test #20:

score: 0
Accepted
time: 117ms
memory: 49952kb

input:

74 2701 61 70
1 2 1
1 3 2
1 4 2
1 5 2
1 6 2
1 7 3
1 8 3
1 9 3
1 10 1
1 11 2
1 12 1
1 13 2
1 14 1
1 15 2
1 16 3
1 17 1
1 18 2
1 19 3
1 20 1
1 21 2
1 22 3
1 23 2
1 24 3
1 25 3
1 26 1
1 27 3
1 28 3
1 29 3
1 30 1
1 31 2
1 32 3
1 33 1
1 34 1
1 35 1
1 36 3
1 37 2
1 38 3
1 39 1
1 40 2
1 41 3
1 42 1
1 43 3
...

output:

5420

result:

ok single line: '5420'

Test #21:

score: 0
Accepted
time: 106ms
memory: 52332kb

input:

77 2926 54 68
1 2 2
1 3 3
1 4 2
1 5 2
1 6 1
1 7 3
1 8 3
1 9 1
1 10 3
1 11 1
1 12 3
1 13 3
1 14 3
1 15 3
1 16 3
1 17 1
1 18 1
1 19 2
1 20 1
1 21 2
1 22 2
1 23 3
1 24 1
1 25 1
1 26 3
1 27 1
1 28 3
1 29 1
1 30 1
1 31 3
1 32 3
1 33 3
1 34 3
1 35 3
1 36 1
1 37 1
1 38 1
1 39 2
1 40 3
1 41 3
1 42 1
1 43 3
...

output:

5867

result:

ok single line: '5867'

Test #22:

score: 0
Accepted
time: 126ms
memory: 55060kb

input:

84 3486 17 46
1 2 2
1 3 3
1 4 3
1 5 1
1 6 1
1 7 2
1 8 2
1 9 3
1 10 3
1 11 1
1 12 1
1 13 3
1 14 2
1 15 1
1 16 1
1 17 1
1 18 1
1 19 1
1 20 2
1 21 3
1 22 3
1 23 1
1 24 3
1 25 1
1 26 3
1 27 3
1 28 1
1 29 2
1 30 3
1 31 1
1 32 2
1 33 2
1 34 1
1 35 3
1 36 2
1 37 3
1 38 3
1 39 1
1 40 1
1 41 3
1 42 2
1 43 3
...

output:

7034

result:

ok single line: '7034'

Test #23:

score: 0
Accepted
time: 45ms
memory: 54028kb

input:

4 2 1 2
1 2 10
3 4 5

output:

5

result:

ok single line: '5'

Test #24:

score: 0
Accepted
time: 83ms
memory: 52372kb

input:

30 379 15 27
1 2 8
1 3 46
1 4 16
1 5 2
1 6 27
1 7 84
1 8 1
1 9 53
1 10 56
1 11 78
1 12 56
1 13 67
1 14 21
1 16 43
1 17 44
1 18 10
1 19 36
1 20 61
1 21 80
1 22 46
1 23 9
1 24 53
1 25 72
1 26 34
1 28 71
1 29 68
1 30 16
2 3 96
2 4 99
2 5 23
2 6 86
2 7 24
2 8 17
2 9 27
2 10 70
2 11 88
2 12 31
2 13 94
2 ...

output:

19360

result:

ok single line: '19360'

Test #25:

score: 0
Accepted
time: 67ms
memory: 54216kb

input:

30 210 26 28
1 3 32
1 5 65
1 7 29
1 9 44
1 11 26
1 13 24
1 15 22
1 17 86
1 19 25
1 21 33
1 23 33
1 25 21
1 27 92
1 29 44
2 4 14
2 6 51
2 8 70
2 10 80
2 12 92
2 14 50
2 16 89
2 18 75
2 20 14
2 22 7
2 24 30
2 26 64
2 28 97
2 30 84
3 5 39
3 7 1
3 9 58
3 11 39
3 13 49
3 15 69
3 17 30
3 19 97
3 21 67
3 2...

output:

11126

result:

ok single line: '11126'