Skip to content

Commit 33692a5

Browse files
authored
[20251021] BOJ / P5 / 개코전쟁 / 이종환
1 parent 8f9fb9e commit 33692a5

File tree

1 file changed

+181
-0
lines changed

1 file changed

+181
-0
lines changed
Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
```java
2+
import java.io.BufferedReader;
3+
import java.io.IOException;
4+
import java.io.InputStreamReader;
5+
import java.util.ArrayList;
6+
import java.util.Arrays;
7+
import java.util.HashSet;
8+
import java.util.StringTokenizer;
9+
10+
public class Main {
11+
12+
static boolean[] visited;
13+
static int nodeCnt,edgeCnt,ans;
14+
static Node[] nodes;
15+
static int[] cost;
16+
17+
static class Node{
18+
int num;
19+
HashSet<Edge> edges = new HashSet<>();
20+
ArrayList<Node> way = new ArrayList<>(); // 이 곳으로 오기위한 길 기록
21+
22+
public Node(int num) {
23+
this.num = num;
24+
}
25+
}
26+
27+
static class Edge{
28+
boolean isAlive = true;
29+
Node from;
30+
Node to;
31+
int cost;
32+
33+
public Edge(Node from, Node to, int cost) {
34+
this.from = from;
35+
this.to = to;
36+
this.cost = cost;
37+
}
38+
}
39+
40+
public static void main(String[] args) throws IOException {
41+
init();
42+
process();
43+
print();
44+
}
45+
46+
private static void init() throws IOException{
47+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
48+
StringTokenizer st = new StringTokenizer(br.readLine());
49+
nodeCnt = Integer.parseInt(st.nextToken());
50+
edgeCnt = Integer.parseInt(st.nextToken());
51+
ans = 0;
52+
53+
visited = new boolean[nodeCnt+1];
54+
cost = new int[nodeCnt+1];
55+
nodes = new Node[nodeCnt+1];
56+
for (int i = 0; i <= nodeCnt; i++) {
57+
nodes[i] = new Node(i);
58+
}
59+
60+
Arrays.fill(cost, Integer.MAX_VALUE);
61+
cost[1] = 0;
62+
63+
64+
65+
for (int i = 0; i < edgeCnt; i++) {
66+
st = new StringTokenizer(br.readLine());
67+
Node n1 = nodes[Integer.parseInt(st.nextToken())];
68+
Node n2 = nodes[Integer.parseInt(st.nextToken())];
69+
int cost = Integer.parseInt(st.nextToken());
70+
71+
n1.edges.add(new Edge(n1,n2,cost));
72+
n2.edges.add(new Edge(n2,n1,cost));
73+
}
74+
}
75+
76+
private static void process() {
77+
HashSet<Node> set =new HashSet<>();
78+
for (Node n: nodes) set.add(n);
79+
Node node = nodes[1];
80+
while (!set.isEmpty()) {
81+
set.remove(node);
82+
83+
for (Edge e: node.edges) {
84+
Node to = e.to;
85+
if (cost[to.num] > cost[node.num] + e.cost) {
86+
cost[to.num] = cost[node.num] + e.cost;
87+
88+
to.way = new ArrayList<>();
89+
to.way.addAll(node.way);
90+
to.way.add(node);
91+
}
92+
}
93+
94+
int idx = -1;
95+
int min = Integer.MAX_VALUE;
96+
97+
for (Node n: set) {
98+
// System.out.println(cost[n.num]);
99+
if (cost[n.num] < min) {
100+
min = cost[n.num];
101+
idx = n.num;
102+
}
103+
}
104+
if (idx == -1) break;
105+
node = nodes[idx];
106+
107+
108+
}
109+
110+
111+
ArrayList<Node> bestWay = nodes[nodeCnt].way;
112+
bestWay.add(nodes[nodeCnt]);
113+
int wayLen = bestWay.size();
114+
for (int i = 0; i < wayLen-1; i++) {
115+
Node cur = bestWay.get(i);
116+
Node next = bestWay.get(i+1);
117+
118+
for (Edge e: cur.edges) {
119+
if (e.to.equals(next)) e.isAlive = false;
120+
}
121+
for (Edge e: next.edges) {
122+
if (e.to.equals(cur)) e.isAlive = false;
123+
}
124+
125+
// System.out.println(cur.num + "->" + next.num);
126+
simulate();
127+
128+
for (Edge e: cur.edges) {
129+
if (e.to.equals(next)) e.isAlive = true;
130+
}
131+
for (Edge e: next.edges) {
132+
if (e.to.equals(cur)) e.isAlive = true;
133+
}
134+
}
135+
136+
}
137+
138+
private static void simulate() {
139+
HashSet<Node> set =new HashSet<>();
140+
for (Node n: nodes) set.add(n);
141+
142+
Arrays.fill(cost, Integer.MAX_VALUE);
143+
cost[1] = 0;
144+
145+
Node node = nodes[1];
146+
while (!set.isEmpty()) {
147+
set.remove(node);
148+
149+
for (Edge e: node.edges) {
150+
if (!e.isAlive) continue;
151+
Node to = e.to;
152+
if (cost[to.num] > cost[node.num] + e.cost) {
153+
cost[to.num] = cost[node.num] + e.cost;
154+
}
155+
}
156+
157+
int idx = -1;
158+
int min = Integer.MAX_VALUE;
159+
160+
for (Node n: set) {
161+
// System.out.println(cost[n.num]);
162+
if (cost[n.num] < min) {
163+
min = cost[n.num];
164+
idx = n.num;
165+
}
166+
}
167+
if (idx == -1) break;
168+
node = nodes[idx];
169+
170+
171+
}
172+
// System.out.println(cost[nodeCnt]);
173+
ans = Math.max(ans, cost[nodeCnt]);
174+
}
175+
176+
private static void print() {
177+
System.out.println(ans);
178+
}
179+
180+
}
181+
```

0 commit comments

Comments
 (0)