Pagini recente » Cod sursa (job #1852514) | Cod sursa (job #1759150) | Cod sursa (job #486831) | Cod sursa (job #2327693) | Cod sursa (job #2176045)
#include <bits/stdc++.h>
#define nn 50003
#define mm 999999999
using namespace std;
ifstream f("dijkstra.in");
ofstream g("dijkstra.out");
vector<pair<int,int> >a[nn];
queue<pair<int,int> >q;
int dist[nn],x,y,z,n,m;
void dij(int k)
{
dist[k]=0;
q.push(make_pair(0,k));
while(!q.empty())
{
int nod =q.front().second;
q.pop();
vector<pair<int,int> >::iterator it;
for(it=a[nod].begin();it!=a[nod].end();it++)
if(dist[it->first]>dist[nod]+it->second)
{
dist[it->first]=dist[nod]+it->second;
q.push(make_pair(dist[it->first],it->first));
}
}
}
int main()
{
f>>n>>m;
for(int i =1; i <= m; ++i)
{
f >> x >> y >> z;
a[x].push_back(make_pair(y, z));
}
for(int i=1;i<=n;i++)
dist[i]=mm;
dij(1);
for(int i = 2; i <= n; ++i)
if(dist[i] == mm) g << 0 << " ";
else g << dist[i] << " ";
}