C + + implementation Floyd-warshall algorithm

Source: Internet
Author: User

#include <iostream> #include <cctype> #include <sstream> #include <string> #include < algorithm> #include <map> #include <cstring> #include <cstdio> #include <iomanip> #include <vector> #include <queue>using namespace std;const int INF = 100000000;void Ch_status (vector<vector<  int> > & W, int nnodes) {//DP, just like bellmanford.for (int k = 0; k < nnodes; ++k) {for (int i = 0; i < Nnodes; ++i) {for (int j = 0; j < nnodes; ++j) {if (W[i][j] > W[i][k] + w[k][j]) {w[i][j] = W[i][k] + w[k][j];}}}} void display (const vector<vector<int> > & W) {int nnodes = W.size (); for (int i = 0; i < nnodes; ++i) {f or (int j = 0; j < nnodes; ++j) {cout << "" << setw (3) << w[i][j];} cout << Endl;}} int main (void) {cout << "Floyd-warshall algorithm for Directed acyclic Graph:" << Endl;while (true) {int Nnod Es;cout << "Number of Nodes:"; Cin >> NNODES;VECTOR&LT;VECTOR&Lt;int> > Wgt (nnodes, vector<int> (Nnodes, INF)); for (int i = 0; i < nnodes; ++i) wgt[i][i] = 0;int Nedges;cou T << "number of Edges:"; Cin >> nedges;cout << "SRC Dest Dist (<" << INF << "):" <& Lt  endl;for (int i = 0; i < nedges; ++i) {int src, dest, dist;cout << "[" << I << "]:"; CIN >> SRC >> dest >> Dist; Wgt[src][dest] = dist;} for (int i = 0, i < nnodes; ++i) {for (int j = 0; j < nnodes; ++j) {if (wgt[i][j]! = INF) cout << "" << SETW (3) << wgt[i][j];elsecout << "" << "INF";} cout << Endl;} O (n^3), amazing, the fastest ever known!! while (true) {Ch_status (WGT, Nnodes);d isplay (WGT); System ("Pause");}} return 0;}

C + + implementation Floyd-warshall 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.