0
1
mirror of https://git.sb/baoshuo/OI-codes.git synced 2024-11-27 17:56:28 +00:00

P3371 【模板】单源最短路径(弱化版)[70' Code]

R53520699 #Unaccepted
This commit is contained in:
Baoshuo Ren 2021-07-18 22:01:45 +08:00 committed by Baoshuo Ren
parent 8f125a7b89
commit b0dd339cbf
Signed by: baoshuo
GPG Key ID: 70F90A673FB1AB68

View File

@ -2,33 +2,41 @@
using namespace std;
int n, m, s;
long long u, v, w, f[10005][10005];
int n, m, s, x, y, z, g[10005][10005], dist[10005];
bool st[10005];
int main() {
scanf("%d%d%d", &n, &m, &s);
for (int i = 1; i <= n; i++) {
void diskstra() {
memset(dist, 0x3f, sizeof(dist));
dist[s] = 0;
for (int i = 0; i < n - 1; i++) {
int t = -1;
for (int j = 1; j <= n; j++) {
f[i][j] = i == j ? 0 : INT_MAX;
}
}
for (int i = 0; i < m; i++) {
scanf("%d%d%d", &u, &v, &w);
f[u][v] = min(f[u][v], w);
}
for (int k = 1; k <= n; k++) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (i == k || f[i][k] == INT_MAX) {
continue;
}
f[i][j] = min(f[i][j], f[i][k] + f[k][j]);
if (!st[j] && (t == -1 || dist[t] > dist[j])) {
t = j;
}
}
for (int j = 1; j <= n; j++) {
dist[j] = min(dist[j], dist[t] + g[t][j]);
}
st[t] = true;
}
}
int main() {
cin >> n >> m >> s;
for (int i = 1; i <= n; i++) {
printf("%d ", f[s][i]);
for (int j = 1; j <= n; j++) {
g[i][j] = i == j ? 0 : 0x3f3f3f3f;
}
}
printf("\n");
for (int i = 1; i <= m; i++) {
cin >> x >> y >> z;
g[x][y] = min(g[x][y], z);
}
diskstra();
for (int i = 1; i <= n; i++) {
cout << (dist[i] == 0x3f3f3f3f ? INT_MAX : dist[i]) << ' ';
}
cout << endl;
return 0;
}