Source:
#include <cstdio>
#include <cstring>
int N,i[1001][1001],f[1001][1001],ans;
int main ()
{
memset (i,0x3f,sizeof (i));
memset (f,0x3f,sizeof (f));
ans=0x3f;//For storing variables and arrays, initialize to the maximum value.
scanf ("%d", &n);
for (int a=1;a<=n;a++)
for (int b=1;b<=n;b++)
{
int t;
scanf ("%d", &t);
if (t!=-1)
f[a][b]=i[a][b]=t;
}//Matrix input.
for (int a=1;a<=n;a++)
{
The length of the for (int b=1;b<a;b++)//ring is defined as the sum of the lengths of two different roads of one point to another.
for (int c=b+1;c<a;c++)//This algorithm excludes self-loops.
if (ans>i[b][c]+f[c][a]+f[a][b])
Ans=i[b][c]+f[c][a]+f[a][b],//[(C--and a-and B) + (B--and C) the shortest circuit] is the smallest ring of B and C.
for (int b=1;b<=n;b++)
for (int c=1;c<=n;c++)
if (I[b][a]+i[a][c]<i[b][c])
i[b][c]=i[b][a]+i[a][c];//By using the Floyd algorithm, the shortest path is updated.
}
printf ("%d", ans);
return 0;
}
Minimum ring--floyd variant algorithm (c + +)