Pagini recente » Cod sursa (job #1134260) | Cod sursa (job #2852911) | Cod sursa (job #295346) | Cod sursa (job #1962086) | Cod sursa (job #2283932)
#include <fstream>
#include <vector>
#include <queue>
std::ifstream cin("djikstra.in");
std::ofstream cout("dijkstra.out");
#define it std::vector<std::pair<int,int> >::iterator
#define maxn 50050
const int INF=1<<30;
std::vector<std::pair<int,int> > g[maxn];
std::queue<int> q;
int n,m,d[maxn];
bool inqueue[maxn];
void bellman(){
int xd;
q.push(1);
inqueue[1]=true;
while(!q.empty()){
xd=q.front();
inqueue[xd]=false;
q.pop();
for(it iter=g[xd].begin();iter!=g[xd].end();iter++){
if(d[iter->first]>(d[xd]+iter->second)){
d[iter->first]=d[xd]+iter->second;
if(!inqueue[iter->first]){
q.push(iter->first);
inqueue[iter->first]=true;
}
}
}
}
}
int main()
{
int i,x,y,z;
cin>>n>>m;
for(i=2;i<=n;i++)
d[i]=INF;
for(;--m;){
cin>>x>>y>>z;
g[x].push_back(std::make_pair(y,z));
}
bellman();
for(i=2;i<=n;i++){
x=(d[i]==INF)?0:d[i];
cout<<x<<' ';
}
return 0;
}