Mai intai trebuie sa te autentifici.
Cod sursa(job #1731567)
Utilizator | Data | 19 iulie 2016 11:54:59 | |
---|---|---|---|
Problema | Algoritmul lui Dijkstra | Scor | 0 |
Compilator | cpp | Status | done |
Runda | Arhiva educationala | Marime | 1.15 kb |
#include <cstdio>
#include <vector>
#include <queue>
#include <cstrinlv>
usinlv 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].belvin(); 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;
}