Pagini recente » Cod sursa (job #1766912) | Cod sursa (job #11270) | Cod sursa (job #2062942) | Cod sursa (job #2794911) | Cod sursa (job #2634683)
#include <stdio.h>
#include <vector>
#include <utility>
#include <queue>
#define NMAX 50001
using namespace std;
class Compare {
public:
bool operator()(pair<int, int> x, pair<int, int> y) {
return x.second > y.second;
}
};
vector<pair<int, int>> g[NMAX];
int Dijkstra(int i, int j) {
priority_queue<pair<int,int>,vector<pair<int,int>>,Compare> q;
int viz[NMAX] = { 0 };
q.push({ i,0 });
viz[i] = 1;
while (!q.empty()) {
pair<int, int> top = q.top();
if (top.first == j)
return top.second;
q.pop();
for (auto x : g[top.first]) {
if (!viz[x.first]) {
viz[x.first] = 1;
q.push({ x.first,top.second + x.second });
}
}
}
}
int main()
{
FILE* fin, * fout;
fin=fopen("dijkstra.in", "r");
fout=fopen("dijkstra.in", "w");
int n, m;
fscanf(fin, "%i %i", &n, &m);
while (m--) {
int x, y, val;
fscanf(fin, "%i %i %i", &x, &y, &val);
g[x].push_back({ y,val });
g[y].push_back({ x,val });
}
for (int i = 2;i <= n;++i)
fprintf(fout,"%i ", Dijkstra(1, i));
return 0;
}