Skip to content

Commit 7ddc751

Browse files
Merge pull request #2675 from learnerInTheFirstStage/master
更新0094.城市间货物运输I.md Java版本解法
2 parents 23852d3 + ab6cb84 commit 7ddc751

File tree

4 files changed

+279
-0
lines changed

4 files changed

+279
-0
lines changed

problems/kamacoder/0094.城市间货物运输I-SPFA.md

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,77 @@ SPFA(队列优化版Bellman_ford) 在理论上 时间复杂度更胜一筹
352352
## 其他语言版本
353353

354354
### Java
355+
```Java
356+
import java.util.*;
357+
358+
public class Main {
359+
360+
// Define an inner class Edge
361+
static class Edge {
362+
int from;
363+
int to;
364+
int val;
365+
public Edge(int from, int to, int val) {
366+
this.from = from;
367+
this.to = to;
368+
this.val = val;
369+
}
370+
}
371+
372+
public static void main(String[] args) {
373+
// Input processing
374+
Scanner sc = new Scanner(System.in);
375+
int n = sc.nextInt();
376+
int m = sc.nextInt();
377+
List<List<Edge>> graph = new ArrayList<>();
378+
379+
for (int i = 0; i <= n; i++) {
380+
graph.add(new ArrayList<>());
381+
}
382+
383+
for (int i = 0; i < m; i++) {
384+
int from = sc.nextInt();
385+
int to = sc.nextInt();
386+
int val = sc.nextInt();
387+
graph.get(from).add(new Edge(from, to, val));
388+
}
389+
390+
// Declare the minDist array to record the minimum distance form current node to the original node
391+
int[] minDist = new int[n + 1];
392+
Arrays.fill(minDist, Integer.MAX_VALUE);
393+
minDist[1] = 0;
394+
395+
// Declare a queue to store the updated nodes instead of traversing all nodes each loop for more efficiency
396+
Queue<Integer> queue = new LinkedList<>();
397+
queue.offer(1);
398+
399+
// Declare a boolean array to record if the current node is in the queue to optimise the processing
400+
boolean[] isInQueue = new boolean[n + 1];
401+
402+
while (!queue.isEmpty()) {
403+
int curNode = queue.poll();
404+
isInQueue[curNode] = false; // Represents the current node is not in the queue after being polled
405+
for (Edge edge : graph.get(curNode)) {
406+
if (minDist[edge.to] > minDist[edge.from] + edge.val) { // Start relaxing the edge
407+
minDist[edge.to] = minDist[edge.from] + edge.val;
408+
if (!isInQueue[edge.to]) { // Don't add the node if it's already in the queue
409+
queue.offer(edge.to);
410+
isInQueue[edge.to] = true;
411+
}
412+
}
413+
}
414+
}
415+
416+
// Outcome printing
417+
if (minDist[n] == Integer.MAX_VALUE) {
418+
System.out.println("unconnected");
419+
} else {
420+
System.out.println(minDist[n]);
421+
}
422+
}
423+
}
424+
425+
```
355426

356427
### Python
357428

problems/kamacoder/0094.城市间货物运输I.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,63 @@ Bellman_ford 是可以计算 负权值的单源最短路算法。
392392
## 其他语言版本
393393

394394
### Java
395+
```Java
396+
public class Main {
397+
398+
// Define an inner class Edge
399+
static class Edge {
400+
int from;
401+
int to;
402+
int val;
403+
public Edge(int from, int to, int val) {
404+
this.from = from;
405+
this.to = to;
406+
this.val = val;
407+
}
408+
}
409+
410+
public static void main(String[] args) {
411+
// Input processing
412+
Scanner sc = new Scanner(System.in);
413+
int n = sc.nextInt();
414+
int m = sc.nextInt();
415+
List<Edge> edges = new ArrayList<>();
416+
417+
for (int i = 0; i < m; i++) {
418+
int from = sc.nextInt();
419+
int to = sc.nextInt();
420+
int val = sc.nextInt();
421+
edges.add(new Edge(from, to, val));
422+
}
423+
424+
// Represents the minimum distance from the current node to the original node
425+
int[] minDist = new int[n + 1];
426+
427+
// Initialize the minDist array
428+
Arrays.fill(minDist, Integer.MAX_VALUE);
429+
minDist[1] = 0;
430+
431+
// Starts the loop to relax all edges n - 1 times to update minDist array
432+
for (int i = 1; i < n; i++) {
433+
434+
for (Edge edge : edges) {
435+
// Updates the minDist array
436+
if (minDist[edge.from] != Integer.MAX_VALUE && (minDist[edge.from] + edge.val) < minDist[edge.to]) {
437+
minDist[edge.to] = minDist[edge.from] + edge.val;
438+
}
439+
}
440+
}
441+
442+
// Outcome printing
443+
if (minDist[n] == Integer.MAX_VALUE) {
444+
System.out.println("unconnected");
445+
} else {
446+
System.out.println(minDist[n]);
447+
}
448+
}
449+
}
450+
451+
```
395452

396453
### Python
397454

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+
// 基于Bellman_ford-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

problems/kamacoder/0096.城市间货物运输III.md

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,71 @@ dijkstra 是贪心的思路 每一次搜索都只会找距离源点最近的非
636636
## 其他语言版本
637637

638638
### Java
639+
```Java
640+
import java.util.*;
641+
642+
public class Main {
643+
// 基于Bellman_for一般解法解决单源最短路径问题
644+
// Define an inner class Edge
645+
static class Edge {
646+
int from;
647+
int to;
648+
int val;
649+
public Edge(int from, int to, int val) {
650+
this.from = from;
651+
this.to = to;
652+
this.val = val;
653+
}
654+
}
655+
656+
public static void main(String[] args) {
657+
// Input processing
658+
Scanner sc = new Scanner(System.in);
659+
int n = sc.nextInt();
660+
int m = sc.nextInt();
661+
662+
List<Edge> graph = new ArrayList<>();
663+
664+
for (int i = 0; i < m; i++) {
665+
int from = sc.nextInt();
666+
int to = sc.nextInt();
667+
int val = sc.nextInt();
668+
graph.add(new Edge(from, to, val));
669+
}
670+
671+
int src = sc.nextInt();
672+
int dst = sc.nextInt();
673+
int k = sc.nextInt();
674+
675+
int[] minDist = new int[n + 1];
676+
int[] minDistCopy;
677+
678+
Arrays.fill(minDist, Integer.MAX_VALUE);
679+
minDist[src] = 0;
680+
681+
for (int i = 0; i < k + 1; i++) { // Relax all edges k + 1 times
682+
minDistCopy = Arrays.copyOf(minDist, n + 1);
683+
for (Edge edge : graph) {
684+
int from = edge.from;
685+
int to = edge.to;
686+
int val = edge.val;
687+
// Use minDistCopy to calculate minDist
688+
if (minDistCopy[from] != Integer.MAX_VALUE && minDist[to] > minDistCopy[from] + val) {
689+
minDist[to] = minDistCopy[from] + val;
690+
}
691+
}
692+
}
693+
694+
// Output printing
695+
if (minDist[dst] == Integer.MAX_VALUE) {
696+
System.out.println("unreachable");
697+
} else {
698+
System.out.println(minDist[dst]);
699+
}
700+
}
701+
}
702+
703+
```
639704

640705
### Python
641706

0 commit comments

Comments
 (0)