Floyed-warshall algorithm (to find the shortest distance between any two points)

Source: Internet
Author: User

Idea: feel a bit like violence ah, anyway I feel very violent, such as beg d[i][j], use this method to ask, directly consider will not go through a point K (k is any point), and finally get the minimum value

Look at the code

#include <iostream>#include<cstdio>#include<cstring>#include<stdio.h>#include<string.h>#include<cmath>#include<math.h>#include<algorithm>#include<Set>#include<queue>#include<map>typedefLong Longll;using namespacestd;Constll mod=1e9+7;Const intmaxn=1e3+Ten;Const intmaxk= -+Ten;Const intmaxx=1e4+Ten;Constll maxe= ++Ten;#defineINF 0x3f3f3f3f3f3fintV,e;ll D[MAXN][MAXN];//Cost[u][v] Represents the weight of the Edge (U,V), does not exist when initializing infinity, the same point is 0BOOLVIS[MAXN];voidsolve () { for(intI=0; i<v;i++)    {         for(intj=0; j<v;j++)        {             for(intk=0; k<v;k++) D[j][k]=min (d[j][k],d[j][i]+D[i][k]); }    }     for(intI=0; i<v;i++)    {         for(intj=i+1; j<v;j++) cout<<i<<" "<<j<<" "<<d[i][j]<<" "<<Endl; }}intMain () {Ios::sync_with_stdio (false); CIN>>v>>e;  for(intI=0; i<v;i++)    {         for(intj=0; j<v;j++) D[i][j]=INF; D[i][i]=0; }    intA,b,va;  for(intI=0; i<e;i++) {cin>>a>>b>>va; D[A][B]=va;    } solve (); return 0;}

Floyed-warshall algorithm (to find the shortest distance between any two points)

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.