Floyd算法,三重循环的顺序问题,按照中间节点的编号从1到n遍历,不要写错了
最外层的循环应该是,中间节点的变量从1~n:
1 for (k=1;k<=n;k++) 2 for (i=1;i<=n;i++) 3 for (j=1;j<=n;j++) 4 dist[i][j] = min(dist[i][j], dist[i][k]+dist[k][j]);
正确代码
1 #include <bits/stdc++.h> 2 using namespace std; 3 #define LL long long 4 #define ULL unsigned long long 5 6 const LL mod_1=1e9+7; 7 const LL mod_2=998244353; 8 9 const double eps_1=1e-5; 10 const double eps_2=1e-10; 11 12 const int maxn=1e3+10; 13 14 LL dist[maxn][maxn]; 15 16 int main() 17 { 18 LL i,j,k,n,m,u,v,w; 19 cin>>n>>m; 20 for (i=1;i<=n;i++) 21 for (j=1;j<=n;j++) 22 dist[i][j] = 1e15; 23 for (i=1;i<=n;i++) 24 dist[i][i] = 0; 25 for (i=1;i<=m;i++) 26 { 27 cin>>u>>v>>w; 28 dist[u][v] = dist[v][u] = min(dist[u][v], w); 29 } 30 31 for (k=1;k<=n;k++) 32 for (i=1;i<=n;i++) 33 for (j=1;j<=n;j++) 34 dist[i][j] = min(dist[i][j], dist[i][k]+dist[k][j]); 35 //可以经过的点,k=1~n,逐渐递增 36 37 for (i=1;i<=n;i++) 38 { 39 for (j=1;j<=n;j++) 40 { 41 cout<<dist[i][j]; 42 if (j!=n) 43 cout<<" "; 44 else 45 cout<<endl; 46 } 47 } 48 49 return 0; 50 } 51 /* 52 3 2 53 1 2 1 54 2 3 2 55 */
错误代码
1 #include <bits/stdc++.h> 2 using namespace std; 3 #define LL long long 4 #define ULL unsigned long long 5 6 const LL mod_1=1e9+7; 7 const LL mod_2=998244353; 8 9 const double eps_1=1e-5; 10 const double eps_2=1e-10; 11 12 const int maxn=1e3+10; 13 14 LL dist[maxn][maxn]; 15 16 int main() 17 { 18 LL i,j,k,n,m,u,v,w; 19 cin>>n>>m; 20 for (i=1;i<=n;i++) 21 for (j=1;j<=n;j++) 22 dist[i][j] = 1e15; 23 for (i=1;i<=n;i++) 24 dist[i][i] = 0; 25 for (i=1;i<=m;i++) 26 { 27 cin>>u>>v>>w; 28 dist[u][v] = dist[v][u] = min(dist[u][v], w); 29 } 30 31 for (i=1;i<=n;i++) 32 for (j=1;j<=n;j++) 33 for (k=1;k<=n;k++) 34 dist[i][j] = min(dist[i][j], dist[i][k]+dist[k][j]); 35 //dist[1][2] i=1,j=2 一开始被确定 1->k->2 但是这样的k不存在,于是dist[1][2] = 初始设置的最大值 36 37 for (i=1;i<=n;i++) 38 { 39 for (j=1;j<=n;j++) 40 { 41 cout<<dist[i][j]; 42 if (j!=n) 43 cout<<" "; 44 else 45 cout<<endl; 46 } 47 } 48 49 return 0; 50 } 51 /* 52 3 2 53 1 2 1 54 2 3 2 55 */
错误样例:
dist[1][2] i=1,j=2 一开始被确定 1->k->2 但是这样的k不存在,于是dist[1][2] = 初始设置的最大值
input:
5 10 4 3 7 1 4 7 2 3 9 5 4 8 3 2 2 2 5 2 2 3 8 4 5 3 1 4 5 3 4 1
output:
0 8 6 5 8 8 0 2 3 2 6 2 0 1 4 5 3 1 0 3 8 2 4 3 0
制造样例:
1 #include <bits/stdc++.h> 2 using namespace std; 3 #define LL long long 4 #define ULL unsigned long long 5 6 const LL mod_1=1e9+7; 7 const LL mod_2=998244353; 8 9 const double eps_1=1e-5; 10 const double eps_2=1e-10; 11 12 const int maxn=1e3+10; 13 14 int a[maxn]; 15 16 int main() 17 { 18 int n,m,v_max=10,i,j,k; 19 srand(time(NULL)); 20 21 //先创建一棵树 22 //crayon ? 23 24 //n=5; 25 //m=10; 26 27 n=4; 28 m=8; 29 30 cout<<n<<' '<<m<<endl; 31 32 for (i=1;i<=n;i++) 33 a[i]=i; 34 random_shuffle(a+1,a+n+1); 35 36 for (i=2;i<=n;i++) 37 { 38 j=rand()%(i-1)+1; 39 40 cout<<a[i]<<' '<<a[j]<<' '<<rand()%v_max+1<<endl; 41 } 42 43 for (i=1;i<=m-n+1;i++) 44 { 45 j=rand()%n+1; 46 while (1) 47 { 48 k=rand()%n+1; 49 if (k!=j) 50 break; 51 } 52 cout<<j<<' '<<k<<' '<<rand()%v_max+1<<endl; 53 } 54 55 return 0; 56 }