Pagini recente » Romanii medaliati la IOI | Cod sursa (job #2341789) | Cod sursa (job #3226621) | Cod sursa (job #2126884) | Cod sursa (job #1570423)
#include <iostream>
#include <fstream>
#include <queue>
using namespace std;
const int nmax = 50005;
const int oo = (1<<29);
vector <pair<int,int> > g[nmax];
int dist[nmax], n;
void dijkstra(int start)
{
queue <int> q;
int i, dad, son, cost;
for(i=1; i<=n; i++)
dist[i]=oo;
dist[start]=0;
q.push(start);
while(!q.empty())
{
dad=q.front();
q.pop();
for(i=0; i<g[dad].size(); i++)
{
son=g[dad][i].first;
cost=g[dad][i].second;
if(dist[dad]+cost < dist[son])
{
dist[son]=dist[dad]+cost;
q.push(son);
}
}
}
}
int main()
{
ifstream fin ("dijkstra.in");
ofstream fout ("dijkstra.out");
ios_base::sync_with_stdio(false);
int m, x, y, c, i;
fin >> n >> m;
for(i=1; i<=m; i++)
{
fin >> x >> y >> c;
g[x].push_back(make_pair(y, c));
}
dijkstra(1);
for(i=2; i<=n; i++)
{
if(dist[i]==oo) dist[i]=0;
fout << dist[i] << " ";
}
fin.close();
fout.close();
return 0;
}