floyed algorithm O (N3) x

Source: Internet
Author: User

floyed algorithm O (N3) abbreviation floyed (Freud) algorithm, is the simplest shortest path algorithm, can calculate the shortest path between any two points in the graph. The time complexity of the floyed is O (N3), which is suitable for cases where negative edge rights are present. Algorithm analysis & Ideas explained: Three-layer cycle, the first cycle of the middle point K, the second third layer of the beginning of the cycle of the end point I, J, the idea of the algorithm is easy to understand: If the point I points k distance plus point K to J distance is less than the original point I to the distance J, Then use this shorter path length to update the original point I to the distance J. When we initialize, the distance between the unconnected points is set to a large number, it may be considered that the two points are very far apart, if there is a shortest path between the two, it will be updated to the shortest path length. The time complexity of the floyed algorithm is O (N3).
1#include <iostream>2#include <cstdio>3#include <cstring>4 5 using namespacestd;6 Const intmaxn=1001;7 8 intMAPS[MAXN][MAXN];9 intans;Ten intMain () { Onememset (Maps,999999,sizeof(maps)); A     intn,m; -Cin>>n>>m; -     intHe,ta,len; the      for(intI=1; i<=m; i++) { -Cin>>he>>ta>>Len; -maps[ta][he]=maps[he][ta]=Len; -     } +     intx, y; -Cin>>x>>y; +      for(intK =1; K <= N; k++) A          for(inti =1; I <= N; i++) at              for(intj =1; J <= N; J + +) { -                 if(maps[i][j]>maps[i][k]+Maps[k][j]) -MAPS[I][J]=MAPS[I][K]+MAPS[K][J];//to update -             } -  -printf"%d", Maps[x][y]); in}

floyed algorithm O (N3) x

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.