Pagini recente » Cod sursa (job #1112953) | Cod sursa (job #2897690) | Cod sursa (job #417342) | Cod sursa (job #3151319) | Cod sursa (job #1012319)
#include<stdio.h>
#include<queue>
using namespace std;
const int NMAX = 50000,INF = 1 << 30;
struct GRAPH {
int node, cost;
const bool operator < (const GRAPH &other) const {
return cost > other.cost;
}
};
vector <GRAPH> graph[NMAX + 5];
vector <GRAPH>::iterator it;
priority_queue <GRAPH> heap;
int vis[NMAX + 5],dmin[NMAX + 5],n;
void dijkstra (int node) {
int i;
for(i = 1; i <= n; i++)
dmin[i] = INF;
dmin[1] = 0;
heap.push({node,0});
while(!heap.empty()) {
node = heap.top().node;
vis[node] = 1;
for(it = graph[node].begin(); it != graph[node].end(); it++)
if(!vis[it -> node] && dmin[node] + it -> cost < dmin[it -> node]) {
dmin[it -> node] = dmin[node] + it -> cost;
heap.push({it -> node,dmin[it -> node]});
}
while(!heap.empty() && vis[heap.top().node])
heap.pop();
}
}
int main() {
freopen("dijkstra.in","r",stdin);
freopen("dijkstra.out","w",stdout);
int a,b,c,m,i;
scanf("%d%d",&n,&m);
for(i = 1; i <= m; i++) {
scanf("%d%d%d",&a,&b,&c);
graph[a].push_back({b,c});
}
dijkstra(1);
for(i = 2; i <= n; i++) {
if(dmin[i] == INF)
dmin[i] = 0;
printf("%d ",dmin[i]);
}
return 0;
}