Pagini recente » Cod sursa (job #3121422) | Cod sursa (job #2946451) | Cod sursa (job #2770344) | Cod sursa (job #2931984) | Cod sursa (job #2972555)
#include <fstream>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.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();
}
}
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));
G[b].push_back(make_pair(a,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 << "-1 ";
}
return 0;
}