Pagini recente » Cod sursa (job #2195962) | Cod sursa (job #1136997) | Cod sursa (job #156292) | Cod sursa (job #425910) | Cod sursa (job #1629812)
#include <bits/stdc++.h>
const int NMAX=50005, INF = 1e9;
using namespace std;
typedef pair<int, int> link;
vector<link> links[NMAX];
int mcost[NMAX];
inline void dijkstra(int p){
queue<int> q;
int t;
q.push(p);
mcost[p]=0;
while(!q.empty()) {
t=q.front();
q.pop();
for(auto &i: links[t]) {
if(mcost[i.first] > i.second + mcost[t]) {
mcost[i.first] = i.second + mcost[t];
q.push(i.first);
}
}
}
}
int main(void){
//freopen("dat.in","r",stdin);
freopen("dijkstra.in","r",stdin);
freopen("dijkstra.out","w",stdout);
int n,p,u,v,c;
scanf("%d%d",&n,&p);
while(scanf("%d%d%d",&u,&v,&c)!=EOF)
links[u].push_back(make_pair(v,c));
for(int i=0; i<NMAX; ++i)
mcost[i]=INF;
dijkstra(1);
for(int i=2; i<=n; ++i)
printf("%d ",mcost[i]);
return 0;
}