Files
python/TangDou/AcWing/P1339_BellmanFord_Edge.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

40 lines
834 B
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const int N = 2510; //结点数
const int M = 6200 * 2 + 10; //边数
struct Edge {
int a, b, c;
} edges[M];
int n, m; // n个结点m条边
int s, t; //起点,终点
int d[N]; //距离数组
void bellman_ford(int start) {
memset(d, 0x3f, sizeof d);
d[start] = 0;
int T = n - 1;
while (T--) {
for (int i = 0; i < 2 * m; i++) {
auto j = edges[i];
d[j.b] = min(d[j.b], d[j.a] + j.c);
}
}
}
int main() {
cin >> n >> m >> s >> t;
for (int i = 0; i < m; i++) {
int a, b, c;
cin >> a >> b >> c;
edges[i] = {a, b, c}, edges[m + i] = {b, a, c};
}
bellman_ford(s);
cout << d[t] << endl;
return 0;
}