Pagini recente » Cod sursa (job #1226309) | Cod sursa (job #363292) | Cod sursa (job #1404659) | Cod sursa (job #239287) | Cod sursa (job #2575701)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
const int NMAX=50001;
const int INF=1e9;
int n,m;
int d[NMAX];
bool inpq[NMAX];
struct graph {
int n,c;
};
vector <graph> g[NMAX];
class cmp {
public:
bool operator() (int x, int y) {
return d[x]>d[y];
};
};
priority_queue <int,vector <int>, cmp> pq;
void dijkstra(int start) {
d[start]=0;
pq.push(start);
inpq[start]=true;
while(!pq.empty()) {
int node=pq.top();
pq.pop();
inpq[node]=false;
for(int i=0; i<(int)g[node].size(); i++) {
graph next=g[node][i];
if(d[next.n]>d[node]+next.c) {
d[next.n]=d[node]+next.c;
if(inpq[next.n]==false) {
pq.push(next.n);
inpq[next.n]=true;
}
}
}
}
}
int main() {
fin>>n>>m;
for(int i=1; i<=m; i++) {
graph aux;
int x;
fin>>x;
fin>>aux.n>>aux.c;
g[x].push_back(aux);
}
for(int i=1; i<=n; i++)
d[i]=INF;
dijkstra(1);
for(int i=2;i<=n;i++)
if(d[i]==INF)
fout<<"0 ";
else
fout<<d[i]<<" ";
return 0;
}