Pagini recente » Cod sursa (job #2578675) | Cod sursa (job #666547) | Cod sursa (job #2470559) | Cod sursa (job #431995) | Cod sursa (job #1116372)
# include <iostream>
# include <fstream>
# include <vector>
# include <queue>
# include <string.h>
# define nmax 50002
using namespace std;
ifstream f("dijkstra.in");
ofstream g("dijkstra.out");
int n,m,dist[nmax];
vector < pair <int, int> > a[nmax];
int main()
{
int i,j,x,y,c;
f>>n>>m;
for(i=1; i<=m; i++)
{
f>>x>>y>>c;
a[x].push_back(make_pair(y,c));
}
bool inq[nmax];
queue <int> q;
vector < pair <int, int> > ::iterator it;
memset(dist, 5, sizeof(dist));
memset(inq, false, sizeof(inq));
dist[1]=0;
q.push(1);
int nod, unde, cost;
while(!q.empty())
{
nod=q.front();
q.pop();
inq[nod]=false;
for(it=a[nod].begin(); it!=a[nod].end(); ++it)
{
unde=it->first;
cost=it->second;
if(dist[nod]+it->second < dist[it->first])
{
dist[it->first]=dist[nod]+it->second;
if(!inq[it->first])
{
inq[it->first]=true;
q.push(it->first);
}
}
}
}
for(i=2; i<=n; i++)
if(dist[i]!=dist[nmax])
g<<dist[i]<<" ";
else
g<<"0 ";
return 0;
}