"Algorithm" Floyd-warshall algorithm (shortest-circuit problem between any two points) (judging negative circle)

Source: Internet
Author: User

Solving the shortest-circuit problem between all two points is called the shortest path between any two points.

Can be solved with dynamic programming,

D[K][I][J] Indicates the shortest path length using only the top k vertices and vertex I to the vertex J.

There are two kinds of situations to discuss:

1. After vertex k, d[k][i][j] = d[k-1][i][j]. That is equal to the shortest path when only the first k-1 vertices are used

2. Do not go through the vertex k, d[k][i][j] = D[k-1][i][k] + d[k-1][k][j]. That is, the shortest path from K is equal to the +j path of I away from K.

Can get recursive d[k][i][j] = min (D[k-1][i][j], d[k-1][i][k] + d[k-1][s][k]);

There are also two-dimensional representations:

D[i][j] = min (D[i][j], d[i][k] + d[k][j]);

Lite algorithm Templates:

#include <bits\stdc++.h>using namespacestd;#defineINF 2147483647#defineMax_v 1000#defineMax_e 2000intD[MAX_V][MAX_V];//D[u][v] Indicates the weight of the Edge e= (u,v) (not present when set to INF, but d[i][i] = 0)intV//fixed-point numbervoidWarshall_floyd () { for(intK =0; k < V; k++){         for(inti =0; i < V; i++){             for(intj =0; J < V; J + +) {D[i][j]= Min (D[i][j], d[i][k] +D[k][j]); }        }    }} intmain () {}

Negative Circle judgment:

Judge the negative circle only by judging whether there is d[i][i] is negative.

"Algorithm" Floyd-warshall algorithm (shortest-circuit problem between any two points) (judging negative circle)

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.