Pagini recente » Cod sursa (job #2368253) | Cod sursa (job #2197153) | Cod sursa (job #3170424) | Cod sursa (job #1448913) | Cod sursa (job #1852095)
#include<cstdio>
#include<queue>
#include<vector>
#include<cstdlib>
#include<algorithm>
using namespace std;
struct ura {
int cost,next;
};
vector <ura> g[101];
queue <int> Q;
bool in[10001];
int n,m,i,x,y,c,best[1001];
int main()
{
freopen ("bellmanford.in","r",stdin);
freopen ("bellmanford.out","w",stdout);
scanf ("%d%d",&n,&m);
for (i=1;i<=m;i++){
scanf ("%d%d%d",&x,&y,&c);
ura q;
q.next = y;
q.cost = c;
g[x].push_back(q);
ura qq;
qq.next = x;
qq.cost = c;
g[y].push_back(qq);
}
Q.push(1);
best[1]=0;
in[1]=true;
while (!Q.empty()){
int node=Q.front();
in[node]=false;
Q.pop();
for (auto&it:g[node]){
if (best[it.next]>best[node]+it.cost){
best[it.next]=best[node]+it.cost;
if (in[it.next]==false)
Q.push(it.next);
}
}
}
for (i=1;i<=n;i++)
printf ("%d ",best[i]);
return 0;
}