Pagini recente » Cod sursa (job #980163) | Cod sursa (job #1930799) | Cod sursa (job #275010) | Cod sursa (job #2196642) | Cod sursa (job #608321)
Cod sursa(job #608321)
#include <fstream>
#include <cstring>
#include <queue>
#include <vector>
#define nmax 50005
#define f "dijkstra.in"
#define g "dijkstra.out"
#define inf 123456789
using namespace std;
vector< pair<int,int> > gf[nmax];
queue<int> Q;
int n, m, d[nmax], viz[nmax];
void bellman_ford_cu_coada(){
memset(d, inf, sizeof(d));
viz[1]=1;
d[1]=0;
for(Q.push(1);!Q.empty();Q.pop()){
int nod = Q.front();
viz[nod]=0;
for(vector< pair<int,int> >:: iterator it=gf[nod].begin(); it!=gf[nod].end(); it++){
int y = it->first;
int c = it->second;
if (d[y] > d[nod] + c){
d[y] = d[nod] + c;
if (viz[y]==0){
Q.push(y);
viz[y]=1;
}
}
}
}
}
int main(){
ifstream in(f);
ofstream out(g);
in>>n>>m;
for(int i=1; i<=m; i++){
int x, y, c;
in>>x>>y>>c;
gf[x].push_back(make_pair(y,c));
}
bellman_ford_cu_coada();
for(int i=2; i<=n; i++) out<<(d[i] < inf ? d[i] : 0)<<" ";
in.close();
out.close();
return 0;
}