Floyd algorithm of shortest circuit

Source: Internet
Author: User

Floyd

In my opinion this is the simplest of the shortest path algorithm, and also the low one.

So a big guy in our group gave him a new name, called Timeout!!!

(In fact, if the data range is very small, this algorithm is very good use!!) )

This algorithm is relatively simple, or we directly on the code?!

#include <cstdio>#include<cstdlib>#include<cstring>#include<iostream>#include<algorithm>#defineMAXN 1000+15#defineOO 123456using namespacestd;intDIS[MAXN][MAXN];intMain () {intx,y,z,n,m; scanf ("%d%d",&n,&m);  for(intI=1; i<=n;i++)      for(intj=1; j<=n;j++) Dis[i][j]=oo* (i!=j);//In addition to the point to the rest of the points outside of himself, the rest of the points are all paid to the maximum value for(intI=1; i<=m;i++) {scanf ("%d%d%d",&x,&y,&z); Dis[x][y]=z;//Input two inter-city and distance} for(intk=1; k<=n;k++)//Enumerate intermediate points       for(intI=1; i<=n;i++)//enumeration starting point        for(intj=1; j<=n;j++)//Enumerate EndpointsDis[i][j]=min (dis[i][j],dis[i][k]+Dis[k][j]); printf ("%d", dis[1][n]);//outputs the shortest path from 1 to n    return 0;}

It is also important to note that the Floyd-warshall algorithm cannot solve a graph with a "negative weight loop" (or "negative weight loop") because there is no shortest path with a "negative weight loop" diagram.

Floyd algorithm of shortest circuit

Related Article

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.