C
#include <stdio.h>
#include <string.h>
#define MAXSIZE 510
#define INF 0x3f3f3f3f
#define min(a,b) ((a)<(b)?(a):(b))
int n, m, graph[MAXSIZE][MAXSIZE], visited[MAXSIZE], dist[MAXSIZE];
void addEdge(int from, int to, int weight) {
graph[from][to] = min(graph[from][to], weight);
}
void dijkstra(int start) {
dist[start] = 0;
for (int i = 1; i <= n; ++i) {
int v = -1, curMin = INF;
for (int j = 1; j <= n; ++j) {
if (!visited[j] && dist[j] < curMin) {
v = j;
curMin = dist[j];
}
}
if (v == -1) {
return;
}
visited[v] = 1;
for (int u = 1; u <= n; ++u) {
if (!visited[u] && graph[v][u] != INF && dist[v]+graph[v][u] < dist[u]) {
dist[u] = dist[v] + graph[v][u];
}
}
}
}
void init() {
int x, y, z;
memset(graph, 0x3f, sizeof(graph));
memset(dist, 0x3f, sizeof(dist));
scanf("%d%d", &n, &m);
while (m--) {
scanf("%d%d%d", &x, &y, &z);
addEdge(x, y, z);
}
}
int main() {
init();
dijkstra(1);
if (dist[n] == INF) {
puts("-1");
} else {
printf("%d\n", dist[n]);
}
return 0;
}
C++
#include <iostream>
#include <vector>
using namespace std;
const int MAXSIZE = 510;
const int INF = 0x3f3f3f3f;
int n, m;
vector<vector<int> > graph(MAXSIZE, vector<int>(MAXSIZE, INF));
vector<bool> visited(MAXSIZE, false);
vector<int> dist(MAXSIZE, INF);
void addEdge(int from, int to, int weight) {
graph[from][to] = min(graph[from][to], weight);
}
void dijkstra(int start) {
dist[start] = 0;
for (int i = 1; i <= n; ++i) {
int v = -1, curMin = INF;
for (int j = 1; j <= n; ++j) {
if (!visited[j] && dist[j] < curMin) {
v = j;
curMin = dist[j];
}
}
if (v == -1) {
return;
}
visited[v] = true;
for (int u = 1; u <= n; ++u) {
if (!visited[u] && graph[v][u] != INF && dist[v]+graph[v][u] < dist[u]) {
dist[u] = dist[v] + graph[v][u];
}
}
}
}
void init() {
int x, y, z;
cin >> n >> m;
while (m--) {
cin >> x >> y >> z;
addEdge(x, y, z);
}
}
int main() {
init();
dijkstra(1);
if (dist[n] == INF) {
cout << -1 << endl;
} else {
cout << dist[n] << endl;
}
return 0;
}
Go
package main
import "fmt"
const INF = 0x3f3f3f3f
var (
n, m int
graph [][]int
visited []bool
dist []int
)
func init() {
fmt.Scan(&n, &m)
graph = make([][]int, n+1)
for i := 0; i < len(graph); i++ {
graph[i] = make([]int, n+1)
for j := 0; j < len(graph[i]); j++ {
graph[i][j] = INF
}
}
visited = make([]bool, n+1)
dist = make([]int, n+1)
for i := 0; i < len(dist); i++ {
dist[i] = INF
}
var x, y, z int
for i := 0; i < m; i++ {
fmt.Scan(&x, &y, &z)
addEdge(x, y, z)
}
}
func minInt(a, b int) int {
if a < b {
return a
}
return b
}
func addEdge(from, to, weight int) {
graph[from][to] = minInt(graph[from][to], weight)
}
func dijkstra(start int) {
dist[start] = 0
for i := 1; i <= n; i++ {
v, curMin := -1, INF
for j := 1; j <= n; j++ {
if !visited[j] && dist[j] < curMin {
v = j
curMin = dist[j]
}
}
if v == -1 {
return
}
visited[v] = true
for u := 1; u <= n; u++ {
if !visited[u] && graph[v][u] != INF && dist[v]+graph[v][u] < dist[u] {
dist[u] = dist[v] + graph[v][u]
}
}
}
}
func main() {
dijkstra(1)
if dist[n] == INF {
fmt.Println(-1)
} else {
fmt.Println(dist[n])
}
}