Playground
#include <iostream> #include <cstring> #include <algorithm> using namespace std; const int MAX_V = 101; int V; int dist[MAX_V][MAX_V]; void floyd_warshall() { for (int k=0; k<V; k++) { for (int i=0; i<V; i++) { for (int j=0; j<V; j++) { dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]); } } } } int main() { cout << "Number of vertices: " << endl; cin >> V; //inicijaliziraj gi vrednostite vo dist for (int i=0; i<V; i++) { for (int j=0; j<V; j++) { if (i == j) { //rastojanie od teme do samoto sebe e 0 dist[i][i] = 0; } else { //inaku, pochni so nekoja golema vrednost //koja oznachuva deka seushte ne sme pronashle pat dist[i][j] = 1000000000; } } } int E; cout << "Number of edges: " << endl; cin >> E; cout << "Enter the edges..." << endl; for (int i=0; i<E; i++) { int from, to, weight; cin >> from >> to >> weight; dist[from][to] = weight; //dist[to][from] = weight; (ako grafot e nenasocen) } //izvrshi go algoritamot floyd_warshall(); //neka gi ispechatime site rastojanija for (int i=0; i<V; i++) { for (int j=0; j<V; j++) { cout << "dist[" << i << "][" << j << "] = " << dist[i][j] << endl; } cout << endl; } return 0; }
Input data
Program output
3 4 0 1 10 0 2 20 1 2 30 2 0 40
Execute
Language: ????????? | Memory: ???? KB | Time: ??? ms
Nothing has been executed, yet!