Pagini recente » Cod sursa (job #649040) | Cod sursa (job #1261279) | Cod sursa (job #584747) | Cod sursa (job #829680) | Cod sursa (job #2045625)
#include <cstdio>
#include <vector>
#include <queue>
#define Nmax 50005
using namespace std;
typedef pair<int,int> posib;
const int INF = int(1e9);
FILE *in = fopen("dijkstra.in","r");
FILE *out = fopen("dijkstra.out","w");
int n,m;
int lung[Nmax];
int y[Nmax];
vector <int> G[Nmax];
struct cmp {
bool operator () (const posib &a, const posib &b) {
/// maxHeap
return a.first < b.first;
/// minHeap
/// return a.first > b.first;
}
};
priority_queue < posib, vector<posib>, cmp > heap;
int d[Nmax];
int main()
{
fscanf(in,"%d%d",&n,&m);
for(int i=1;i<=m;i++)
{
int x;
fscanf(in,"%d%d%d",&x,&y[i],&lung[i]);
G[x].push_back(i);
}
for(int i=2; i<=n; i++) {
d[i] = INF;
}
d[1] = 0;
heap.push({0, 1});
while(!heap.empty())
{
posib p = heap.top(); heap.pop();
int bestx = -p.first;
int x = p.second;
if(bestx != d[x]) {
continue;
}
for(auto it : G[x])
{
int newcost = bestx + lung[it];
int where = y[it];
if(newcost < d[where]) {
d[where] = newcost;
heap.push({ -(newcost), where });
}
}
}
for(int i=2; i<=n; i++) {
if(d[i]!=INF)
fprintf(out, "%d ", d[i]);
else fprintf(out, "0 ");
}
return 0;
}