Pagini recente » Cod sursa (job #1505661) | Cod sursa (job #747543) | Cod sursa (job #1953857) | Cod sursa (job #2939985) | Cod sursa (job #2859061)
#include <bits/stdc++.h>
using namespace std;
using PII = pair<int, int>;
ifstream fin("royfloyd.in");
ofstream fout("royfloyd.out");
vector<vector<PII>> graf;
int nr_noduri, nr_muchii, dist[101][101];
int main()
{
fin >> nr_noduri;
graf.resize(nr_noduri + 1);
int n1, n2, cost;
for (int i = 1; i <= nr_noduri; i++)
for (int j = 1; j <= nr_noduri; j++)
{
int cost;
fin >> cost;
if (cost)
graf[i].push_back(make_pair(j, cost));
}
for (int i = 1; i <= nr_noduri; i++)
for (int j = 1; j <= nr_noduri; j++)
dist[i][j] = INT_MAX;
for (int i = 1; i <= nr_noduri; i++)
{
priority_queue<PII, vector<PII>, greater<PII>> optim;
map<int, bool> visited;
optim.push(make_pair(0, i));
dist[i][i] = 0;
//cout << start << "\n";
while (optim.size() > 0)
{
PII current = optim.top();
//cout << current.first << " " << current.second << "\n";
optim.pop();
if (visited[current.second])
continue;
visited[current.second] = 1;
for (PII &nod:graf[current.second])
if (dist[i][current.second] + nod.second < dist[i][nod.first])
{
//cout << "Adaugat: " << nod.first << " " << nod.second << "\n";
dist[i][nod.first] = dist[i][current.second] + nod.second;
optim.push(make_pair(dist[i][nod.first], nod.first));
}
}
}
for (int i = 1; i <= nr_noduri; i++)
{
for (int j = 1; j <= nr_noduri; j++)
{
if (dist[i][j] == INT_MAX)
dist[i][j] = -1;
fout << dist[i][j] << " ";
}
fout << "\n";
}
return 0;
}