#include <iostream> #include <cstring> using namespace std; const int N = 1000010; int n, m, s, t; bool st[N]; int h[N], e[N], w[N], ne[N], idx; int d[N]; void add(int a, int b, int c) { e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx ++; } int dijkstra() { memset(d, 0x3f, sizeof d); d[s] = 0; for (int i = 0; i < n; i ++ ) { int t = -1; for (int j = 1; j <= n; j ++ ) if (!st[j] && (t == -1 || d[t] > d[j])) t = j; st[t] = true; for (int j = h[t]; j != -1; j = ne[j]) { int k = e[j]; if (d[k] > d[t] + w[j]) d[k] = d[t] + w[j]; } } return d[t]; } int main() { cin >> n >> m >> s >> t; memset(h, -1, sizeof h); while (m -- ) { int a, b, c; cin >> a >> b >> c; add(a, b, c); add(b, a, c); } cout << dijkstra() << endl; return 0; }帮我将这段代码中的变量a,b,c改为uvw
时间: 2024-01-18 10:05:16 浏览: 159
c++实现二叉查找树示例
好的,修改后的代码如下:
```c++
#include <iostream>
#include <cstring>
using namespace std;
const int N = 1000010;
int n, m, s, t;
bool st[N];
int h[N], e[N], w[N], ne[N], idx;
int d[N];
void add(int u, int v, int w) {
e[idx] = v, w[idx] = w, ne[idx] = h[u], h[u] = idx ++;
}
int dijkstra() {
memset(d, 0x3f, sizeof d);
d[s] = 0;
for (int i = 0; i < n; i ++ ) {
int t = -1;
for (int j = 1; j <= n; j ++ )
if (!st[j] && (t == -1 || d[t] > d[j]))
t = j;
st[t] = true;
for (int j = h[t]; j != -1; j = ne[j]) {
int k = e[j];
if (d[k] > d[t] + w[j])
d[k] = d[t] + w[j];
}
}
return d[t];
}
int main() {
cin >> n >> m >> s >> t;
memset(h, -1, sizeof h);
while (m -- ) {
int u, v, w;
cin >> u >> v >> w;
add(u, v, w);
add(v, u, w);
}
cout << dijkstra() << endl;
return 0;
}
```
阅读全文