Extension of the Floyd algorithm

Source: Internet
Author: User

Extension of the Floyd algorithm

The Floyd algorithm is a shortest distance from any point to arbitrary point. It is possible to find the shortest path of a negative value, a direction graph, a non-direction graph, etc. But the edge of the right can have negative weight of the side, but can not contain the negative weight of the side of the loop, otherwise the calculation is not the correct answer!! This algorithm is very inefficient and can be said to be violent. But this violence is violent with dynamic planning!! But there is no right side or negative loop.

On the Code
#include <cstdio>#include<cstring>#include<algorithm>using namespacestd;#defineINF 1<<29intmp[ -][ -];intn,m;intMain () { while(~SCANF ("%d%d", &n,&m)) {//represents a weighted edge of N-node M-Bar         for(intI=1; i<=n;i++){             for(intj=1; j<=n;j++){                if(I!=J) {//initializing the default distance represents the distance from the I node to the J node. nodes with the same distance of 0mp[i][j]=Inf; }Else{Mp[i][j]=0; }            }        }        intA,b,c;  for(intI=1; i<=m;i++) {scanf ("%d%d%d",&a,&b,&c); MP[A][B]=C; //mp[b][a]=c;//This is a no-show diagram.        }         for(intk=1; k<=n;k++) {//The main code implementation area, you can simulate a bit. The key is to simulate, and then think for yourself why.              for(intI=1; i<=n;i++){                 for(intj=1; j<=n;j++){                    if(i!=j&&j!=k) {Mp[i][j]=min (mp[i][j],mp[i][k]+Mp[k][j]); }                }            }        }         for(intI=1; i<=n;i++){             for(intj=1; j<=n;j++) {//The distance from any point to any point is output hereprintf"dis%d%d =%d\n", I,j,mp[i][j]); }        }    }    return 0;}

Extension of the Floyd algorithm

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.