Pagini recente » Cod sursa (job #1380019) | Cod sursa (job #74301) | Cod sursa (job #1080018) | Cod sursa (job #1168075) | Cod sursa (job #1131486)
#include <fstream>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
vector<pair<int,int> >g[50001];
queue<int>q;
int c[50001], n, m, x, y, z;
void Dijkstra(int x)
{
q.push(x);
for(int i = 2; i<= n; i++ )
c[i]=999999999;
while(!q.empty())
{
x=q.front(); q.pop();
for(vector<pair<int,int> >::iterator it=g[x].begin(); it<g[x].end(); it++ )
{
if(c[it->first]>c[x]+it->second)
{
c[it->first]=c[x]+it->second;
q.push(it->first);
}
}
}
}
int main()
{
fin>>n>>m;
for(int i = 1; i<= m; i++ )
{
fin>>x>>y>>z;
g[x].push_back(make_pair(y,z));
}
Dijkstra(1);
for(int i = 2; i<= n; i++ )
fout<<c[i]<<' ';
fin.close();
fout.close();
return 0;
}