1.题目:求出从某一点到达任意一点的最短距离(到不了的点设为10000)
2.源代码:
#include <iostream>
#include<stdio.h>
#define n 4//矩阵维度大小(需更改)
int main()
{
FILE* fpr, * fpw;
fopen_s(&fpr, "1.txt", "r");
fopen_s(&fpw, "2.txt", "w");
int a[n][n], i, j, k;
for (i = 0; i < n; i++)
for (j = 0; j < n; j++)
fscanf_s(fpr, "%d", &a[i][j]);
for (k = 0; k < n; k++)
for (i = 0; i < n; i++)
for (j = 0; j < n; j++)
if (a[i][k] + a[k][j] < a[i][j])
a[i][j] = a[i][k] + a[k][j];
for (i = 0; i < n; i++)
{
for (j = 0; j < n; j++)
fprintf(fpw, "%d ", a[i][j]);
fprintf(fpw,"\n");
}
fclose(fpr);
fclose(fpw);
}
3.输入输出: