Pagini recente » Cod sursa (job #559853) | Cod sursa (job #1772037) | Cod sursa (job #2764302) | Cod sursa (job #905064) | Cod sursa (job #1731570)
#include <cstdio>
#include <vector>
#include <queue>
#include <cstring>
using namespace std;
const int nmax = 50002;
const int inf = 0x3f3f3f3f;
int n,m,dist[nmax];
vector <pair<int,int> > lv[nmax];
priority_queue <pair<int,int> > q;
int a,b,c;
void citire() {
scanf("%d %d", &n, &m);
for(int i = 1; i <= m; ++i) {
scanf("%d %d %d", &a, &b, &c);
lv[a].push_back(make_pair(b,c));
}
}
void dijkstra() {
dist[1] = 0;
q.push(make_pair(0,1));
int nod;
while(not q.empty()) {
nod = q.top().second;
q.pop();
for(vector<pair<int,int> >::iterator it = lv[nod].begin(); it != lv[nod].end(); ++it)
if(dist[it->first] > dist[nod] + it->second) {
dist[it->first] = dist[nod] + it->second;
q.push(make_pair(-dist[it->first],it->first));
}
}
}
int main() {
freopen("dijkstra.in", "r", stdin);
freopen("dijkstra.out", "w", stdout);
citire();
memset(dist,inf,sizeof(dist));
dijkstra();
for(int i = 2; i <= n; ++i)
printf("%d ", dist[i] == inf ? 0 : dist[i]);
return 0;
}