Pagini recente » Cod sursa (job #2332010) | Cod sursa (job #2122372) | Cod sursa (job #675827) | Cod sursa (job #2886538) | Cod sursa (job #2811895)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#include <stack>
#include <algorithm>
using namespace std;
ifstream in("royfloyd.in");
ofstream out("royfloyd.out");
const int nmax = 101;
const int inf = 1001;
class Graf
{
private:
bool orientat;
int nrNoduri;
vector <int> listaAdiacenta[nmax];
vector <pair <int, int>> listaAdiacentaCost[nmax];
public:
Graf(int nrNoduri = 0, bool orientat = false);
void adaugaMuchie(int, int);
void adaugaMuchieCost(int, int, int);
void citireMuchii(int);
void citireMuchiiCost(int);
vector<int> BFS(int);
void DFS(int, bool*);
int nrComponenteConexe();
void DFSStiva(int nodcurent, bool *, stack<int> &);
void sortareTopologica();
void TarjanCTC(int&, int, int*, int*, bool*, stack <int> &, vector<vector<int>> &);
void CTC();
void TarjanBC(int&, int, int, int*, int*, stack <int>&, vector<vector<int>>&);
void BC();
void TarjanMC(int&, int, int, int*, int*, vector<pair<int,int>>&);
void MC();
vector <int> citireHakimi();
bool Hakimi(vector <int>);
vector <int> Dijkstra(int);
pair<int, vector <pair <int, int>>> Prim(int);
vector <int> BellmanFord(int);
void reuniune(int, int, vector<int>&, vector<int>&);
int gasire(int, vector<int>&);
void verifica(int, int, vector<int>&);
void paduri();
void RoyFloyd(int matriceDrumuri[][nmax]);
void rezolvareRoyFloyd();
};
Graf :: Graf(int nrNoduri, bool orientat) : nrNoduri(nrNoduri), orientat(orientat)
{
this->nrNoduri = nrNoduri;
this->orientat = orientat;
}
void Graf:: adaugaMuchie(int nod1, int nod2)
{
listaAdiacenta[nod1].push_back(nod2);
}
void Graf::adaugaMuchieCost(int nod1, int nod2, int cost)
{
listaAdiacentaCost[nod1].push_back(make_pair(nod2, cost));
}
void Graf::citireMuchii(int nrMuchii)
{
int nod1, nod2;
for(int i = 0; i < nrMuchii; i++)
{
in >> nod1 >> nod2;
listaAdiacenta[nod1].push_back(nod2);
if(!orientat)
{
listaAdiacenta[nod2].push_back(nod1);
}
}
}
void Graf::RoyFloyd(int matriceDrumuri[][nmax])
{
for(int k = 1; k <= nrNoduri; k++)
{
for(int i = 1; i <= nrNoduri; i++)
{
for(int j = 1; j <= nrNoduri; j++)
{
matriceDrumuri[i][j] = min(matriceDrumuri[i][j], matriceDrumuri[i][k] + matriceDrumuri[k][j]);
}
}
}
}
void Graf::rezolvareRoyFloyd()
{
int matriceDrumuri[nmax][nmax];
for(int i = 1; i <= nrNoduri; i++)
{
for(int j = 1; j <= nrNoduri; j++)
{
in >> matriceDrumuri[i][j];
if(matriceDrumuri[i][j] == 0 && i != j)
{
matriceDrumuri[i][j] = inf;
}
}
}
RoyFloyd(matriceDrumuri);
for(int i = 1; i <= nrNoduri; i++)
{
for(int j = 1; j <= nrNoduri; j++)
{
if(matriceDrumuri[i][j] == inf)
{
out << 0 << " ";
continue;
}
out << matriceDrumuri[i][j] << " ";
}
out << "\n";
}
}
int main()
{
int n;
in >> n;
Graf g(n,true);
g.rezolvareRoyFloyd();
return 0;
}