Skip to content

Commit 047c57a

Browse files
更新0095.城市间货物运输II 基于DPFA版本的Java解法
1 parent f3dae1f commit 047c57a

File tree

1 file changed

+86
-0
lines changed

1 file changed

+86
-0
lines changed

problems/kamacoder/0095.城市间货物运输II.md

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,92 @@ int main() {
244244
## 其他语言版本
245245
246246
### Java
247+
```Java
248+
import java.util.*;
249+
250+
public class Main {
251+
// 基于SPFA方法
252+
// Define an inner class Edge
253+
static class Edge {
254+
int from;
255+
int to;
256+
int val;
257+
public Edge(int from, int to, int val) {
258+
this.from = from;
259+
this.to = to;
260+
this.val = val;
261+
}
262+
}
263+
264+
public static void main(String[] args) {
265+
// Input processing
266+
Scanner sc = new Scanner(System.in);
267+
int n = sc.nextInt();
268+
int m = sc.nextInt();
269+
List<List<Edge>> graph = new ArrayList<>();
270+
271+
for (int i = 0; i <= n; i++) {
272+
graph.add(new ArrayList<>());
273+
}
274+
275+
for (int i = 0; i < m; i++) {
276+
int from = sc.nextInt();
277+
int to = sc.nextInt();
278+
int val = sc.nextInt();
279+
graph.get(from).add(new Edge(from, to, val));
280+
}
281+
282+
// Declare the minDist array to record the minimum distance form current node to the original node
283+
int[] minDist = new int[n + 1];
284+
Arrays.fill(minDist, Integer.MAX_VALUE);
285+
minDist[1] = 0;
286+
287+
// Declare a queue to store the updated nodes instead of traversing all nodes each loop for more efficiency
288+
Queue<Integer> queue = new LinkedList<>();
289+
queue.offer(1);
290+
291+
// Declare an array to record the times each node has been offered in the queue
292+
int[] count = new int[n + 1];
293+
count[1]++;
294+
295+
// Declare a boolean array to record if the current node is in the queue to optimise the processing
296+
boolean[] isInQueue = new boolean[n + 1];
297+
298+
// Declare a boolean value to check if there is a negative weight loop inside the graph
299+
boolean flag = false;
300+
301+
while (!queue.isEmpty()) {
302+
int curNode = queue.poll();
303+
isInQueue[curNode] = false; // Represents the current node is not in the queue after being polled
304+
for (Edge edge : graph.get(curNode)) {
305+
if (minDist[edge.to] > minDist[edge.from] + edge.val) { // Start relaxing the edge
306+
minDist[edge.to] = minDist[edge.from] + edge.val;
307+
if (!isInQueue[edge.to]) { // Don't add the node if it's already in the queue
308+
queue.offer(edge.to);
309+
count[edge.to]++;
310+
isInQueue[edge.to] = true;
311+
}
312+
313+
if (count[edge.to] == n) { // If some node has been offered in the queue more than n-1 times
314+
flag = true;
315+
while (!queue.isEmpty()) queue.poll();
316+
break;
317+
}
318+
}
319+
}
320+
}
321+
322+
if (flag) {
323+
System.out.println("circle");
324+
} else if (minDist[n] == Integer.MAX_VALUE) {
325+
System.out.println("unconnected");
326+
} else {
327+
System.out.println(minDist[n]);
328+
}
329+
}
330+
}
331+
332+
```
247333

248334
### Python
249335

0 commit comments

Comments
 (0)