Pagini recente » Cod sursa (job #1972803) | Cod sursa (job #2457021) | Cod sursa (job #2946829) | Cod sursa (job #2755047) | Cod sursa (job #2972561)
#include <fstream>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
ifstream fin("bellmanford.in");
ofstream fout("bellmanford.out");
const long long inf=10000000000;
int n,p,m;
vector<pair<int,int>> G[50001];
long long dist[50001];
queue<int> q;
void dij(int x)
{
q.push(x);
dist[x]=0;
while(!q.empty())
{
int xc=q.front();
for(vector<pair<int,int>>::iterator it=G[xc].begin();it!=G[xc].end();++it)
{
if((*it).second+dist[xc]<dist[(*it).first])
{
dist[(*it).first]=(*it).second+dist[xc];
q.push((*it).first);
}
}
q.pop();
}
return 1;
}
int main()
{
fin >> n >> m;
for(int i=1;i<=m;i++)
{
int a,b,c;
fin >> a >> b >> c;
G[a].push_back(make_pair(b,c));
}
for(int i=1;i<=n;i++)
dist[i]=inf;
dij(1);
for(int i=2;i<=n;i++)
{
if(dist[i]==inf) fout << dist[i] << " ";
else fout << "0 ";
}
return 0;
}