반응형
https://www.acmicpc.net/problem/1238
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
|
package com.ssay.algo;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.PriorityQueue;
import java.util.StringTokenizer;
public class B1238 {
public static class Town implements Comparable<Town> {
int u;
int w;
public Town(int u, int w) {
super();
this.u = u;
this.w = w;
}
@Override
public int compareTo(Town o) {
// TODO Auto-generated method stub
if (this.w > o.w)
return 1;
else
return -1;
}
}
static int result;
static int minValue;
static int N, M, X;
static int backDigit[];
static int digit[];
static ArrayList<Town>[] list;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
N = Integer.parseInt(st.nextToken());
M = Integer.parseInt(st.nextToken());
X = Integer.parseInt(st.nextToken());
list = new ArrayList[N + 1];
for (int n = 1; n <= N; n++) {
list[n] = new ArrayList<>();
}
int u, v, w;
for (int m = 0; m < M; m++) {
st = new StringTokenizer(br.readLine());
u = Integer.parseInt(st.nextToken());
v = Integer.parseInt(st.nextToken());
w = Integer.parseInt(st.nextToken());
list[u].add(new Town(v,w));
}
digit = new int[N + 1];
backDigit = new int[N + 1];
// X~ 모든 마을로가는 방법 backDigit 채우기
Dijkstra(X, backDigit);
// 각각의 마을에서 X 마을로 가는 방법
result = Integer.MIN_VALUE;
for (int n = 1; n <= N; n++) {
if (n == X)
continue;
// 다잌스트라 값 반환
Dijkstra(n, digit);
int sum = digit[X] + backDigit[n];
result = Math.max(result, sum);
}
System.out.println(result);
}
static void Dijkstra(int u, int[] Digit) {
Digit[u] = 0;
PriorityQueue<Town> q = new PriorityQueue<>();
q.add(new Town(u,0));
while (!q.isEmpty()) {
Town tmp = q.poll();
int len = list[tmp.u].size();
for (int i = 0; i < len; i++) {
if(Digit[tmp.u] + list[tmp.u].get(i).w < Digit[list[tmp.u].get(i).u]) {
Digit[list[tmp.u].get(i).u] = Digit[tmp.u] + list[tmp.u].get(i).w;
q.add(new Town(list[tmp.u].get(i).u,Digit[list[tmp.u].get(i).u]));
}
}
}
}
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
|
반응형
'ProgramSoliving' 카테고리의 다른 글
백준 : 1504 (0) | 2020.02.08 |
---|---|
백준 : 1261 (0) | 2020.02.08 |
백준 : 17136 (0) | 2020.02.07 |
백준 : 1916 (0) | 2020.02.07 |
백준 : 17142 (0) | 2020.02.03 |