Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[pull] master from youngyangyang04:master #475

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
jenningsloy318 merged 11 commits into AlgorithmAndLeetCode:master from youngyangyang04:master
Aug 21, 2024
Merged
Changes from 1 commit
Commits
Show all changes
11 commits
Select commit Hold shift + click to select a range
00405e5
更新0094.城市间货物运输I.md Java版本解法
learnerInTheFirstStage Aug 7, 2024
2ecb6a5
更新0094.城市间货物运输I-SPFA Java版本解法
learnerInTheFirstStage Aug 7, 2024
d90d5fb
添加 卡码网0104.建造最大岛屿 JS版
nineninee Aug 8, 2024
aff4169
添加 卡码网0107.寻找存在的路径 JS版
nineninee Aug 8, 2024
f3dae1f
Merge branch 'youngyangyang04:master' into master
learnerInTheFirstStage Aug 8, 2024
047c57a
更新0095.城市间货物运输II 基于DPFA版本的Java解法
learnerInTheFirstStage Aug 8, 2024
d2163ac
更新0095.城市间货物运输II 基于SPFA版本的Java解法
learnerInTheFirstStage Aug 8, 2024
ab6cb84
更新0096.城市间货物运输III 基于Bellman_ford一般解法Java版本
learnerInTheFirstStage Aug 9, 2024
7ddc751
Merge pull request #2675 from learnerInTheFirstStage/master
youngyangyang04 Aug 21, 2024
2cc0080
Merge pull request #2677 from nineninee/km0104
youngyangyang04 Aug 21, 2024
488f040
Merge pull request #2678 from nineninee/km0107
youngyangyang04 Aug 21, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
更新0094.城市间货物运输I.md Java版本解法
  • Loading branch information
commit 00405e519283b681daca6bb0f9dc5d292c1cc9ab
57 changes: 57 additions & 0 deletions problems/kamacoder/0094.城市间货物运输I.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,63 @@ Bellman_ford 是可以计算 负权值的单源最短路算法。
## 其他语言版本

### Java
```Java
public class Main {

// Define an inner class Edge
static class Edge {
int from;
int to;
int val;
public Edge(int from, int to, int val) {
this.from = from;
this.to = to;
this.val = val;
}
}

public static void main(String[] args) {
// Input processing
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
List<Edge> edges = new ArrayList<>();

for (int i = 0; i < m; i++) {
int from = sc.nextInt();
int to = sc.nextInt();
int val = sc.nextInt();
edges.add(new Edge(from, to, val));
}

// Represents the minimum distance from the current node to the original node
int[] minDist = new int[n + 1];

// Initialize the minDist array
Arrays.fill(minDist, Integer.MAX_VALUE);
minDist[1] = 0;

// Starts the loop to relax all edges n - 1 times to update minDist array
for (int i = 1; i < n; i++) {

for (Edge edge : edges) {
// Updates the minDist array
if (minDist[edge.from] != Integer.MAX_VALUE && (minDist[edge.from] + edge.val) < minDist[edge.to]) {
minDist[edge.to] = minDist[edge.from] + edge.val;
}
}
}

// Outcome printing
if (minDist[n] == Integer.MAX_VALUE) {
System.out.println("unconnected");
} else {
System.out.println(minDist[n]);
}
}
}

```

### Python

Expand Down

AltStyle によって変換されたページ (->オリジナル) /