下面贴上代码
#include <bits/stdc++.h>
#define INF 99999
using namespace std;
const int maxn = 1000;
int g[maxn][maxn];
int n, m, s;
bool v[maxn];
int d[maxn];
void dij(int x) {
for (int i = 0; i < n; i++)
d[i] = INF;
d[x] = 0;
for (int i = 0; i < n; i++) {
int tk, mn = INF;
for (int j = 0; j < n; j++)
if (!v[j] && d[j] <= mn)
mn = d[tk = j];
v[tk] = 1;
for (int k = 0; k < n; k++)
if (!v[k] && mn + g[tk][k] < d[k])
d[k] = mn + g[tk][k];
}
return;
}
int main() {
cin >> n >> m >> s;
s--;
//初始化,设置默认距离为INF,自己到自己为0;
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
if (i != j)
g[i][j] = INF;
int ta, tb;
for (int i = 0; i < m; i++) {
scanf("%d%d", &ta, &tb);
scanf("%d", &g[ta - 1][tb - 1]);
}
dij(s);
for (int i = 0; i < n; i++)
printf("%d ", d[i]);
return 0;
}
不考虑MLE RE TLE等问题,假定我的数组开的足够大,为什会WA呢?