Minimum ring--floyd variant algorithm (c + +)

Source: Internet
Author: User

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 + +)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.