【模板】Bellman-Ford
C++版本:
Bellman-Ford
void shortest_path(int s) {
for (int i = 0; i <= V; i++)d[i] = INF;
d[s] = 0;
while (true) {
bool update = false;
for (int i = 1; i <= E; i++) {
edge e = es[i];
if (d[e.from] != INF && d[e.to] > d[e.from] + e.cost) {
d[e.to] = d[e.from] + e.cost;
update = true;
}
}
if (!update)break;
}
}