Pagini recente » Cod sursa (job #1692282) | Cod sursa (job #2123505) | Cod sursa (job #1131151) | Cod sursa (job #3217074) | Cod sursa (job #2424327)
//prim
#include <iostream>
#include <vector>
#include <queue>
#include <set>
#include <fstream>
#include <climits>
using namespace std;
int main()
{
ifstream fin;
ofstream fout;
fin.open("royfloyd.in");
fout.open("royfloyd.out");
int n;
fin >> n;
int graphMatrix[101][101], dist[101][101];
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
fin >> graphMatrix[i][j];
if (graphMatrix[i][j] != 0)
dist[i][j] = graphMatrix[i][j];
else
dist[i][j] = INT_MAX;
}
dist[i][i] = 0;
}
for(int i = 1;i<=n;i++)
for(int j=1;j<=n;j++)
for(int k=1;k<=n;k++)
{
if(dist[i][j] > dist[i][k] + dist[k][j] && dist[i][k]!=INT_MAX && dist[k][j]!=INT_MAX)
{
dist[i][j] = dist[i][k] + dist[k][j];
}
}
for(int i=1;i<=n;i++)
{
for (int j = 1; j <= n; j++)
fout << dist[i][j]<<" ";
fout << "\n";
}
return 0;
}