Pagini recente » Cod sursa (job #245306) | Cod sursa (job #273175) | Cod sursa (job #1831457) | Cod sursa (job #355322) | Cod sursa (job #751247)
Cod sursa(job #751247)
#include<cstdio>
#include<vector>
#include<queue>
#define inf 0xfffffff
using namespace std;
struct muchie {int y, c;};
struct nod {int n, c;};
vector <muchie> muchii[50001];
vector <int> cost;
int n, m, poz;
struct compara{
bool operator()(const nod &n1, const nod& n2) const
{return n1.c > n2.c;}
};
priority_queue <nod, vector<nod>, compara> heap;
int main(){
freopen("dijkstra.in", "r", stdin), freopen("dijkstra.out", "w", stdout);
scanf("%d %d", &n, &m);
int i, x, y, c;
for (i = 0; i < m; i++){
scanf("%d %d %d", &x, &y, &c);
muchii[x].push_back( (muchie){y, c} );
}
cost.assign(n+1, inf);
cost[1] = 0;
heap.push( (nod){1, 0} );
while (!heap.empty()){
int x = heap.top().n;
heap.pop();
for (i = 0 ;i < (int)muchii[x].size(); i++){
y = muchii[x][i].y;
c = muchii[x][i].c;
if (cost[y] > cost[x] + c){
cost[y] = cost[x] + c;
heap.push( (nod){y, cost[y]} );
}
}
}
for (i = 2; i <= n; i++) printf("%d ", cost[i] != inf ? cost[i] : 0);
return 0;
}